在页脚添加pdfpTable同时使用iTextSharp的创建PDF [英] Add pdfpTable at footer while creating PDF using iTextsharp

查看:1458
本文介绍了在页脚添加pdfpTable同时使用iTextSharp的创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Asp.net应用程序,我不得不产生 PDF ,所以我这样做是使用 iTextSharp的4.0 这是免费开源的,我指< A HREF =htt​​p://www.mazsoft.com/blog/post/2008/04/30/$c$c-sample-for-using-iTextSharp-PDF-library.aspx相对=nofollow>此设置夏理/页脚上生成的PDF,

In my Asp.net application i have to generate PDF so i did this using Itextsharp 4.0 which is free Open source,I refer this to set hearder/footer on the PDF generated,

但现在我的万阿英,蒋达清是我不能够添加 pdfPtable 里面的页脚,任何建议如何在页脚中插入表格?

but now my probelm is i am not able to add pdfPtable inside footer,any suggestion how to insert table at footer ?

code。在的Page_Load

CreatePDF("Invoice", dtaddproduct, "Prince", "1313", "04/09/2013","4000");

CreatePDF 方式我打电话

 TwoColumnHeaderFooter PageEventHandler = new TwoColumnHeaderFooter();
  writer.PageEvent = PageEventHandler;

document.Open();

自定义的HeaderFooter code(TwoColumnHeaderFooter):

The custom HeaderFooter code (TwoColumnHeaderFooter):

    public override void OnStartPage(PdfWriter writer, Document document)
    {
    base.OnStartPage(writer, document);

    Rectangle pageSize = document.PageSize;

    if (Title != string.Empty)
    {
    cb.BeginText();
    cb.SetFontAndSize(bf, 15);
    cb.SetRGBColorFill(50, 50, 200);
    cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
    cb.ShowText(Title);
    cb.EndText();
    }

    if (HeaderLeft + HeaderRight != string.Empty)
    {
    PdfPTable HeaderTable = new PdfPTable(4);
    HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
    HeaderTable.TotalWidth = pageSize.Width - 80;
    HeaderTable.SetWidthPercentage(new float[] { 45, 45, 45, 45 }, pageSize);

    PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
    HeaderLeftCell.Padding = 5;
    HeaderLeftCell.PaddingBottom = 8;
    HeaderLeftCell.BorderWidthRight = 1;
    HeaderTable.AddCell(HeaderLeftCell);

    PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
    HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
    HeaderRightCell.Padding = 5;
    HeaderRightCell.PaddingBottom = 8;
    HeaderRightCell.BorderWidthLeft = 1;
    HeaderTable.AddCell(HeaderRightCell);

    cb.SetRGBColorFill(0, 0, 0);

    PdfPTable table = new PdfPTable(3);
    table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
    PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
    cell.Colspan = 3;
    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
    table.AddCell(cell);
   string billNumber = HttpContext.Current.Session["billlno"].ToString();
    string billDate = HttpContext.Current.Session["billdate"].ToString();
    string TotalRate = HttpContext.Current.Session["totalhours"].ToString();
    string customerName = HttpContext.Current.Session["customername"].ToString();
    table.AddCell("Date: " + billDate);
    table.AddCell("Bill No: " + billNumber);
    Chunk ck=new Chunk("one\ntwo\nthree\nfour");
     ck.SetBackground(Color.RED);
     Phrase p = new Phrase();
    p.Add(ck);
    PdfPCell cl = new PdfPCell();
    cl.AddElement(p);
    cl.BackgroundColor = new Color(0, 150, 0);
    table.AddCell(cl);
   table.AddCell("Total : "+TotalRate);
    table.AddCell("M/s : "+customerName);
    table.AddCell("Col 2 Row 2");
    table.AddCell("Col 3 Row 2");
    table.AddCell("Col 1 Row 3");
    table.AddCell("Col 2 Row 3");
    table.AddCell("Col 3 Row 3");
    table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent);
   }
    }

    public override void OnEndPage(PdfWriter writer, Document document)
    {
    base.OnEndPage(writer, document);

    int pageN = writer.PageNumber;
    String text = "Page " + pageN + " of ";
    float len = bf.GetWidthPoint(text, 8);
    Rectangle pageSize = document.PageSize;
    cb.SetRGBColorFill(100, 100, 100);

    cb.BeginText();
    cb.SetFontAndSize(bf, 8);
    cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
    cb.ShowText(text);
    cb.EndText();

    cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
    cb.BeginText();
    cb.SetFontAndSize(bf, 8);
    cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
    "Printed On " + PrintTime.ToString(),
    pageSize.GetRight(40),
    pageSize.GetBottom(30), 0);
    cb.EndText();
    }

    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
    base.OnCloseDocument(writer, document);
    template.BeginText();
    template.SetFontAndSize(bf, 8);
    template.SetTextMatrix(0, 0);
    template.ShowText("" + (writer.PageNumber - 1));
    template.EndText();
    }

