将现有的PdfDocument保存到文件

罗曼诺·祖贝(RomanoZumbé)

我目前正在尝试分割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中,我找不到与之等效的解决方案。谢谢你的帮助!

安德烈·莱莫斯(AndréLemos)

简而言之,您需要通过在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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将现有的Azure资源组保存到Resource Manager模板JSON文件中?

来自分类Dev

保存到现有的excel文件而无需打开新的excel文件

来自分类常见问题

如何使用Pandas将新工作表保存在现有的Excel文件中?

来自分类Dev

Imacross ..将提取的数据保存在现有的.csv文件中

来自分类Dev

将数据结构保存在现有的 .mat 文件中,无需嵌套

来自分类Dev

将CSV文件保存到现有文件中,但保存在新工作表中,然后重命名工作表

来自分类Dev

从现有的base64编码保存Excel文件

来自分类Dev

使用 Swift 在 Firebase 中保存现有的 JSON 文件?

来自分类Dev

将乘法结果保存到现有数组

来自分类Dev

将文件添加到现有的zipfile

来自分类Dev

将目录添加到现有的.zip文件

来自分类Dev

将页面添加到现有的pdf文件

来自分类Dev

如何将XElement插入现有的xml文件?

来自分类Dev

将SQL文件导入现有的DB表(phpmyadmin)

来自分类Dev

将数据附加到现有的 json 文件

来自分类Dev

将现有项目推送到github删除现有的自述文件

来自分类Dev

使用现有的 Excel 文件保存数据并以新名称保存

来自分类Dev

视觉工作室。调试。如何在运行期间将变量具有的所有值保存到文件中?

来自分类Dev

如何保存/覆盖现有的Excel文件而没有消息

来自分类Dev

Inno Setup将文件和文件夹复制到现有的Zip文件

来自分类Dev

将所有终端输出保存到文件

来自分类Dev

将NSDictionary保存到文件是否有任何限制

来自分类Dev

将grep的输出保存到带有颜色的文件中

来自分类Dev

将NSDictionary保存到文件是否有任何限制

来自分类Dev

将所有终端输出保存到文件

来自分类Dev

将BufferedHttpEntity保存到文件

来自分类Dev

将结构保存到文件

来自分类Dev

将字典保存到文件

来自分类Dev

将质数保存到文件

Related 相关文章

  1. 1

    如何将现有的Azure资源组保存到Resource Manager模板JSON文件中?

  2. 2

    保存到现有的excel文件而无需打开新的excel文件

  3. 3

    如何使用Pandas将新工作表保存在现有的Excel文件中?

  4. 4

    Imacross ..将提取的数据保存在现有的.csv文件中

  5. 5

    将数据结构保存在现有的 .mat 文件中,无需嵌套

  6. 6

    将CSV文件保存到现有文件中,但保存在新工作表中,然后重命名工作表

  7. 7

    从现有的base64编码保存Excel文件

  8. 8

    使用 Swift 在 Firebase 中保存现有的 JSON 文件?

  9. 9

    将乘法结果保存到现有数组

  10. 10

    将文件添加到现有的zipfile

  11. 11

    将目录添加到现有的.zip文件

  12. 12

    将页面添加到现有的pdf文件

  13. 13

    如何将XElement插入现有的xml文件?

  14. 14

    将SQL文件导入现有的DB表(phpmyadmin)

  15. 15

    将数据附加到现有的 json 文件

  16. 16

    将现有项目推送到github删除现有的自述文件

  17. 17

    使用现有的 Excel 文件保存数据并以新名称保存

  18. 18

    视觉工作室。调试。如何在运行期间将变量具有的所有值保存到文件中?

  19. 19

    如何保存/覆盖现有的Excel文件而没有消息

  20. 20

    Inno Setup将文件和文件夹复制到现有的Zip文件

  21. 21

    将所有终端输出保存到文件

  22. 22

    将NSDictionary保存到文件是否有任何限制

  23. 23

    将grep的输出保存到带有颜色的文件中

  24. 24

    将NSDictionary保存到文件是否有任何限制

  25. 25

    将所有终端输出保存到文件

  26. 26

    将BufferedHttpEntity保存到文件

  27. 27

    将结构保存到文件

  28. 28

    将字典保存到文件

  29. 29

    将质数保存到文件

热门标签

归档