web-dev-qa-db-ja.com

iTextsharpを使用してPDFを複数のPDFに分割

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

    }

50ページ間隔でPDFを複数のPDFに分割したい(400ページのPDFがある場合、8つのPDFが必要)。上記のコードは、すべてのページをPDFに分割しています。私を助けてください...私はiTextSharpでasp.netを使用しています。

15
Billy

あなたはページを進めるたびに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ページごとに上記のメソッドを呼び出します。開始ページと終了ページを追跡するために、ブロックに変数を追加する必要があるだけです。

13
ovaltein

これは役に立ちます。非常にあなたの要件に一致します

http://www.codeproject.com/Articles/559380/SplittingplusandplusMergingplusPdfplusFilesplusinp

4
RohitWagh

ここに短い解決策があります。どちらの方法の方がパフォーマンスが良いかテストしていません。

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

同じ問題に直面しましたが、iText7 for .NETを使用したいと思いました。この具体的なケースでは、このコードは私にとってうまくいきました:

1番目:独自の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"));
    }
}

2番目: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

これが他の人に役立つことを願っています!

0