使用iTextsharp将PDF拆分为多个PDF

时间:2013-09-12 10:40:29

标签: c# asp.net pdf split 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;
        }

    }

我想将Pdf分成多个PDF,间隔为50页。(假设有400页PDF,我想要8个pdf)。上面的代码将每个页面拆分为pdf。请帮帮我...我正在使用带有iTextSharp的asp.net。

4 个答案:

答案 0 :(得分:12)

每次推进页面时,您都会遍历pdf并创建新文档。您需要跟踪您的页面,以便每50页执行一次拆分。我个人会把它放在一个单独的方法中,并从你的循环中调用它。像这样:

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

因此,在您的原始代码循环中通过您的pdf,每50页调用上述方法。您只需在块中添加变量即可跟踪开始/结束页面。

答案 1 :(得分:4)

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我遇到了同样的问题,但想将iText7用于.NET。 在这种具体情况下,此代码对我有用:

第一个:实现自己的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"));
    }
}

第二:用它来分割PDF

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

从Java示例:https://itextpdf.com/en/resources/examples/itext-7/splitting-pdf-file

迁移的代码

希望这对其他人有帮助!