【发布时间】:2018-03-04 16:26:24
【问题描述】:
我正在尝试逐页拆分 PDF 文件,并获取每个页面文件的字节数组。但是,在 iText 7.0.4 版 C# 中,我无法将每个页面转换为字节数组。
其他解决方案中引用的方法依赖于 PdfWriter.GetInstance 或 PdfCopy,这在 iText 版本 7.0.4 中似乎不再存在。
我浏览了 iText 的示例代码和 API 文档,但无法从中提取任何有用的信息。
using (Stream stream = new MemoryStream(pdfBytes))
using (PdfReader reader = new PdfReader(stream))
using (PdfDocument pdfDocument = new PdfDocument(reader))
{
PdfSplitter splitter = new PdfSplitter(pdfDocument);
// My Attempt #1 - None of the document's functions seem to be of help.
foreach (PdfDocument splitPage in splitter.SplitByPageCount(1))
{
// ??
}
// My Attempt #2 - GetContentBytes != pdf file bytes.
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++)
{
PdfPage page = pdfDocument.GetPage(i);
byte[] bytes = page.GetContentBytes();
}
}
任何帮助将不胜感激。
【问题讨论】:
-
您在处理压缩的 pdf 文件吗?压缩单个页面不会产生与压缩整个文件时相同的字节数。所以你应该找到一种比“返回文件中找到的相同字节”更好的方法来定义成功
-
不,我正在处理未压缩的 PDF 文件。我所需要的只是拆分 PDF 文件并存储拆分页面以供以后使用的能力。拆分后,我无需担心将原始文档重新组合在一起。