簡體   English   中英

內容較長時,itextsharp不會創建新頁面

[英]itextsharp doesn't make new page when content is longer

我嘗試從3天開始尋找制作一個pdf文檔的方法,我將不勝感激。

我只有很少的表單字段可以訪問和填寫。 我要在此字段下方放置動態創建的表。 該表可能足夠長,不能超過一頁。 這是我的問題。 我無法在同一頁面上的表格字段下方添加此表格。 我找到合並pdf文件的示例。 現在我有這種情況。

  1. 用填寫的表單域制作一個pdf
  2. 用pdfTable制作pdf,該pdfTable總是只有一頁,但是內容更長
  3. 制作從前兩個合並的pdf。 在第一頁中填寫表格字段,在第二頁中是我的pdfTable。

我只希望表單字段和表格從第一頁開始,然后在下一頁繼續。

我發布我的代碼有點混亂,但是...

string pdfTemplate = Server.MapPath("~/PDF/") + "invoiceTest.pdf";
        string newFile = Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf";

        using (FileStream ms = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf", FileMode.Create))
        using (FileStream formFile = new FileStream(Server.MapPath("~/PDF/") + "invoiceTest.pdf", FileMode.Open))
        {
            PdfReader reader = new PdfReader(formFile);
            using (Document document = new Document(reader.GetPageSizeWithRotation(1)))
            {
                PdfStamper outStamper = new PdfStamper(reader, ms);
                PdfContentByte body = outStamper.GetOverContent(reader.NumberOfPages);

                document.Open(); //Open document to work with

                AcroFields fields = outStamper.AcroFields;

                BaseFont bfComic = BaseFont.CreateFont(Server.MapPath("~/PDF/") + "trebuc.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                Font font = new Font(bfComic, 12);

                // UPDATE THE FORM FIELDS
                fields.SetFieldProperty("txtContragentName", "textfont", bfComic, null);
                fields.SetField("txtContragentName", "Фрея");
                fields.SetFieldProperty("txtContragentCode", "textfont", bfComic, null);
                fields.SetField("txtContragentCode", "DGB34TT");
                fields.SetFieldProperty("txtDateCreated", "textfont", bfComic, null);
                fields.SetField("txtDateCreated", "03.06.2013");

                outStamper.Close();
            }
        }

        using (FileStream ms = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 2 + ".pdf", FileMode.Create))
        using (FileStream formFile = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf", FileMode.Open))
        {
            PdfReader reader = new PdfReader(formFile);
            using (Document document = new Document(reader.GetPageSizeWithRotation(1)))
            {
                PdfWriter writer = PdfWriter.GetInstance(document, ms);
                document.Open();
                //Paragraph heading = new Paragraph("Page Heading", new Font(Font.FontFamily.HELVETICA, 10f, Font.BOLD));
                //heading.SpacingAfter = 8f;
                //doc.Add(heading);
                string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse blandit blandit turpis. Nam in lectus ut dolor consectetuer bibendum. Morbi neque ipsum, laoreet id; dignissim et, viverra id, mauris. Nulla mauris elit, consectetuer sit amet, accumsan eget, congue ac, libero. Vivamus suscipit. Nunc dignissim consectetuer lectus. Fusce elit nisi; commodo non, facilisis quis, hendrerit eu, dolor? Suspendisse eleifend nisi ut magna. Phasellus id lectus! Vivamus laoreet enim et dolor. Integer arcu mauris, ultricies vel, porta quis, venenatis at, libero. Donec nibh est, adipiscing et, ullamcorper vitae, placerat at, diam. Integer ac turpis vel ligula rutrum auctor! Morbi egestas erat sit amet diam. Ut ut ipsum? Aliquam non sem. Nulla risus eros, mollis quis, blandit ut; luctus eget, urna. Vestibulum vestibulum dapibus erat. Proin egestas leo a metus?";
                PdfContentByte cb = writer.DirectContent;
                ColumnText columns = new ColumnText(cb);
                //float left, float right, float gutterwidth, int numcolumns
                columns.SetSimpleColumn(40, 20, document.PageSize.Width - 40, document.PageSize.Height - 20);
                //Paragraph para = new Paragraph(text, new Font(Font.FontFamily.HELVETICA, 8f));
                ////para.SpacingAfter = 9f;
                //para.Alignment = Element.ALIGN_JUSTIFIED;
                //for (int i = 0; i < 28; i++)
                //{
                //    columns.AddElement(para);
                //}

                //columns.Go();

                BaseFont bfComic = BaseFont.CreateFont(Server.MapPath("~/PDF/") + "trebuc.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                Font font = new Font(bfComic, 12);
                PdfPTable table = new PdfPTable(10);
                table.HorizontalAlignment = Element.ALIGN_LEFT;
                table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
                table.WidthPercentage = 100;
                PdfPCell cell1 = new PdfPCell(new Phrase("ДАТА", new Font(bfComic, 10f, Font.NORMAL, BaseColor.WHITE))) { HorizontalAlignment = 1, VerticalAlignment = 2 };
                cell1.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#808080"));

                PdfPCell cell2 = new PdfPCell(new Phrase("Header spanning 3 columns", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.WHITE))) { HorizontalAlignment = 1 };
                cell2.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#808080"));

                table.AddCell(cell1);
                table.AddCell(cell2);
                //dump data to be set
                #region dump data
                for (int i = 0; i < 100; i++)
                {
                    table.AddCell("Col 1 Row 1");
                }

                #endregion

                float[] widths = new float[] { 200f, 200f, 200f, 200f, 100f, 100f, 100f, 100f, 100f, 100f };
                table.SetWidths(widths);
                table.CompleteRow(); //Added - table won't add the final row if its cells are incomplete - safe to have it ending a table
                columns.AddElement(table);
                columns.Go();
            }
        }

        using (FileStream ms = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 3 + ".pdf", FileMode.Create))
        using (FileStream stampedfile = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf", FileMode.Open))
        using (FileStream appendfile = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 2 + ".pdf", FileMode.Open))
        {
            PdfReader stampedContentReader = new PdfReader(stampedfile);
            PdfReader appendContentReader = new PdfReader(appendfile);

            using (Document document = new Document(stampedContentReader.GetPageSizeWithRotation(1)))
            {
                PdfCopy pdfCopy = new PdfCopy(document, ms);

                document.Open();

                for (int i = 1; i <= stampedContentReader.NumberOfPages; i++)
                    pdfCopy.AddPage(pdfCopy.GetImportedPage(stampedContentReader, i));

                for (int i = 1; i <= appendContentReader.NumberOfPages; i++)
                    pdfCopy.AddPage(pdfCopy.GetImportedPage(appendContentReader, i));
            }
        }

您的代碼確實是一團糟。 我將復制/粘貼它,但請理解我不是C#開發人員。 我只知道Java,而且從內到外都知道iText(我是它的原始開發人員)。

string pdfTemplate = Server.MapPath("~/PDF/") + "invoiceTest.pdf";
string newFile = Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf";

using (FileStream ms = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf", FileMode.Create))
using (FileStream formFile = new FileStream(Server.MapPath("~/PDF/") + "invoiceTest.pdf", FileMode.Open))
{
    PdfReader reader = new PdfReader(formFile);
    // YOU DON'T NEED A DOCUMENT OBJECT HERE!
    // READ THE DOCUMENTATION!!!
    PdfStamper outStamper = new PdfStamper(reader, ms);
    AcroFields fields = outStamper.AcroFields;
    BaseFont bfComic = BaseFont.CreateFont(Server.MapPath("~/PDF/") + "trebuc.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    // UPDATE THE FORM FIELDS
    fields.SetFieldProperty("txtContragentName", "textfont", bfComic, null);
    fields.SetField("txtContragentName", "Фрея");
    fields.SetFieldProperty("txtContragentCode", "textfont", bfComic, null);
    fields.SetField("txtContragentCode", "DGB34TT");
    fields.SetFieldProperty("txtDateCreated", "textfont", bfComic, null);
    fields.SetField("txtDateCreated", "03.06.2013");
    outStamper.Close();
}

using (FileStream ms = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 2 + ".pdf", FileMode.Create))
using (FileStream formFile = new FileStream(Server.MapPath("~/PDF/") + "invoice" + 1 + ".pdf", FileMode.Open))
{
    PdfReader reader = new PdfReader(formFile);
    // I'm adding extra parameters to change the margins so that they match what you had when you defined your ColumnText object
    using (Document document = new Document(reader.GetPageSizeWithRotation(1), 40, 40, 20, 20))
    {
        PdfWriter writer = PdfWriter.GetInstance(document, ms);
        document.Open();
        // HEADERS ARE ADDED WITH PAGE EVENTS!!!
        // PLEASE READ ABOUT PAGE EVENTS IF YOU NEED PAGE HEADERS
        string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse blandit blandit turpis. Nam in lectus ut dolor consectetuer bibendum. Morbi neque ipsum, laoreet id; dignissim et, viverra id, mauris. Nulla mauris elit, consectetuer sit amet, accumsan eget, congue ac, libero. Vivamus suscipit. Nunc dignissim consectetuer lectus. Fusce elit nisi; commodo non, facilisis quis, hendrerit eu, dolor? Suspendisse eleifend nisi ut magna. Phasellus id lectus! Vivamus laoreet enim et dolor. Integer arcu mauris, ultricies vel, porta quis, venenatis at, libero. Donec nibh est, adipiscing et, ullamcorper vitae, placerat at, diam. Integer ac turpis vel ligula rutrum auctor! Morbi egestas erat sit amet diam. Ut ut ipsum? Aliquam non sem. Nulla risus eros, mollis quis, blandit ut; luctus eget, urna. Vestibulum vestibulum dapibus erat. Proin egestas leo a metus?";
        BaseFont bfComic = BaseFont.CreateFont(Server.MapPath("~/PDF/") + "trebuc.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(bfComic, 12);
        PdfPTable table = new PdfPTable(10);
        // The next line doesn't make sense if the width percentage is 100%
        // table.HorizontalAlignment = Element.ALIGN_LEFT;
        table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
        table.WidthPercentage = 100;
        PdfPCell cell1 = new PdfPCell(new Phrase("ДАТА", new Font(bfComic, 10f, Font.NORMAL, BaseColor.WHITE))) { HorizontalAlignment = 1, VerticalAlignment = 2 };
        cell1.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#808080"));
        PdfPCell cell2 = new PdfPCell(new Phrase("Header spanning 3 columns", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.WHITE))) { HorizontalAlignment = 1 };
        cell2.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#808080"));
        table.AddCell(cell1);
        table.AddCell(cell2);
        //dump data to be set
        #region dump data
        for (int i = 0; i < 100; i++)
        {
            table.AddCell("Col 1 Row 1");
        }
        #endregion
        float[] widths = new float[] { 200f, 200f, 200f, 200f, 100f, 100f, 100f, 100f, 100f, 100f };
        table.SetWidths(widths);
        table.CompleteRow(); //Added - table won't add the final row if its cells are incomplete - safe to have it ending a table
        document.Add(table);
    }
}

您在代碼的第一部分中犯的主要錯誤是引入Document 沒有必要。 請閱讀我的書的第6章 ,並查看C#示例

您在第二部分中犯的主要錯誤是使用ColumnText這意味着您想控制布局,但不知道如何處理該控件。 如果使用ColumnText ,則需要詢問該列是否所有內容都已消耗。 如果不是,則需要自己創建一個新頁面。 這一切都在我的書的第3章中進行了解釋。

第三部分看起來還可以(據我所知C#)。

我寫書是為了使人們從一開始就可以通過編寫正確的代碼來節省時間。 我沒有寫書,所以我不得不將它們復制/粘貼到StackOverflow上;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM