2017-08-29 70 views
2

我確實在這裏的代碼,我不斷收到錯誤消息:文檔沒有頁面。我還設置了表格寬度和短語,但仍然出現錯誤消息。我現在完全不在了,但我試過尋找其他一些案例,但他們試圖修正它們的設置表格寬度。有什麼我想念的嗎?任何幫助,將不勝感激。謝謝!C# - iTextSharp該文檔沒有頁面

private void printPDF(object sender, EventArgs e) 
    {    

      Document docu = new Document(PageSize.LETTER); 
      PdfWriter writer = PdfWriter.GetInstance(docu, new FileStream("C:\\Report\\" + empno + ".pdf", FileMode.Create)); 
      Phrase phrase = null; 
      PdfPCell cell = null; 
      PdfPTable table = null; 
      BaseColor color = null; 

      docu.Open(); 

      //Header Table 
      table = new PdfPTable(2); 
      table.TotalWidth = 500f; 
      table.LockedWidth = true; 
      table.SetWidths(new float[] { 0.3f, 0.7f }); 

      //Company Name and Address 
      phrase = new Phrase(); 
      phrase.Add(new Chunk("Company Name\n\n", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE))); 
      phrase.Add(new Chunk("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK))); 
      cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
      cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
      table.AddCell(cell); 

      docu.Add(table); 
      docu.Close(); 
    } 
private static PdfPCell PhraseCell(Phrase phrase, int align) 
    { 
     PdfPCell cell = new PdfPCell(phrase); 
     cell.BorderColor = BaseColor.WHITE; 
     cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
     cell.HorizontalAlignment = align; 
     cell.PaddingBottom = 2f; 
     cell.PaddingTop = 0f; 
     return cell; 
    } 
+0

是否有您的printPdf()方法的詳細代碼?除了添加表格並關閉它之外,您似乎沒有對文檔「docu」進行任何操作。是否需要返回給調用者? – njenson

回答

2

我有一個快速瀏覽一下,我想說的是,你指定table = new PdfPTable(2);告訴表將有每行2細胞,但是你只在第一行中提供一個單元。由於您只提供了一個單元格,因此表格定義並未完成,因此它沒有任何要呈現的行。

現在,如果您更改您的代碼並提供2個單元格。

phrase = new Phrase(); 
phrase.Add(new Chunk("Company Name\n\n", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE))); 
phrase.Add(new Chunk("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK))); 
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
table.AddCell(cell); 

//yes this is just duplicating content but proves the point 
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
table.AddCell(cell); 


docu.Add(table); 
docu.Close(); 

這工作得很好,因爲我們現在有兩個單元並可以完成表定義。如果你嘗試添加3個單元格,你會發現第三個單元格不會被渲染,因爲它沒有完成表格定義。

當您將單元格添加到表格中時,您將它們水平添加到表格中,表格將根據列定義將其呈現爲行。即在2列的表細胞變得

cell 1 | Row 1 Cell 1 
cell 2 | Row 1 Cell 2 
cell 3 | Row 2 Cell 1 
cell 4 | Row 2 Cell 2 

現在這種說法

table = new PdfPTable(2); 
table.TotalWidth = 500f; 
table.LockedWidth = true; 
table.SetWidths(new float[] { 0.3f, 0.7f }); 

等同於:

table = new PdfPTable(new float[] { 0.3f, 0.7f }); 
table.TotalWidth = 500f; 
table.LockedWidth = true; 

當你在構造函數中2列寬度已指定,這將給表2列說明。

作爲一個完整的例子看下面的代碼:

private void printPDF(object sender, EventArgs e) 
{ 
    string path = "D:\\Test\\" + Guid.NewGuid().ToString() + ".pdf"; 
    using (var fileStream = new FileStream(path, FileMode.Create)) 
    { 
     Document docu = new Document(PageSize.LETTER); 
     PdfWriter writer = PdfWriter.GetInstance(docu, fileStream); 

     docu.Open(); 

     //Header Table 
     var table = new PdfPTable(new float[] { 0.3f, 0.7f }); 
     table.TotalWidth = 500f; 

     //company name 
     var phrase = new Phrase("Company Name", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE)); 
     var cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
     table.AddCell(cell); 

     phrase = new Phrase("My test company", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE)); 
     cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
     table.AddCell(cell); 

     //company address 
     phrase = new Phrase("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)); 
     cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
     table.AddCell(cell); 

     phrase = new Phrase("123 Main Street, Your City.", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)); 
     cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
     table.AddCell(cell); 

     docu.Add(table); 
     docu.Close(); 
     writer.Close(); 
    } 
} 
private static PdfPCell PhraseCell(Phrase phrase, int align) 
{ 
    PdfPCell cell = new PdfPCell(phrase); 
    cell.BorderColor = BaseColor.WHITE; 
    cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
    cell.HorizontalAlignment = align; 
    cell.PaddingBottom = 2f; 
    cell.PaddingTop = 0f; 
    return cell; 
} 
+0

謝謝!我只是試了一下,它按預期工作。沒有弄清楚每行的單元格。 –