问题
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.
- Create a new Windows Form project.
- Add a Chart control onto Form1.
- 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.
回答1:
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.
来源:https://stackoverflow.com/questions/4935751/adding-a-wmf-to-a-pdf-using-itextsharp