2011-08-30 33 views
6

私はプログラムフィールドにC#で記入するフォームフィールドを持つpdfドキュメントを持っています。 3つの条件に応じて、その文書からいくつかのページをトリミング(削除)する必要があります。itextsharpトリミングpdfドキュメントのページ

これは可能ですか?条件1のため

:私はページ1-4を保つが、条件2のためのページ5と6

を削除する必要があります。私は、ページ1-4を保つが、5を削除し、条件のために6

を維持する必要があります3:1〜5ページを保持する必要がありますが、6を削除する必要があります

答えて

5

ドキュメント内のページを削除するのではなく、新しいドキュメントを作成し、保持したいページだけをインポートします。以下は、iTextSharp 5.1.1.0をターゲットにした完全なWinFormsアプリケーションです。関数removePagesFromPdfの最後のパラメータは、保持するページの配列です。

以下のコードは物理ファイルで動作しますが、ストリームに基づいたものに変換するのは非常に簡単なので、したくない場合はディスクに書き込む必要はありません。

using System; 
using System.ComponentModel; 
using System.IO; 
using System.Linq; 
using System.Windows.Forms; 
using iTextSharp.text.pdf; 
using iTextSharp.text; 


namespace Full_Profile1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      //The files that we are working with 
      string sourceFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
      string sourceFile = Path.Combine(sourceFolder, "Test.pdf"); 
      string destFile = Path.Combine(sourceFolder, "TestOutput.pdf"); 

      //Remove all pages except 1,2,3,4 and 6 
      removePagesFromPdf(sourceFile, destFile, 1, 2, 3, 4, 6); 
      this.Close(); 
     } 
     public void removePagesFromPdf(String sourceFile, String destinationFile, params int[] pagesToKeep) 
     { 
      //Used to pull individual pages from our source 
      PdfReader r = new PdfReader(sourceFile); 
      //Create our destination file 
      using (FileStream fs = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.None)) 
      { 
       using (Document doc = new Document()) 
       { 
        using (PdfWriter w = PdfWriter.GetInstance(doc, fs)) 
        { 
         //Open the desitination for writing 
         doc.Open(); 
         //Loop through each page that we want to keep 
         foreach (int page in pagesToKeep) 
         { 
          //Add a new blank page to destination document 
          doc.NewPage(); 
          //Extract the given page from our reader and add it directly to the destination PDF 
          w.DirectContent.AddTemplate(w.GetImportedPage(r, page), 0, 0); 
         } 
         //Close our document 
         doc.Close(); 
        } 
       } 
      } 
     } 
    } 
} 
2

ここには、既存のPDFの最後のページ以外のすべてをコピーするためのコードがあります。すべてがメモリストリームにあります。変数pdfByteArrayは、ms.ToArray()を使用して取得した元のpdfのバイト[]です。 pdfByteArrayは新しいPDFで上書きされます。

 PdfReader originalPDFReader = new PdfReader(pdfByteArray); 

     using (MemoryStream msCopy = new MemoryStream()) 
     { 
      using (Document docCopy = new Document()) 
      { 
       using (PdfCopy copy = new PdfCopy(docCopy, msCopy)) 
       { 
       docCopy.Open(); 
       for (int pageNum = 1; pageNum <= originalPDFReader.NumberOfPages - 1; pageNum ++) 
       { 
        copy.AddPage(copy.GetImportedPage(originalPDFReader, pageNum)); 
       } 
       docCopy.Close(); 
       } 
      } 

      pdfByteArray = msCopy.ToArray(); 
14

PdfReader.SelectPages()をPdfStamperと組み合わせて使用​​します。以下のコードはiTextSharp 5.5.1を使用しています。

public void SelectPages(string inputPdf, string pageSelection, string outputPdf) 
{ 
    using (PdfReader reader = new PdfReader(inputPdf)) 
    { 
     reader.SelectPages(pageSelection); 

     using (PdfStamper stamper = new PdfStamper(reader, File.Create(outputPdf))) 
     { 
      stamper.Close(); 
     } 
    } 
} 

この方法を、条件ごとに正しいページ選択で呼び出します。

条件1:

SelectPages(inputPdf, "1-4", outputPdf); 

条件2:

SelectPages(inputPdf, "1-4,6", outputPdf); 

又は

SelectPages(inputPdf, "1-6,!5", outputPdf); 

条件3:

ここでは、iTextSharpのソースコードから、どのようなページを選択するかについてのコメントがあります。これは、ページ選択を処理するために使用されるSequenceListクラスにあります。

/** 
* This class expands a string into a list of numbers. The main use is to select a 
* range of pages. 
* <p> 
* The general systax is:<br> 
* [!][o][odd][e][even]start-end 
* <p> 
* You can have multiple ranges separated by commas ','. The '!' modifier removes the 
* range from what is already selected. The range changes are incremental, that is, 
* numbers are added or deleted as the range appears. The start or the end, but not both, can be ommited. 
*/ 
関連する問題