2011-02-08 107 views
0

我完全無法使用iTextSharp將WMF文件添加到PDF上。我特別想使用WMF,因爲它是一個基於矢量的文件。使用iTextSharp添加WMF到PDF

我的WMF文件來自Chart控件。

重現此代碼非常簡單。

  1. 創建一個新的Windows窗體項目。
  2. 將圖表控件添加到Form1上。
  3. 然後添加以下代碼:

添加此使用指令

using iTS = iTextSharp.text; 

而下面的代碼添加到您的Form1.cs的文件:

private void Form1_Load(object sender, EventArgs e) 
{ 
    Document pdfDoc = new Document(); 
    PdfWriter.GetInstance(pdfDoc, new FileStream(@"D:\dev\Test\TestPdfCreation\TestPdfCreation\bin\Debug\test.pdf", FileMode.Create)); 

    MemoryStream mimg1 = new MemoryStream(); 
    chart1.SaveImage(mimg1, ImageFormat.Wmf); 
    mimg1.Seek(0, SeekOrigin.Begin); 
    iTS.Image img1 = iTS.Image.GetInstance(mimg1); 
    pdfDoc.Add(img1); 
    pdfDoc.Close(); 
} 

我收到的錯誤是: 發生IOException。字節數組不是可識別的圖像格式。

使用iTextSharp 5.0.5。

+0

我使用的WMF文件沒有任何問題,但我從磁盤加載它。你可以將它寫入磁盤並通過路徑加載它,看看它是否有效?我想知道是否有內存流的問題。 – 2011-02-08 17:07:11

+0

再看一點,似乎有兩種不同類型的WMF文件。 Windows 3.0版本的前六個字節是「01 00 09 00 00 03」或「02 00 09 00 00 03」。但是,當我從Adobe Illustrator製作WMF時,我將`D7 CD C6 9A`作爲前4個字節。後者是iTextSharp適用於我的。 – 2011-02-08 17:24:57

回答

3

你可以嘗試建立直接而不是調用的createImage:

Image img = new ImgWMF(bytes); 

雖然看着我看到你只是得到一個不同的異常代碼:

 InputMeta in = new InputMeta(is); 
     if (in.readInt() != 0x9AC6CDD7) { 
      throw new BadElementException(errorID + " is not a valid placeable windows metafile."); 
     } 

這裏的關鍵點可能是「可置換的」。我不完全熟悉WMF,但你可能會找到替代ImageFormat什麼的。

是的。看起來可以放置的WMFs是Aldus想出來的。這裏有一個關於從WMF轉換的東西iText的可以使用的問題:

http://itext-general.2136553.n4.nabble.com/WMF-file-doesn-t-display-correctly-td2283480.html

這個特殊的問題曾與漸變填充做。