添加空白页面,同时将多个TIFF图像合并为一个PDF

问题描述 投票:-1回答:1

[我正在尝试将多个TIFF(多页)图像合并为一个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个回答
0
投票

这里是问题:

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.