PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2008 VS 2005 CLR 2.0

Adding Page Breaks To A PDF Document In .NET

Learn to use the new AddPageBreak method introduced in v3.0.4.
By V. Subhash

In Version v3.0.4, we have introduced a new method named AddPageBreak() in the PDFDocument class. This method behaves like the similar feature in Microsoft Word and other word-processing applications. It inserts a new page and makes it the current page for further content rendering operations. However, the method does this only when it is creating a document. When this method is called after loading an existing document, then no new page is created but the next page is made the current page and the focus of further rendering operations.

To find out how this works, create a Windows Forms project, add two buttons and use the following click-event handlers.

// Illustrates AddPageBreak() behavior in creation mode
private void button1_Click(object sender, EventArgs e) {
  PDFDocument doc = 
     new PDFDocument("your-license-key");
  doc.OpenAfterCreate = true;
  doc.MeasurementUnit = PDFMeasurementUnit.Inches;
  doc.AutoPaginate = false;
  PDFFont fontCourier = new PDFFont(StdType1Font.Courier, PDFFontStyle.Fill, 24);

  for (float i = 1, y=0.5f; i <= 40; i++) {

    doc.WriteText(i.ToString() + 
                    ". I will not litter the classroom. ", 
                  fontCourier, 1, y);
    y += 0.5f;
    if ( (i % 10 == 0) & (i < 40)) {
        // Insert a new page
        doc.AddPageBreak();
        y = 0.5f;
    }

  }

  doc.Save("imposition.pdf");
  doc.Dispose(); 
}

// Illustrates AddPageBreak() behavior in reading mode
private void button2_Click(object sender, EventArgs e) {
  PDFDocument doc = 
     new PDFDocument("your-license-key");
  doc.Load("imposition.pdf");
  doc.OpenAfterCreate = true;
  doc.MeasurementUnit = PDFMeasurementUnit.Inches;
  doc.AutoPaginate = false;
  doc.Brush.Color = Color.Red;

  PDFFont fontCourier = new PDFFont(StdType1Font.Courier, PDFFontStyle.Fill, 24);

  for (float i = 1, y = 0.5f; i <= 40; i++) {
    doc.WriteText(" Now, behave! ", fontCourier, 5.5f, y);
    y += 0.5f;
    if ((i % 10 == 0) & (i < 40)) {
      // Move to the next page
      doc.AddPageBreak();
      y = 0.5f;
    }
  }

  doc.Save("imposition_reviewed.pdf");
  doc.Dispose();
  Close();
}

When you run the application and click on the first button, it will create a PDF document like this. The method AddPageBreak() creates a new page each time it is called.

When you click the second button, new pages are not created. However, the focus simply moves to the next page and rendering operations resume from there. Thanks to this behaviour, we are able to render content where it was previously left off when the document was first created.

Privacy | Legal | Feedback | Newsletter © 2002-2010 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.