2015-04-23 41 views
1

我想獲取PDF,保留一些頁面,然後將其保存到另一個目標而不會丟失字段結構。使用ItextSharp複製不帶鬆散表單字段結構的PDF

下面的代碼工作完美複印:

string sourceFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
     string sourceFile = Path.Combine(sourceFolder, "POMultiple.pdf"); 

     string fileName = @"C:\Users\MyUser\Desktop\POMultiple.pdf"; 
     byte[] file = System.IO.File.ReadAllBytes(fileName); 

public static void removePagesFromPdf(byte[] 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()) 
      { 
       PdfWriter writer = 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 
        writer.DirectContent.AddTemplate(writer.GetImportedPage(r, page), 0, 0); 

       } 
       //Close our document 
       doc.Close(); 
      } 
     } 
    } 

但是當我打開Acrobat Reader軟件「TestOutput.pdf」文件我所有的字段爲空。

任何幫助?

回答

3

你需要的東西是這樣的:

PdfReader reader = new PdfReader(sourceFile); 
reader.SelectPages(2-4,8-9); 
PdfStamper stp = new PdfStamper(reader, new FileStream(destinationFile, FileMode.Create)); 
stp.Close(); 
reader.Close(); 
+0

得益於它的一個偉大的答案! –

相關問題