[英]iTextSharp error document has no page before open it
I've a problem, i want to add an header and a footer into me pdf document. 我有问题,我想在我的pdf文档中添加页眉和页脚。 For that, i used to PdfPageEventHelper.
为此,我习惯了PdfPageEventHelper。
i've created a class : 我创建了一个类:
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);
}
}
i use a method to open my document : 我使用一种方法来打开我的文档:
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;
}
But when i try to do myDocument.Open(), i have an exception "The document has no page" 但是,当我尝试执行myDocument.Open()时,出现异常“文档无页面”
Can you help me to resolve it ? 你能帮我解决吗?
There are two serious errors in your code: 您的代码中有两个严重错误:
OnStartPage()
method. OnStartPage()
方法中添加内容。 document
should be used as a READ-ONLY object. document
应被用作只读对象。 If is forbidden to do document.Add()
. document.Add()
。 Instead you should add your header and footer at absolute positions (using the direct content of the writer). Please throw away your code and don't start anew until after you've read the documentation. 请丢弃您的代码,直到阅读完文档后再重新开始。 For some examples, see http://tinyurl.com/itextsharpIIA2C05
有关某些示例,请参见http://tinyurl.com/itextsharpIIA2C05
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.