2012-03-01 12 views
1

私はPDFにテキストフィールド値を動的に追加するフォームフィルpdfを持っています。これを追加した後、私はpdfの同じページにテーブルを追加する必要があります。 テーブルを追加すると、テーブルのみで新しいpdfが作成されます。他のすべての既存のデータはクリアされます。私は、コードの下に使用しています :既存のフォームフィル用のテーブルを挿入する方法itextsharpを使用してPDFを

private void AddTableToPDF() 
    { 

     Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 100, 100); 
     try 
     { 
      string pdfFilePath = @"D:\Temp\PDF\Inspection Form - Steel Girder.pdf"; 

      PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Append)); 

      doc.Open();//Open Document to write   

      System.IO.MemoryStream mStream = new System.IO.MemoryStream(); 
      PdfWriter writer = PdfWriter.GetInstance(doc, mStream);    

      DataTable dt = GetDataTable(); 
      DataTable dtHeader = new DataTable(); 
      dtHeader = GetHeaderDataTable();    


      if (dtHeader != null) 
      { 
       PdfPTable PdfTable = new PdfPTable(dtHeader.Columns.Count); 
       PdfPCell PdfPCell = null; 

       for (int rows = 0; rows < dtHeader.Rows.Count; rows++) 
       { 
        for (int column = 0; column < dtHeader.Columns.Count; column++) 
        { 
         PdfPCell = new PdfPCell(new Phrase(new Chunk(dtHeader.Rows[rows][column].ToString(), font8))); 
         PdfTable.AddCell(PdfPCell); 
        } 
       }    

       doc.Add(PdfTable); // add pdf table to the document 

      } 

      if (dt != null) 
      {      
       PdfPTable PdfTable = new PdfPTable(dt.Columns.Count); 
       PdfPCell PdfPCell = null; 


       PdfPCell = new PdfPCell(new Phrase(new Chunk("Reference", font8))); 
       PdfTable.AddCell(PdfPCell); 

       PdfPCell = new PdfPCell(new Phrase(new Chunk("Remark", font8))); 
       PdfTable.AddCell(PdfPCell); 

       PdfPCell = new PdfPCell(new Phrase(new Chunk("Description", font8))); 
       PdfTable.AddCell(PdfPCell); 


       for (int rows = 0; rows < dt.Rows.Count; rows++) 
       { 
        for (int column = 0; column < dt.Columns.Count; column++) 
        { 
         PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8))); 
         PdfTable.AddCell(PdfPCell); 
        } 
       } 

       doc.Add(PdfTable); // add pdf table to the document     

      } 


     } 
     catch (DocumentException docEx) 
     { 
      Response.Write(docEx.Message); 

     } 
     catch (IOException ioEx) 
     { 
      Response.Write(ioEx.Message); 

     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.Message); 

     } 
     finally 
     { 

      doc.Close(); 

     } 
    } 
+0

私の答えはこちらhttp://stackoverflow.com/a/9472064/231316 'Document'オブジェクトは新しいPDFを作成するためのもので、既存のものを修正するものではありません。あなたは 'PdfStamper'や' ColumnText'のようなものを使う必要があります。 –

+0

代わりに2つのpdfを新しいpdfにマージし、前のpdfを削除することができます。 –

答えて

1

はあなたのメインPDFm1.pdfと命名されたとします。追加する必要がある新しいTableを含むm2.pdfという名前の新しいPDFを作成するだけです。

protected void BtnMerge_Click(object sender, EventArgs e) 
    { 
     String[] files = @"d:\m1.pdf,d:\m2.pdf".Split(','); 
     MergeFiles(@"d:\MergedNew.pdf", files); 
    } 

    public void MergeFiles(string destinationFile, string[] sourceFiles) 
    { 

     if (System.IO.File.Exists(destinationFile)) 
      System.IO.File.Delete(destinationFile); 

     string[] sSrcFile; 
     sSrcFile = new string[2]; 
     string[] arr = new string[2]; 
     for (int i = 0; i <= sourceFiles.Length - 1; i++) 
     { 
      if (sourceFiles[i] != null) 
      { 
       if (sourceFiles[i].Trim() != "") 
        arr[i] = sourceFiles[i].ToString(); 
      } 
     } 
     if (arr != null) 
     { 
      sSrcFile = new string[2]; 

      for (int ic = 0; ic <= arr.Length - 1; ic++) 
      { 
       sSrcFile[ic] = arr[ic].ToString(); 
      } 
     } 
     try 
     { 
      int f = 0; 
      PdfReader reader = new PdfReader(sSrcFile[f]); 
      int n = reader.NumberOfPages; 
      //Response.Write("There are " + n + " pages in the original file."); 
      Document document = new Document(PageSize.A4); 
      PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create)); 
      document.Open(); 
      PdfContentByte cb = writer.DirectContent; 
      PdfImportedPage page; 
      int rotation; 
      while (f < sSrcFile.Length) 
      { 
       int i = 0; 
       while (i < n) 
       { 
        i++; 
        document.SetPageSize(PageSize.A4); 
        document.NewPage(); 
        page = writer.GetImportedPage(reader, i); 
        rotation = reader.GetPageRotation(i); 
        if (rotation == 90 || rotation == 270) 
        { 
         cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); 
        } 
        else 
        { 
         cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); 
        } 
        //Response.Write("\n Processed page " + i); 
       } 

       f++; 
       if (f < sSrcFile.Length) 
       { 
        reader = new iTextSharp.text.pdf.PdfReader(sSrcFile[f]); 
        //get the numnber of pages 
        n = reader.NumberOfPages; 
        //Response.Write("There are " + n + " pages in the original file."); 
       } 
      } 
      //Response.Write("Success"); 
      document.Close(); 
     } 
     catch (Exception e) 
     { 
      //Response.Write(e.Message); 
     } 
    } 

が、これはあなたのお役に立てば幸い!今だけmergeこれら二つのPDF(m1.pdf & m2.pdf)新しいもの "MergedNerw.pdf'までで)

MergeTwo PDFに私はそれをテストしている、これは立派に動作し、次のコードを使用してください

関連する問題