FLK
FLK

Reputation: 13

Creating a multiple pages pdf with iTextSharp

I'm trying the create a multiple-page pdf using iTextSharp, but I'm having some issue creating a loop to have more than a single page.

My code below is working well for one page; however, my content will not fit into a single page. Thanks.

How can I write a loop to write contents on the first pdf page and the remaining to the second, third, etc...So far, I'm only seeing one page. Thank you.

        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();

Upvotes: 0

Views: 5732

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

Please go to the official documentation and click Q&A to go to the most frequently asked questions. Pick the Getting started category. The first thing you'll see, is the most popular iText example (which I am porting to C# for your convenience):

// 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();

In your code, you are adding text at absolute positions by introducing PDF syntax, such as BeginText() (BT) / EndText() (ET) / SetFontAndSize() (Tf). This is creating PDF the hard way. If is very easy to make mistakes, as shown in this question: What is causing syntax errors in a page created with iText?

If you don't know the PDF reference (ISO 32000-1) by heart, you should avoid code like this. Instead you can use objects such as Paragraph, List, PdfPTable... The beauty of adding these objects to a Document using the Add() method, is that a new page gets triggered automagically as soon as a page is full. You can also trigger a new page yourself using:

document.NewPage();

If you want to add text at absolute positions, iText offers convenience methods and objects such as ColumnText. See my answer to Adding footer to existing PDF

You can define a Rectangle and add objects such as Paragraph, List, PdfPTable... Take a look at the answer to the question How to continue an ordered list on a second page? for inspiration:

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();
}

The while loop is the loop you are looking for.

Upvotes: 1

Related Questions