EDITED code(TwoColumnHeaderFooter):

EDITED CODE (TwoColumnHeaderFooter):

编辑我的code放置的OnEndPage。在这里我得到同样的问题,看看截图在那里你会发现一个链接的更多信息的显示,而不是我也写在code中的表。
帮助PLS

I edit my code placed at OnEndPage. here i am getting same problem, have a look at screenshot there you will find a link More Information is displaying and not the table which i have also written in code. help pls

public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);

        Rectangle pageSize = document.PageSize;
        if (HeaderLeft + HeaderRight != string.Empty)
        {
            PdfPTable HeaderTable = new PdfPTable(2);
            HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
            HeaderTable.TotalWidth = pageSize.Width - 80;
            HeaderTable.SetWidthPercentage(new float[] { 45, 45 }, pageSize);
            PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
            HeaderLeftCell.Padding = 5;
            HeaderLeftCell.PaddingBottom = 8;
            HeaderLeftCell.BorderWidthRight = 0;
            HeaderTable.AddCell(HeaderLeftCell);
            PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
            HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
            HeaderRightCell.Padding = 5;
            HeaderRightCell.PaddingBottom = 8;
            HeaderRightCell.BorderWidthLeft = 0;
            HeaderTable.AddCell(HeaderRightCell);
            cb.SetRGBColorFill(0, 0, 0);
            PdfPTable table = new PdfPTable(3);
            table.TotalWidth = document.PageSize.Width - document.LeftMargin;
            //    table.DefaultCell.Border = PdfPCell.NO_BORDER;
            PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
            cell.Colspan = 3;
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            table.AddCell(cell);
            string billNumber = HttpContext.Current.Session["billlno"].ToString();
            string billDate = HttpContext.Current.Session["billdate"].ToString();
            string TotalRate = HttpContext.Current.Session["totalhours"].ToString();
            string customerName = HttpContext.Current.Session["customername"].ToString();
            string address = "Thane west, Street no=30, Mumbai ";
            Font verdana = FontFactory.GetFont("Verdana", 12, Font.BOLD, new Color(76, 177, 255));
            Font subheadfont = FontFactory.GetFont("Verdana", 9, Font.BOLD, new Color(68, 68, 68));
            Font normaltext = FontFactory.GetFont("Verdana", 9, Font.NORMAL, new Color(68, 68, 68));
            Font whiteFont = FontFactory.GetFont("Verdana", 10, Font.NORMAL, new Color(255, 255, 255));
            Chunk ckInvoice = new Chunk("INVOICE", verdana);
            Chunk ck4 = new Chunk("\n");
            Chunk ckcuslbl = new Chunk("CLIENT :", subheadfont);
            Chunk ckaddlbl = new Chunk("ADDRESS :", subheadfont);
            Chunk ckinvlbl = new Chunk("INVOICE #:", subheadfont);
            Chunk ckdatelbl = new Chunk("DATE :", subheadfont);
            Chunk ckcusname = new Chunk(customerName, normaltext);
            Chunk ckaddr = new Chunk(address, normaltext);
            Chunk ckinvoice = new Chunk(billNumber, normaltext);
            Chunk ckdate = new Chunk(billDate, normaltext);
            float[] widthColumnas = new float[] { 25f, 150f };
            PdfPTable nested = new PdfPTable(2);
            nested.DefaultCell.Border = PdfPCell.NO_BORDER;
            nested.SetWidths(widthColumnas);
            nested.AddCell(new Phrase(ckInvoice));
            nested.AddCell(new Phrase(ck3));
            nested.AddCell(new Phrase(ck3));
            nested.AddCell(new Phrase(ck3));
            nested.AddCell(new Phrase(ckcuslbl));
            nested.AddCell(new Phrase(ckcusname));
            nested.AddCell(new Phrase(ckinvlbl));
            nested.AddCell(new Phrase(ckinvoice));
            nested.AddCell(new Phrase(ckdatelbl));
            nested.AddCell(new Phrase(ckdate));
            nested.AddCell(new Phrase(ckaddlbl));
            nested.AddCell(new Phrase(ckaddr));
            PdfPCell nesthousing = new PdfPCell(nested);
            nesthousing.Padding = 0f;
            nesthousing.BorderWidth = 0;
            nesthousing.Colspan = 2;
            table.AddCell(nesthousing);
             Chunk ck = new Chunk("Honda acresone\nThane(W). Mumbai \nStreet rno.20\n022 5023233", whiteFont);
            Paragraph p = new Paragraph();
            p.Add(ck);
            p.Alignment = Element.ALIGN_RIGHT;
            PdfPCell cl = new PdfPCell();
            cl.AddElement(p);
            cl.BackgroundColor = new Color(76, 177, 255);
            cl.FixedHeight = 100.0f;
            cl.HorizontalAlignment = 0;
            cl.PaddingRight = 35;
            cl.PaddingTop = 10;
            cl.VerticalAlignment = Element.ALIGN_TOP;
            table.AddCell(cl);
            table.AddCell("Total : " + TotalRate);
            table.AddCell("M/s : " + customerName);
            table.AddCell("Col 2 Row 2");
            table.AddCell("Col 3 Row 2");
            table.AddCell("Col 1 Row 3");
            table.AddCell("Col 2 Row 3");
            table.AddCell("Col 3 Row 3");
            table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent);

        }

        int pageN = writer.PageNumber;
        String text = "Page " + pageN + " of ";
        float len = bf.GetWidthPoint(text, 8);

       // Rectangle pageSize = document.PageSize;
        cb.SetRGBColorFill(100, 100, 100);
        //Create our ColumnText bound to the canvas
        var ct = new ColumnText(cb);
        //Set the dimensions of our "box"
        ct.SetSimpleColumn(pageSize.GetRight(200), pageSize.GetBottom(30), pageSize.Right, pageSize.Bottom);
        //Create a new chunk with our text and font
        var c = new Chunk("More Information", new iTextSharp.text.Font(bf, 10));
        //Set the chunk's action to a remote URL
        c.SetAction(new PdfAction("http://www.google.com"));
        //Add the chunk to the ColumnText
        ct.AddElement(c);
        //Tell the ColumnText to draw itself
        ct.Go();

        PdfPTable table2 = new PdfPTable(3);
        table2.TotalWidth = document.PageSize.Width - document.LeftMargin;
        table2.AddCell("cell Value 1");
        table2.AddCell("Cell Value 2 ");
        table2.WriteSelectedRows(0, -1, document.LeftMargin + 200, document.PageSize.Height - 30, writer.DirectContent);

    }

