0

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();
3
  • use the debugger and step through the code.. also fix the formatting of the code so that we can read it better.. ItexSharp site also has examples on how to add pages.. Commented Feb 23, 2016 at 2:51
  • Thanks Bruno for the prompt reply. I'm an electrical engineer who likes solving problems using programming during my free time. I decided to write this code after buying and reading your book, with the end goal of automatically generating tables of content for our engineering construction packages, which can have hundreds of drawings/pages. So far "combo" is successfully storing info for each drawing, and my final challenge was to nicely put those info on pdf. I will go read the official documentation. Thanks again. Commented Feb 24, 2016 at 2:06
  • Also, I'm using PdfimportedPage because I wanted to use a "prefabricated" pdf document with our company's logo, but I don't have any issue using document.NewPage(). The code is working well for smaller table of content (where all the info can be fitted on a single page). I will incorporate all your recommended changes. Thank you again. Commented Feb 24, 2016 at 2:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, Bruno; much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.