2011-02-08 149 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

这个特殊的问题曾与渐变填充做。

相关问题