【问题标题】:Blank pages getting added, while merging multiple TIFF images into a single PDF添加空白页,同时将多个 TIFF 图像合并到一个 PDF 中
【发布时间】:2020-04-02 12:55:05
【问题描述】:

我正在尝试将多个 TIFF(多页)图像合并到一个 PDF 中,该 PDF 的页数与合并的 tiff 相同。

使用 PDFsharp 库, 我正在合并 2 个 TIFF 图像,每个图像有 100 页。

问题是第一张图片被完美复制到 PDF 中,但是从第 101 页开始,所有页面都是空白的。即从第二张 TIFF 图像开始的所有 PDF 页面都是空白的。

我不确定是什么导致了这个问题,有人可以帮我解决这个问题吗?

这是我的代码。 filePathWithFileName 将具有包含多个 TIFF 图像的 Zip 文件夹的路径。

private static void MergeTiffToPDF(string filePathWithFileName)
{
  string[] sa;
  sa = Directory.GetFiles(filePathWithFileName.Substring(0, filePathWithFileName.LastIndexOf('.')));
  string destinaton = "C:\\Users\\someuser\\Desktop\\PDF_TIF_Document.pdf";
  PdfDocument doc = new PdfDocument();
  foreach (string s in sa)
  {
    Image MyImage = Image.FromFile(s);
    for (int PageIndex = 0; PageIndex < MyImage.GetFrameCount(FrameDimension.Page); PageIndex++)
    {
     MyImage.SelectActiveFrame(FrameDimension.Page, PageIndex);
     XImage img = XImage.FromGdiPlusImage(MyImage);
     var page = new PdfPage();
     if (img.PixelWidth > img.PixelHeight)
     {
      page.Orientation = PageOrientation.Landscape;
     }
     else
     {
      page.Orientation = PageOrientation.Portrait;
     }
      doc.Pages.Add(page);
      XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[PageIndex]);
      xgr.DrawImage(img, 0, 0);
      xgr.Dispose();
     }
     doc.AddPage();
     MyImage.Dispose();
     }
     doc.Save(destinaton);
     doc.Close();
}

【问题讨论】:

    标签: c# .net pdf-generation pdfsharp


    【解决方案1】:

    问题来了:

    XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[PageIndex]);
    

    您必须添加 101 才能在第二个文件的正确页面上绘图。

    您在第 1 到 100 页上看到第二个文件的图像,而第一个文件中的图像被隐藏了。

    【讨论】:

    • 第一个文件图像是否被隐藏或覆盖?
    • 隐藏。您可以有多个重叠图像,但 PDFsharp 不会替换图像。如果图像具有透明区域或不同大小,那么您可以同时看到这两个图像。
    猜你喜欢
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 2017-04-17
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多