我目前正在尝试分割PDF文件,然后将每个ne保存PdfDocument
到一个新文件中。问题是我找不到将新的东西附加PdfWriter
到现有的方法PdfDocument
。这是我当前用来打开和拆分PDF文件的代码(只是一个样机代码示例):
IList<int> splitByPage = new List<int>() { 1,2,3};
PdfDocument pdfDoc = new PdfDocument(new PdfReader(@"C:\temp\test.pdf"));
PdfSplitter splitter = new PdfSplitter(pdfDoc);
IList<PdfDocument> splittedDocuments = splitter.SplitByPageNumbers(splitByPage);
这正在工作,我有一组PdfDocument
对象。现在,我想将它们保存到新文件中。我在Java中找到了一个解决方案,可以在该解决方案的基础上基于给定的Document
和创建一个新的PdfWriter实例,OutputStream
但是在.net中,我找不到与之等效的解决方案。谢谢你的帮助!
简而言之,您需要通过在GetNextPdfWriter方法上创建一个新的PdfWriter实例来扩展PdfSplitter类如何处理文档。
public static readonly String DEST = "splitDocument1_{0}.pdf";
public void Split()
{
IList<int> splitByPage = new List<int>() {1, 2, 3};
PdfDocument pdfDoc = new PdfDocument(new PdfReader(@"C:\temp\hello.pdf"));
PdfSplitter splitter = new PdfSplitter(pdfDoc);
IList<PdfDocument> splittedDocuments = new CustomPdfSplitter(pdfDoc, DEST).SplitByPageNumbers(splitByPage);
foreach (PdfDocument doc in splittedDocuments)
{
doc.Close();
}
pdfDoc.Close();
}
private class CustomPdfSplitter : PdfSplitter
{
private String dest;
private int partNumber = 1;
public CustomPdfSplitter(PdfDocument pdfDocument, String dest) : base(pdfDocument)
{
this.dest = dest;
}
protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
{
return new PdfWriter(String.Format(dest, partNumber++));
}
}
我在网上找不到GetNextPdfWriter的文档,但是这里是源代码:
/// <summary>This method is called when another split document is to be created.</summary>
/// <remarks>
/// This method is called when another split document is to be created.
/// You can override this method and return your own
/// <see cref="T:iText.Kernel.Pdf.PdfWriter" />
/// depending on your needs.
/// </remarks>
/// <param name="documentPageRange">the page range of the original document to be included in the document being created now.
/// </param>
/// <returns>the PdfWriter instance for the document which is being created.</returns>
protected internal virtual PdfWriter GetNextPdfWriter(PageRange documentPageRange)
除了Github上的示例外,我还可以在Volume Counter FAQ(第3个示例)上找到一个示例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句