簡體   English   中英

使用iTextSharp將WMF添加到PDF

[英]Adding a WMF to a PDF using iTextSharp

我完全無法使用iTextSharp將WMF文件轉換為PDF。 我特別想使用WMF,因為它是基於矢量的文件。

我的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。

您可以嘗試直接構建它,而不是調用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或其他東西。

是的 似乎可放置的WMF是Aldus不久前提出的。 這是有關從WMF轉換為iText可以使用的內容的問題:

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

這個特殊問題與漸變填充有關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM