2010-04-29 58 views
2

我正在使用iTextSharp創建我的PDF文檔。 一切工作正常,我在代碼中沒有錯誤;然而,當我打開創建PDF時,它給了我錯誤,說該文檔將不能正確顯示,因爲它包含錯誤。iTextSharp一切正常,但打開PDF文檔時出現奇怪的錯誤

這裏是波紋管,該代碼給了我一個問題:

public class pdfevents : PdfPageEventHelper 
{ 
    public override void OnEndPage(PdfWriter writer, Document document) 
    { 
     base.OnEndPage(writer, document); 
     PdfContentByte cb = writer.DirectContent; 
     cb.BeginText(); 

     cb.SetTextMatrix(20, document.GetBottom(-30)); 
     BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 
     cb.SetFontAndSize(bf, 10); 

     //thats is the piece of code that makes problems 
     //if i remove it then document displays without error 
     cb.MoveTo(15F, document.GetBottom(-15)); 
     cb.SetLineWidth(0.5F); 
     cb.LineTo(document.GetRight(0), document.GetBottom(-15)); 
     cb.Stroke(); 

     cb.ShowText(DateTime.Now.ToLongDateString()); 

     int n = writer.PageNumber; 
     cb.SetTextMatrix(document.GetRight(20), document.GetBottom(-30)); 
     cb.ShowText(" - " + n + " - "); 

     cb.EndText(); 
    } 
} 

如果我刪除下面幾行:

//thats is the piece of code that makes problems 
      //if i remove it then document displays without error 
      cb.MoveTo(15F, document.GetBottom(-15)); 
      cb.SetLineWidth(0.5F); 
      cb.LineTo(document.GetRight(0), document.GetBottom(-15)); 

話,我沒有得到任何錯誤打開生成的PDF,否則我可以打開PDF並查看文檔和它的內容,包括該行。但是,然後我得到錯誤,該文檔已生成錯誤。

有人可以告訴我什麼是錯的?

在此先感謝。 cb.Stroke();

回答

0

經過一段時間(長時間)之後,我發現了一個解決方法,現在文檔不僅顯示內容(如之前顯示的那樣),但不會顯示錯誤消息。

我仍然不明白爲什麼以及這個解決方案如何工作,以前的解決方案沒有,但以防萬一有人會需要它或將有類似的問題。

取而代之的是:

PdfContentByte cb = writer.DirectContent; 
     cb.BeginText(); 

     cb.SetTextMatrix(20, document.GetBottom(-30)); 
     BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 
     cb.SetFontAndSize(bf, 10); 

     //thats is the piece of code that makes problems 
     //if i remove it then document displays without error 
     cb.MoveTo(15F, document.GetBottom(-15)); 
     cb.SetLineWidth(0.5F); 
     cb.LineTo(document.GetRight(0), document.GetBottom(-15)); 
     cb.Stroke(); 

我改變了代碼的地方與字體代碼繪製的線條。

base.OnEndPage(writer, document); 
     PdfContentByte cb = writer.DirectContent; 


     ////making a line 
     cb.MoveTo(15F, document.GetBottom(-15)); 
     cb.SetLineWidth(0.5F); 
     cb.LineTo(document.GetRight(-10), document.GetBottom(-15)); 
     cb.Stroke(); 

cb.BeginText(); 

      cb.SetTextMatrix(20, document.GetBottom(-30)); 
      BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 
      cb.SetFontAndSize(bf, 10); 
相關問題