2011-05-10 5 views
2

iTextSharp를 사용하여 양식이있는 PDF를 읽고 양식의 필드를 채우고 다시 클라이언트에 씁니다. 나는 모든 페이지가 비어있는 경우 특정 페이지를 제거해야한다는 요구 조건을 갖게되었습니다. (질문의 목적을 위해 부울 변수를 검사하여 페이지를 제거해야하는지 여부를 알 수 있습니다.) iTextSharp에서 실제로 PDF를 복사하고 제거 할 페이지를 생략하고 있습니다.iTextSharp로 PDF 양식 복사하기

나는이 작업을하고 있지만 복사 된 PDF의 양식을 잃어 버리고 있습니다. 즉, 값을 쓰지 않고 있습니다. 일부 페이지를 "제거"하기 위해 PDF를 복사하기 전에 값이 올바르게 쓰여졌습니다.

양식을 복사 할 때 이미 작성한 PDF 양식을 유지하거나 더 나은 제거 방법이 있습니까? 페이지는? 지금까지 내 코드는 파일에 PDF를 씁니다 있지만 양식을 작성하지 않습니다 (아마 bec

string file = "output.pdf"; 
    PdfReader reader = new PdfReader("template.pdf"); 

    Document doc = new Document(); 
    using (MemoryStream ms = new MemoryStream()) 
    { 

     PdfWriter writer = PdfWriter.GetInstance(doc, ms); 
     doc.Open(); 
     doc.AddDocListener(writer); 
     for (int i = 1; i <= reader.NumberOfPages; i++) 
     { 
      bool skipPage = this.SkipPage; // some nifty logic here 
      if (skipPage) 
       continue; 

      doc.SetPageSize(reader.GetPageSize(i)); 
      doc.NewPage(); 
      PdfContentByte cb = writer.DirectContent; 
      PdfImportedPage page = writer.GetImportedPage(reader, i); 
      int rotation = reader.GetPageRotation(i); 
      if (rotation == 90 || rotation == 270) 
       cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height); 
      else 
       cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0); 
     } 
     reader.Close(); 
     doc.Close(); 
     using (FileStream fs = new FileStream(file, FileMode.Create)) 
     { 
      // this is the part stumping me; I need to use a PdfStamper to write 
      // out some values to fields on the form AFTER the pages are removed. 
      // This works, but there doesn't seem to be a form on the copied page... 
      this.stamper = new PdfStamper(new PdfReader(ms.ToArray()), fs); 
      // write out fields here... 
      stamper.FormFlattening = true; 
      stamper.SetFullCompression(); 
      stamper.Close(); 
     } 
    } 

답변

1

신경 끄시 고, 내가 PDF 리더에 SelectPages 방법을 사용하여 그것을 알아낼 수 있었던 것 같다, 나는 실제 사본을 할 필요 피할 수 있습니다 : ause 양식을 복사 할 때) 보존되지 않습니다 파일의.

+0

나에게 이길 ... +1 –

+3

힌트 대신 정확한 코드 샘플을 게시하는 것이 좋습니다. –

+0

예, 적절한 코드 샘플이 좋은 것입니다! – CodeIt

관련 문제