简体   繁体   中英

Adding a WMF to a PDF using iTextSharp

I am completely unable to get a WMF file onto a PDF using iTextSharp. I specifically want to use WMF because it is a vector based file.

My WMF file is coming from a Chart control.

The code to reproduce this is Very Easy.

  1. Create a new Windows Form project.
  2. Add a Chart control onto Form1.
  3. Then add the below code:

Add this using directive

using iTS = iTextSharp.text;

And add the below code to your Form1.cs file:

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();
}

The error I receive is: IOException occurred. The byte array is not a recognized image format.

Using iTextSharp 5.0.5.

You might try building it directly rather than calling createImage:

Image img = new ImgWMF( bytes );

Though looking at the code I see that you'll just get a different exception:

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

The key point here might be "placeable". I'm not exactly familiar with WMF, but you might be able to find an alternate ImageFormat or something.

Yep. It looks like placeable WMFs are something Aldus came up with a while back. Here's a question about converting from WMF to something iText can use:

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

This particular problem had to do with gradient fills.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM