Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:38:18+00:00 2026-06-07T03:38:18+00:00

I am trying to create pdf with multiple pages using iTextSharp Document document =

  • 0

I am trying to create pdf with multiple pages using iTextSharp

Document document = new Document(PageSize.A4, 2, 2, 10, 10);
private PdfContentByte _pcb;

try
{
    PdfWriter writer = PdfWriter.GetInstance(document, output);
    document.Open();             
    document.NewPage();
    _pcb = writer.DirectContent;
    _pcb.BeginText();
    _pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
    _pcb.EndText();
    writer.Flush();
}
catch(e)
{

}
finally
{
    document.Close();
}

This is working fine for me. When I am trying to add a new page on the same document, it is replacing the existing written text with new page and no new page is getting added. Below is the code which is not working.

_pcb.EndText();
writer.Flush();
document.NewPage();
_pcb = writer.DirectContent;
_pcb.BeginText();
_pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
_pcb.EndText();
writer.Flush();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:38:27+00:00Added an answer on June 7, 2026 at 3:38 am

    Below is my attempt to clean-up and unify your code. Generally avoid try-catch until you actually have to, you’ll often miss some very important errors. (For instance, you’re not actually setting the font and size which is required but maybe you just omitted that code.) Also, unless you are writing a very large PDF there’s really no reason to flush the buffers, leave that to the OS to do for you when necessary.

    When I run the code below I get two pages with text on both pages, does it work for you? (Targeting iTextSharp 5.2.0.0)

            var output = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
            var bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document doc = new Document(PageSize.A4, 2, 2, 10, 10)) {
                    PdfContentByte _pcb;
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                        //Open document for writing
                        doc.Open();
                        //Insert page
                        doc.NewPage();
                        //Alias to DirectContent
                        _pcb = writer.DirectContent;
                        //Set the font and size
                        _pcb.SetFontAndSize(bf, 12);
                        //Show some text
                        _pcb.BeginText();
                        _pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Page 1", 40, 600, 0);
                        _pcb.EndText();
                        //Insert a new page
                        doc.NewPage();
                        //Re-set font and size
                        _pcb.SetFontAndSize(bf, 12);
                        //Show more text on page 2
                        _pcb.BeginText();
                        _pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Page 2", 100, 400, 0);
                        _pcb.EndText();
                        doc.Close();
                    }
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a pdf document in c# using iTextSharp 5.0.6. I
I am trying to create Landscape PDF using iTextSharp but It is still showing
I'm trying to create a pdf report using iTextSharp and I'm stumped as to
I am trying to create a PDF using iTextSharp library (version 4.1.2.0). At the
I'm trying to dynamically create a PDF using the TCPDF library in a document
I am trying to generate a PDF using iTextSharp. It will consists of a
I am trying to create a PDF document in the background via Resque background
I'm trying to create Pdf from java using iText. How Can I Add two
I am trying to create a PDF file using struts 2.Action class location is
i am trying to create a pdf email using data inputed into a html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.