截图:

ScreenShot:

推荐答案

不要的OnStartPage 添加内容,而不是在设置变量,并将它们在<$ C $写C>的OnEndPage 。这是iText的书,但它很难找到超越新闻组认为,这一在线文档。见<一href=\"http://itext-general.2136553.n4.nabble.com/Use-PdfPageEventHelper-onStartPage-tp4229511p4230850.html\"相对=nofollow>这个和<一个href=\"http://itext-general.2136553.n4.nabble.com/Will-it-be-a-problem-if-I-add-content-in-onStartPage-tp2162582p2162583.html\"相对=nofollow>这个

Do not add content in OnStartPage, instead set variables in that and write them in OnEndPage. This is in the iText book but its hard to find online documentation beyond newsgroups that say this. See this and this

修改

@Satinder辛格,这是一个很大code的阅读,如果可以的话,总是试图提炼它下降到code仍然打破了量最小,并删除您的任何特定领域的东西,这样的为会话和全局变量。这么说,我没看过的大部分,但我想我看到您的问题:

@Satinder singh, that's a lot of code to read, if you can, always try to distill it down to the smallest amount of code that still breaks and remove any of your domain-specific stuff such as session and global variables. That said, I didn't read most of it but I think I spotted your problem:

PdfPTable table2 = new PdfPTable(3);
table2.TotalWidth = document.PageSize.Width - document.LeftMargin;
table2.AddCell("cell Value 1");
table2.AddCell("Cell Value 2 ");
table2.WriteSelectedRows(0, -1, document.LeftMargin + 200, document.PageSize.Height - 30, writer.DirectContent);

以上,你要创建一个三列的表格,但你只有两个单元添加到它。 iTextSharp的是有点挑剔其细胞的正确数目所以要么将其更改为一个两列的表格或添加第三个单元格中。

Above, you're creating a three column table but you're only add two cells to it. iTextSharp is a little picky about having the correct number of cells so either change it to a two column table or add a third cell.

这篇关于在页脚添加pdfpTable同时使用iTextSharp的创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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