2014-09-03 129 views
0

我有一個問題,我想添加一個頁眉和頁腳到我的PDF文檔。爲此,我用PdfPageEventHelper。iTextSharp錯誤文檔打開之前沒有頁面

我創建了一個類:

public class PDFFooter : PdfPageEventHelper 
    { 
     // write on top of document 
     public override void OnOpenDocument(PdfWriter writer, Document document) 
     { 
      base.OnOpenDocument(writer, document); 
     } 

     // write on start of each page 
     public override void OnStartPage(PdfWriter writer, Document document) 
     { 
      base.OnStartPage(writer, document); 
      EnTeteDePage(document); 
     } 

     // write on end of each page 
     public override void OnEndPage(PdfWriter writer, Document document) 
     { 
      base.OnEndPage(writer, document); 
      PiedDePage(document); 
     } 

     //write on close of document 
     public override void OnCloseDocument(PdfWriter writer, Document document) 
     { 
      base.OnCloseDocument(writer, document); 
     } 

     private void EnTeteDePage(Document document) 
     { 
      Controleur.ControleurParametre CP = new ControleurParametre(); 
      T_PARAMETRE _param = CP.Charger(); 
      T_ADRESSE _adresseParam = CP.ChargerAdresse(_param.ID_ADRESSE_SOCIETE); 
      iTextSharp.text.Image img = (_param.PATH_LOGO_SOCIETE != "" && _param.PATH_LOGO_SOCIETE != null) ? iTextSharp.text.Image.GetInstance(_param.PATH_LOGO_SOCIETE) : null; 
      if (img != null) 
      { 
       if (img.Width/150 > img.Height/150) img.ScalePercent((img.Width/150) * 100); 
       else img.ScalePercent((img.Height/150) * 100); 
       document.Add(img); 
      } 
      PdfPTable head = new PdfPTable(6); 
      PdfPCell cell1 = new PdfPCell(img); 
      PdfPCell cell2 = new PdfPCell(new Phrase("Rapport de réception de chantier \n" + _param.NOM_SOCIETE + " - " + 
       _adresseParam.ADDR_VOIE + " - " + 
       _adresseParam.ADDR_VOIE_BIS + "\n" + 
       _adresseParam.ADDR_CP + " - " + 
       _adresseParam.ADDR_VILLE + "\n")); 
      float[] wid = new float[] { 0.1f, 0.9f }; 
      head.SetWidths(wid); 
      cell1.HorizontalAlignment = 1; 
      cell2.HorizontalAlignment = 1; 
      head.AddCell(cell1); 
      head.AddCell(cell2); 
      document.Add(head); 
     } 
     private void PiedDePage(Document document) 
     { 
      Paragraph Footer = new Paragraph(); 

      document.Add(Footer); 
     } 
    } 

我使用的方法來打開我的文檔:

private async Task<bool> ouvrirPDF(iTextSharp.text.Rectangle re) 
    { 
     str = await f.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); 
     st = str.AsStreamForWrite(); 
     using (myDocument = new Document(re)) //Création du PDF 
     { 
      using (writer = PdfWriter.GetInstance(myDocument, st)) 
      { 
       PDFFooter foot = new PDFFooter(); 
       writer.PageEvent = foot; 
       myDocument.Open(); //Ouverture du PDF 
       cb = writer.DirectContent; 
      } 
     } 
     return true; 
    } 

但是,當我嘗試做myDocument.Open(),我有一個例外「文檔沒有頁面」

你能幫我解決嗎?

+0

*當我嘗試執行myDocument.Open(),我有一個異常「文檔沒有頁面」* - 我認爲在打開期間還有一些其他錯誤觸發'使用(writer = PdfWriter.GetInstance(myDocument,st))'試圖關閉文檔,並且因爲確實沒有頁面而失敗。在這種情況下,您最好嘗試捕獲發生故障的線路上的異常並檢查該異常。開業期間的實際問題可能確實是由於布魯諾指出的問題。 – mkl 2014-09-03 11:46:51

回答

0

有你的代碼中有兩處嚴重的錯誤:

  1. 如果你閱讀文檔,你會知道你永遠應在OnStartPage()方法添加內容。
  2. 如果您閱讀文檔,您將瞭解到document應該用作只讀對象。如果是禁止document.Add()。相反,您應該將您的頁眉和頁腳添加到絕對位置(使用作者的直接內容)。

請丟掉您的密碼,直到您已閱讀文檔。對於一些示例,請參閱http://tinyurl.com/itextsharpIIA2C05

+0

我用iTextSharp來打開和修改文檔,它的工作原理爲什麼我不能用它來創建頁眉和頁腳? – Moussawi 2014-09-03 10:14:12

+0

這是一個很奇怪的問題。這聽起來像「爲什麼我不能用叉子吃湯?」或者「爲什麼我不能在收音機上看電視?」當你想打開一個*現有的文檔*並*在它上面打上一個頁眉和一個頁腳時*,你可以使用'PdfReader'和'PdfStamper',** NOT **'PdfWriter'和** NOT **'PdfPageEvents'。您是否閱讀過文檔? http://manning.com/lowagie2/samplechapter6.pdf – 2014-09-03 10:17:41

+0

我不使用現有文檔。我創建了一個新文檔pdf,並使用PdfWriter將數據放入其中 – Moussawi 2014-09-03 10:25:12

相關問題