How To Add and Delete PDF Pages

This example demonstrates how to add and delete pages  to/from PDF document using PDF Mosaic Library.

Add and Delete PDF Pages

 

C# :

/*Description:This sample shows how to add and delete pages to/from PDF document */

using PDFMosaic;
using System;

namespace AddAndDeletePages
{
  class AddAndDeletePages
  {
    static void Main(string[] args)
    {
      PDFDocument document = new PDFDocument();

      // Create font and brush for drawing on page
      PDFFont font = new PDFFont(PDFStandardFont.Helvetica, 25);
      PDFBrush brush = new PDFSolidBrush();
      
      // Add a standart page sizes
      foreach(PDFPaperFormat pageSize in Enum.GetValues(typeof(PDFPaperFormat)))
      {
        // Append new standart size page to the document
        PDFPage page = new PDFPage(pageSize);
        page.Canvas.DrawString("The page format is " + pageSize, font, brush, 100, 100);
        
        // Add created page to page collection
        document.Pages.Add(page);
      }
            
      // Create new custom size page to the document
      PDFPage customSizePage = new PDFPage(800, 600);
      customSizePage.Canvas.DrawString("This is the custom page size. Width = 800. Height = 600.", font, brush, 100, 100);
      document.Pages.Add(customSizePage);
      
      // Save pdf document
      document.Save("AddAndDeletePages.pdf", true);

      // Open created document before
      PDFDocument document2 = new PDFDocument("AddAndDeletePages.pdf");
      
      // Delete all pages, which width > 500
      for(int i = document2.Pages.Count-1; i >= 0; --i)
      {
        if(document2.Pages[i].Width > 500)
          document2.Pages.Remove(i);
      }
      
      // Save modifed pdf document
      document2.Save("AddAndDeletePages2.pdf", true);
    }
  }
}

Visual Basic.NET :

' Description:
' This sample shows how to add and delete pages to/from PDF document

Imports PDFMosaic
Imports System

Module AddAndDeletePages
  Sub Main()
  Dim document As New PDFDocument()

  ' Create font and brush for drawing on page
  Dim font As New PDFFont(PDFStandardFont.Helvetica, 25)
  Dim brush As New PDFSolidBrush()
  
  ' Add a standart page sizes
  For Each pageSize As PDFPaperFormat In [Enum].GetValues(GetType(PDFPaperFormat))
    ' Append new standart size page to the document
    Dim page As New PDFPage(pageSize)
    page.Canvas.DrawString("The page format is " & pageSize.ToString(), font, brush, 100, 100)
    
    ' Add created page to page collection
    document.Pages.Add(page)
  Next
  
              
  ' Create new custom size page to the document
  Dim customSizePage As New PDFPage(800, 600)
  customSizePage.Canvas.DrawString("This is the custom page size. Width = 800. Height = 600.", font, brush, 100, 100)
  document.Pages.Add(customSizePage)
  
  ' Save document
  document.Save("AddAndDeletePages.pdf", True)  
  
  ' Open created document before
  Dim document2 As New PDFDocument("AddAndDeletePages.pdf")
  
        
  ' Delete all pages, which width > 500
  ' Add a standart page sizes
  Dim i As Integer = document2.Pages.Count-1
  Do While i > 0
    If document2.Pages(i).Width > 500 Then document2.Pages.Remove(i)
    i -= 1
  Loop
  
  ' Save document
  document2.Save("AddAndDeletePages2.pdf", True)  
  End Sub
End Module