如何将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 文档 = 新文档();var copy = new PdfCopy(document, new FileStream(result + i + ".pdf", FileMode.Create));文档.Open();copy.AddPage(copy.GetImportedPage(reader, i));文档.关闭();}
iText 7,但不工作
第一个问题
我发现有 PdfSplitter
,它可以将我的 pdf 分成小 pdf.然而,即使我的测试 pdf 也有 7 页,甚至 GetNumberOfPages()
返回数字 7,拆分文档的数量只是一个.
在这个链接文档 以某种方式显示了如何拆分文档.但是,我不知道如何制作与上述方法类似的方法 - getNextPdfWriter
第二个问题
即使我有一个文件,它是空的.我想知道如何设置合适的作者来创建正确的 pdf.分别是如何设置reader来读取分割文档的内容.
string result = outputPath + @"/page00";使用 (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++){//如何设置阅读器读取拆分文档的内容.或者如何为拆分文档设置作者.var pdfDoc = new PdfDocument(new PdfWriter(new FileStream(result + i + ".pdf", FileMode.Create)));pdfDoc.Close();splittedDocs[i].Close();}}
问题
<块引用>如何使用 iText 7 在 .NET Core 中正确地将文档拆分为小文档
嗯,这很容易.根据链接的文档,我做了以下事情:
创建自定义拆分器覆盖 PdfSplitter 的功能.
class CustomSplitter : PdfSplitter{私人 int _order;私有只读字符串_destinationFolder;公共 CustomSplitter(PdfDocument pdfDocument, string destinationFolder) : base(pdfDocument){_destinationFolder = 目标文件夹;_order = 0;}受保护的覆盖 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(splittedDocs 中的 var splittedDoc){splittedDoc.Close();}}
结果是几个 pdf.
I need to split a document into several small documents. For example, if document has 7 pages I need to generate 7 pdfs.
In iTextSharp i was using the following code, works pretty well. However, in iText 7 its not possible to do it in the same way.
iTextSharp old code
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, but not working
First problem
I have found that there is PdfSplitter
, wich could split my pdf into small pdfs. However, even my testing pdf has 7 pages and even GetNumberOfPages()
returns number 7, number of splitted document is just one.
In this linked documenation is somehow shown how to split document. However, I have no idea how to make similiar method to the one mentioned - getNextPdfWriter
Second problem
Even I have one file, its empty. I am wondering how to set proper writer to create correct pdf. Respectively, how to set reader in order to read the content of splitted document.
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();
}
}
Question
How to properly split document into small ones in .NET core with iText 7
Well, it was quite easy. According to linked documentation I did the following:
Create custom splitter overriding functionality of 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");
}
}
Then just use it for splitting a PDF document. Dont forget to close splitted document. And I would like to point out one thing. SplitByPageCount
- it takes number according to wich should be splitted. SplitByPageCount(1)
split a PDF document by one page. I trully misunderstood this method.
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();
}
}
The result is a few pdfs.
这篇关于如何将PDF文档拆分成小文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!