iTextSharp错误文档在打开之前没有页面 [英] iTextSharp error document has no page before open it

查看:399
本文介绍了iTextSharp错误文档在打开之前没有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想添加一个标题和页脚到我的pdf文件。为此,我习惯了PdfPageEventHelper。



我创建了一个类:

  public class PDFFooter: PdfPageEventHelper 
{
//写在文档的顶部
public override void OnOpenDocument(PdfWriter writer,Document document)
{
base.OnOpenDocument(writer,document);
}

//每页开头写
public override void OnStartPage(PdfWriter writer,Document document)
{
base.OnStartPage(writer ,文件);
EnTeteDePage(document);
}

//在每个页面的末尾写
public override void OnEndPage(PdfWriter writer,Document document)
{
base.OnEndPage(writer ,文件);
PiedDePage(document);
}

//写入文档
public override void OnCloseDocument(PdfWriter writer,Document document)
{
base.OnCloseDocument(writer,文件);
}

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 deréceptionde chantier \\\
+ _param.NOM_SOCIETE + - +
_adresseParam.ADDR_VOIE + - +
_adresseParam。 ADDR_VOIE_BIS +\\\
+
_adresseParam.ADDR_CP + - +
_adresseParam.ADDR_VILLE +\\\
));
float [] wid = new float [] {0.1f,0.9f};
head.SetWidths(wid);
cell1.Horizo​​ntalAlignment = 1;
cell2.Horizo​​ntalAlignment = 1;
head.AddCell(cell1);
head.AddCell(cell2);
document.Add(head);
}
private void PiedDePage(Document document)
{
段落页脚= new Paragraph();

document.Add(Footer);
}
}

我使用一种方法来打开我的文档: p>

 私有异步任务< bool> ouvrirPDF(iTextSharp.text.Rectangle re)
{
str = await f.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
st = str.AsStreamForWrite();
using(myDocument = new Document(re))//Créationdu PDF
{
using(writer = PdfWriter.GetInstance(myDocument,st))
{
PDFFooter脚=新PDFFooter();
writer.PageEvent = foot;
myDocument.Open(); // Ouverture du PDF
cb = writer.DirectContent;
}
}
返回true;
}

但是当我尝试做myDocument.Open()时,我有一个异常该文件没有页面



你能帮我解决吗?

解决方案

您的代码中有两个严重错误:


  1. 如果您阅读文档,您将了解到 OnStartPage()方法中添加内容

  2. 如果您阅读文档,您将了解到文档应该被用作只读对象。如果禁止执行 document.Add()。相反,您应该将页眉和页脚添加到绝对位置(使用作者的直接内容)。

请丢弃代码不要重新开始,直到 后,您已阅读文档。有关示例,请参阅 http://tinyurl.com/itextsharpIIA2C05


I've a problem, i want to add an header and a footer into me pdf document. For that, i used to 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"

Can you help me to resolve it ?

解决方案

There are two serious errors in your code:

  1. If you read the documentation, you will learn that you never ever should add content in the OnStartPage() method.
  2. If you read the documentation, you will learn that the document should be used as a READ-ONLY object. If is forbidden to do 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

这篇关于iTextSharp错误文档在打开之前没有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