简体   繁体   English

使用iTextsharp将PDF拆分为多个PDF

[英]Split PDF into multiple PDFs using iTextsharp

public int SplitAndSave(string inputPath, string outputPath)
    {
        FileInfo file = new FileInfo(inputPath);
        string name = file.Name.Substring(0, file.Name.LastIndexOf("."));

        using (PdfReader reader = new PdfReader(inputPath))
        {

            for (int pagenumber = 1; pagenumber <= reader.NumberOfPages; pagenumber++)
            {
                string filename = pagenumber.ToString() + ".pdf";

                Document document = new Document();
                PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + filename, FileMode.Create));

                document.Open();

                copy.AddPage(copy.GetImportedPage(reader, pagenumber));

                document.Close();
            }
            return reader.NumberOfPages;
        }

    }

I want to split the Pdf in to multiple PDFs with 50 pages interval.(Suppoose If there are 400 pages PDF, I want 8 pdfs). 我想将Pdf分成多个PDF,间隔为50页。(如果有400页PDF,我想要8个pdf)。 The above code is splitting every page into a pdf. 上面的代码将每个页面拆分为pdf。 Please help me...I'm using asp.net with iTextSharp. 请帮帮我...我正在使用带有iTextSharp的asp.net。

You're looping through the pdf and creating a new document every time you advance a page. 每次推进页面时,您都会遍历pdf并创建新文档。 You'll need to keep track of your pages so that you perform split only every 50 pages. 您需要跟踪您的页面,以便每50页执行一次拆分。 Personally I would put that in a separate method and call it from your loop. 我个人会把它放在一个单独的方法中,并从你的循环中调用它。 Something like this: 像这样的东西:

private void ExtractPages(string sourcePDFpath, string outputPDFpath, int startpage,  int endpage)
{
    PdfReader reader = null;
    Document sourceDocument = null;
    PdfCopy pdfCopyProvider = null;
    PdfImportedPage importedPage = null;

    reader = new PdfReader(sourcePDFpath);
    sourceDocument = new Document(reader.GetPageSizeWithRotation(startpage));
    pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPDFpath, System.IO.FileMode.Create));

    sourceDocument.Open();

    for (int i = startpage; i <= endpage; i++)
    {
        importedPage = pdfCopyProvider.GetImportedPage(reader, i);
        pdfCopyProvider.AddPage(importedPage);
    }
    sourceDocument.Close();
    reader.Close();
}

So in your original code loop through your pdf and every 50 pages call the above method. 因此,在原始代码循环中通过pdf,每50页调用上面的方法。 You'll just need to add variables in your block to keep track of the start/end pages. 您只需在块中添加变量即可跟踪开始/结束页面。

Here is a shorter solution. 这是一个较短的解决方案。 Haven't tested which method has the better performance. 没有测试哪种方法具有更好的性能。

private void ExtractPages(string sourcePDFpath, string outputPDFpath, int startpage, int endpage)
{
  var pdfReader = new PdfReader(sourcePDFpath);
  try
  {
    pdfReader.SelectPages($"{startpage}-{endpage}");
    using (var fs = new FileStream(outputPDFpath, FileMode.Create, FileAccess.Write))
    {
      PdfStamper stamper = null;
      try
      {
        stamper = new PdfStamper(pdfReader, fs);
      }
      finally
      {
        stamper?.Close();
      }
    }
  }
  finally
  {
    pdfReader.Close();
  }
}

I faced the same problem but wanted to use iText7 for .NET. 我遇到了同样的问题,但想用iText7 for .NET。 In this concrete case, this code worked for me: 在这个具体案例中,这段代码对我有用:

1st: Implement own PdfSplitter 第一:实现自己的PdfSplitter

 public class MyPdfSplitter : PdfSplitter
 {
    private readonly string _destFolder;
    private int _pageNumber;
    public MyPdfSplitter(PdfDocument pdfDocument, string destFolder) : base(pdfDocument)
    {
        _destFolder = destFolder;
    }

    protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
    {
        _pageNumber++;
        return new PdfWriter(Path.Combine(_destFolder, $"p{_pageNumber}.pdf"));
    }
}

2nd: Use it to split your PDF 第二:用它来分割你的PDF

using (var pdfDoc = new PdfDocument(new PdfReader(filePath)))
{
    var splitDocuments = new MyPdfSplitter(pdfDoc, targetFolder).SplitByPageCount(1);
    foreach (var splitDocument in splitDocuments)
    {
        splitDocument.Close();
    }
 }

Code migrated from Java example: https://itextpdf.com/en/resources/examples/itext-7/splitting-pdf-file 从Java示例迁移的代码: https//itextpdf.com/en/resources/examples/itext-7/splitting-pdf-file

Hope this helps to others! 希望这对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM