如何将PDF文档拆分为小文档 [英] How to split PDF document into small ones

查看:170
本文介绍了如何将PDF文档拆分为小文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个文档分割成几个小文档.例如,如果文档有7页,我需要生成7个pdf.

I need to split a document into several small documents. For example, if document has 7 pages I need to generate 7 pdfs.

在iTextSharp中,我使用以下代码,效果很好.但是,在iText 7中,不可能以相同的方式做到这一点.

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.

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()返回数字7,拆分文档的数量也仅为1.

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.

在此链接文档中以某种方式显示了如何拆分文档.但是,我不知道如何对提到的方法进行类似的方法-getNextPdfWriter

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

第二个问题
即使我只有一个文件,它也是空的.我想知道如何设置适当的作者来创建正确的pdf.分别设置读取器以读取拆分文档的内容.

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();
    }
}

问题

如何使用iText 7在.NET Core中正确地将文档拆分为较小的文档

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:

创建PdfSplitter的自定义拆分器替代功能.

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");
        }
    }

然后仅将其用于拆分PDF文档.不要忘记关闭拆分的文档.我想指出一件事. SplitByPageCount-根据需要的数字应该分开. SplitByPageCount(1)将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();
        }
    }

结果是几个pdfs.

这篇关于如何将PDF文档拆分为小文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