Oleg Sh
Oleg Sh

Reputation: 9013

iTextSharp : create one more page copying last page

I have a PDF file with headers, footers etc on every page. I need to extend count of pages, if text is very long (I have variable text). For example:

        if (countOfRecords > 120)
        {
            // add one more page like second page of template (which does not have content, only header/footer
        }

Is it possible?

Upvotes: 0

Views: 353

Answers (1)

Oleg Sh
Oleg Sh

Reputation: 9013

I implemented it the following way: created PDF file with "empty" page (means only with necessary headers/footers) and then add it to main PDF if necessary how many times as necessary:

        PdfReader pdfReader = new PdfReader(model.Input);
        Document document = new Document(pdfReader.GetPageSizeWithRotation(1));

        using (MemoryStream ms = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, ms);
            document.Open();

            PdfContentByte cb = writer.DirectContent;

            PdfImportedPage page = writer.GetImportedPage(pdfReader, 1);
            cb.AddTemplate(page, 0, 0);

            int countOfPages = (int)Math.Ceiling(Convert.ToDecimal(model.ActiveDriverList.Count - countDriversOnFirstPage) / countDriversOnEmptyPage);


            for (int i = 0; i < countOfPages; i++)
            {
                PdfReader readerPage = new PdfReader(model.EmptyPage);
                readerPage.ConsolidateNamedDestinations();

                document.SetPageSize(pdfReader.GetPageSizeWithRotation(1));
                document.NewPage();

                PdfImportedPage importedPage = writer.GetImportedPage(readerPage, 1);
                cb = AddTextDriversNextPage(cb, model.ActiveDriverList, i + 1);
                cb.AddTemplate(importedPage, 0, 0);
            }

            document.Close();
            writer.Close();
            return ms.ToArray();
        }

Upvotes: 1

Related Questions