如何将PDF文件分割成小文件
How to split PDF document into small ones
我需要将一个文档拆分成几个小文档。例如,如果文档有 7 页,我需要生成 7 个 pdf。
在 iTextSharp 中,我使用了以下代码,效果很好。但是,在 iText 7 中不可能以相同的方式进行。
iTextSharp 旧代码
var reader = new PdfReader(src);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
var document = new Document();
var copy = new PdfCopy(document, new FileStream(result + i + ".pdf", FileMode.Create));
document.Open();
copy.AddPage(copy.GetImportedPage(reader, i));
document.Close();
}
iText 7,但无法正常工作
第一个问题
我发现有 PdfSplitter
,它可以将我的 pdf 拆分成小的 pdf。但是,即使我的测试 pdf 有 7 页,甚至 GetNumberOfPages()
returns 第 7 页,拆分文档的数量也只有一个。
在此 linked documenation 中以某种方式显示了如何拆分文档。但是,我不知道如何制作与提到的方法类似的方法 - getNextPdfWriter
第二题
即使我有一个文件,它也是空的。我想知道如何设置合适的作者来创建正确的 pdf。分别如何设置reader才能读取拆分后的文档内容
string result = outputPath + @"/page00";
using (pdfDocument = new PdfDocument(new PdfReader(pdfPath)))
{
var splitter = new PdfSplitter(pdfDocument);
var splittedDocs = splitter.SplitByPageCount(pdfDocument.GetNumberOfPages());
for (int i = 0; i < pdfDocument.GetNumberOfPages(); i++)
{
//how to set reader to read the content of splitted docs. Or how to set writer for splitted doc.
var pdfDoc = new PdfDocument(new PdfWriter(new FileStream(result + i + ".pdf", FileMode.Create)));
pdfDoc.Close();
splittedDocs[i].Close();
}
}
问题
How to properly split document into small ones in .NET core with iText 7
好吧,这很容易。根据链接文档,我做了以下操作:
创建 PdfSplitter 的自定义拆分器覆盖功能。
class CustomSplitter : PdfSplitter
{
private int _order;
private readonly string _destinationFolder;
public CustomSplitter(PdfDocument pdfDocument, string destinationFolder) : base(pdfDocument)
{
_destinationFolder = destinationFolder;
_order = 0;
}
protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
{
return new PdfWriter(_destinationFolder + "splitDocument1_" + _order++ + ".pdf");
}
}
然后就用它来分割PDF文件吧。不要忘记关闭拆分的文档。我想指出一件事。 SplitByPageCount
- 它需要根据应该拆分的数字。 SplitByPageCount(1)
将 PDF 文档拆分为一页。我真的误解了这个方法。
using (var pdfDoc = new PdfDocument(new PdfReader("doc.pdf")))
{
var outputDir = @"C:\";
var splitter = new CustomSplitter(pdfDoc, outputDir);
var splittedDocs = splitter.SplitByPageCount(1);
foreach (var splittedDoc in splittedDocs)
{
splittedDoc.Close();
}
}
结果是几个pdf。
我需要将一个文档拆分成几个小文档。例如,如果文档有 7 页,我需要生成 7 个 pdf。
在 iTextSharp 中,我使用了以下代码,效果很好。但是,在 iText 7 中不可能以相同的方式进行。
iTextSharp 旧代码
var reader = new PdfReader(src);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
var document = new Document();
var copy = new PdfCopy(document, new FileStream(result + i + ".pdf", FileMode.Create));
document.Open();
copy.AddPage(copy.GetImportedPage(reader, i));
document.Close();
}
iText 7,但无法正常工作
第一个问题
我发现有 PdfSplitter
,它可以将我的 pdf 拆分成小的 pdf。但是,即使我的测试 pdf 有 7 页,甚至 GetNumberOfPages()
returns 第 7 页,拆分文档的数量也只有一个。
在此 linked documenation 中以某种方式显示了如何拆分文档。但是,我不知道如何制作与提到的方法类似的方法 - getNextPdfWriter
第二题
即使我有一个文件,它也是空的。我想知道如何设置合适的作者来创建正确的 pdf。分别如何设置reader才能读取拆分后的文档内容
string result = outputPath + @"/page00";
using (pdfDocument = new PdfDocument(new PdfReader(pdfPath)))
{
var splitter = new PdfSplitter(pdfDocument);
var splittedDocs = splitter.SplitByPageCount(pdfDocument.GetNumberOfPages());
for (int i = 0; i < pdfDocument.GetNumberOfPages(); i++)
{
//how to set reader to read the content of splitted docs. Or how to set writer for splitted doc.
var pdfDoc = new PdfDocument(new PdfWriter(new FileStream(result + i + ".pdf", FileMode.Create)));
pdfDoc.Close();
splittedDocs[i].Close();
}
}
问题
How to properly split document into small ones in .NET core with iText 7
好吧,这很容易。根据链接文档,我做了以下操作:
创建 PdfSplitter 的自定义拆分器覆盖功能。
class CustomSplitter : PdfSplitter
{
private int _order;
private readonly string _destinationFolder;
public CustomSplitter(PdfDocument pdfDocument, string destinationFolder) : base(pdfDocument)
{
_destinationFolder = destinationFolder;
_order = 0;
}
protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
{
return new PdfWriter(_destinationFolder + "splitDocument1_" + _order++ + ".pdf");
}
}
然后就用它来分割PDF文件吧。不要忘记关闭拆分的文档。我想指出一件事。 SplitByPageCount
- 它需要根据应该拆分的数字。 SplitByPageCount(1)
将 PDF 文档拆分为一页。我真的误解了这个方法。
using (var pdfDoc = new PdfDocument(new PdfReader("doc.pdf")))
{
var outputDir = @"C:\";
var splitter = new CustomSplitter(pdfDoc, outputDir);
var splittedDocs = splitter.SplitByPageCount(1);
foreach (var splittedDoc in splittedDocs)
{
splittedDoc.Close();
}
}
结果是几个pdf。