使用 iTextSharp 创建多页 PDF

本文关键字:PDF 创建 iTextSharp 使用 | 更新日期: 2023-09-27 18:35:42

我正在尝试使用 iTextSharp 创建多页 pdf,但我在创建循环以包含多个页面时遇到了一些问题。

下面的代码适用于一个页面;但是,我的内容不适合单个页面。谢谢。

我怎样才能写一个循环来写第一个pdf页面上的内容,其余的写到第二、第三页等......到目前为止,我只看到一页。谢谢。

        int height = 600;
        int totalPage = 1;
        int oldPage = 1;
        bool cons = false;
        bool st = false;
        foreach (string al in combo)
foreach (string al in combo) //my loop to write on the pdf but "combo" has 100 lines, which would fit into a single page.
{
                string[] word = al.Split(',');
                int strIndex = combo.IndexOf(al);

                if (al.Length != 0)
                {
                    if (word[0].ToString().ToUpper().Contains("(REV")==true && cons == false)
                    {
                        cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 11);
                        cb.BeginText();
                        // put the alignment and coordinates here
                        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "CONSTRUCTION PRINTS", 80, height, 0);
                        cb.EndText();
                        height = height - 20;
                        cons = true;
                    }
                    if (word[0].ToString().ToUpper().Contains("(REV")==false && st == false)
                    {
                        cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 11);
                        cb.BeginText();
                        // put the alignment and coordinates here
                        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "SAG & TENSION CHARTS", 80, height, 0);
                        cb.EndText();
                        height = height - 20;
                        st = true;
                    }
                    cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 11);
                    totalPage = totalPage + oldPage;
                    // write the text in the pdf content
                    cb.BeginText();
                    // put the alignment and coordinates here
                    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, word[0].ToString().ToUpper(), 80, height, 0);
                    cb.EndText();
                    // write the text in the pdf content
                    cb.BeginText();
                    // put the alignment and coordinates here
                    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "......................................................................................", 335, height, 0);
                    cb.EndText();
                    // write the text in the pdf content
                    cb.BeginText();
                    // put the alignment and coordinates here
                    cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, totalPage.ToString(), 500, height, 0);
                    cb.EndText();
                    oldPage = Convert.ToInt32(word[1]);
                }
                height = height - 20;
            }
            //// create the new page and add it to the pdf
            //  reader = new PdfReader(oldFile);//old file
            PdfImportedPage page = writer.GetImportedPage(reader, 1);
            cb.AddTemplate(page, 0, 0);
            //// close the streams and voilá the file should be changed :)
            document.Close();
            reader.Close();
            fs.Close();
            writer.Close();

使用 iTextSharp 创建多页 PDF

请转到官方文档并单击问答以转到最常见的问题。选择"入门"类别。您将看到的第一件事是最流行的iText示例(为了方便起见,我将其移植到C#):

// step 1
Document document = new Document();
// step 2
FileStream fs = new FileStream("hello.pdf", FileMode.Create);
PdfWriter.GetInstance(document, fs);
// step 3
document.Open();
// step 4
document.Add(new Paragraph("Hello World!"));
// step 5
document.Close();

在您的代码中,通过引入 PDF 语法在绝对位置添加文本,例如 BeginText()BT )/EndText()ET )/SetFontAndSize()Tf )。这是创建PDF的艰难方式。如果很容易出错,如以下问题所示:是什么导致了使用 iText 创建的页面中的语法错误?

如果您不了解PDF参考(ISO 32000-1),则应避免使用此类代码。相反,您可以使用诸如 ParagraphListPdfPTable ...使用 Add() 方法将这些对象添加到Document的美妙之处在于,一旦页面已满,就会自动触发新页面。您也可以使用以下方法自行触发新页面:

document.NewPage();

如果要在绝对位置添加文本,iText 提供了方便的方法和对象,例如 ColumnText 。请参阅我对将页脚添加到现有 PDF 的回答

您可以定义Rectangle并添加对象,例如 ParagraphListPdfPTable ...看看问题的答案 如何在第二页上继续排序列表?灵感来源:

ColumnText ct = new ColumnText(cb);
ct.AddElement(list);
Rectangle rect = new Rectangle(36, 36, 559, 806);
ct.SetSimpleColumn(rect);
int status = ct.Go();
while (ColumnText.HasMoreText(status)) {
    document.NewPage();
    ct.SetSimpleColumn(rect);
    ct.Go();
}

while循环是您要查找的循环。