【发布时间】: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