问题描述
我完全无法使用iTextSharp将WMF文件转换为PDF。我特别想使用WMF,因为它是一个基于矢量的文件。
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.
我的WMF文件来自控件。
My WMF file is coming from a Chart control.
重现这一点的代码非常简单。
The code to reproduce this is Very Easy.
- 创建一个新的Windows窗体项目。
- 在Form1上添加一个Chart控件。
- 然后添加以下代码:
使用指令添加此
using iTS = iTextSharp.text;
并将以下代码添加到Form1.cs文件中:
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();
}
我收到的错误是:
IOException发生。字节数组不是可识别的图像格式。
The error I receive is: IOException occurred. The byte array is not a recognized image format.
使用iTextSharp 5.0.5。
Using iTextSharp 5.0.5.
推荐答案
您可以尝试直接构建它而不是调用createImage:
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.");
}
此处的关键点可能是可放置。我并不完全熟悉WMF,但你可以找到一个替代的 ImageFormat
或其他东西。
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.
是的。看起来像可放置的WMF是Aldus不久前发现的东西。这是一个关于从WMF转换为iText可以使用的问题:
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:
这个特殊问题与渐变填充有关。
This particular problem had to do with gradient fills.
这篇关于使用iTextSharp将WMF添加到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!