Document Solutions for PDF
Features / Pages
In This Topic
    Pages
    In This Topic

    Each page of a document is represented by a page object that includes references to the content and other attributes of a page. DsPdf provides Page class held in GrapeCity.Documents.Pdf assembly to allow you to work with pages. The Page class represents a page in GcPdfDocument. To get started, you need to add a page to your PDF document using NewPage method. When a new page is created, it is added to the page collection which is a collection of document's pages. The collection allows standard collection operations, such as adding, inserting, deleting, and moving elements(pages). These pages can be modified using the following page properties for individual pages while creating a PDF document.

    Insert a Page

    To insert an empty page in a PDF document:

    1. Create an object of GcPdfDocument class.
    2. Access the NewPage method of GcDdfDocument class using the GcPdfDocument object.
      Page.cs
      Copy Code
      GcPdfDocument doc = new GcPdfDocument();
      // Adds a new blank page
      var page = doc.NewPage();
      
    Back to Top

    Get a Particular Page

    To get a particular page from a document:

    1. Create an instance of PageCollection class that includes all the pages added in a PDF document.
    2. Use the PageCollection object to access any particular page using its index value.
      Page.cs
      Copy Code
      // Load an existing PDF using FileStream
      FileStream fileStream = File.OpenRead(args[0].ToString());
      GcPdfDocument doc = new GcPdfDocument();
      doc.Load(fileStream, null);
      
      // Use the PageCollection object to get page properties
      PageCollection pageCollection = doc.Pages;
      // Get the owner of the page
      Console.WriteLine("Page Owner: {0}", pageCollection[0].Owner);
      
    Back to Top

    Get Page Properties

    To get page properties:

    1. Create an instance of PageCollection class that includes all the pages added in a PDF document.
    2. Use the PageCollection object to access any particular page using its index value.
    3. Access the properties associated to a particular page through its page index, for example, Size property.
      C#
      Copy Code
      // Load an existing PDF using FileStream
      FileStream fileStream = File.OpenRead(args[0].ToString());
      GcPdfDocument doc = new GcPdfDocument();
      var page = doc.NewPage();
      doc.Load(fileStream, null);
      
      // Use the PageCollection object to get a particular page
      PageCollection pageCollection = doc.Pages;
      // Get the size of first page
      Console.WriteLine("Paper Size: {0}", pageCollection[0].Size);
      
    Back to Top

    Set Page Properties

    To set page properties:

    1. Create an object of GcPdfDocument class.
    2. Access the NewPage method using the GcPdfDocument object.
    3. Use the page object to set a page property, for example, Rotate property.
      C#
      Copy Code
      GcPdfDocument doc = new GcPdfDocument();
      // Adds a new blank page
      var page = doc.NewPage();
              
      // Set the page property
      page.Rotate = 90;
      
    Back to Top

    Set PageSize and Orientation

    To set a new page size and orientation in a document:

    1. Add a new page in the PDF document using NewPage method of GcPdfDocument class.
    2. Set the PaperKind and Landscape property using the page object.
      PageSize.cs
      Copy Code
      var doc = new GcPdfDocument();
      // The default page size is Letter (8 1/2" x 11") with portrait orientation
      var page = doc.NewPage();
              
      // Change the page size and orientation
      page.PaperKind = PaperKind.A4;
      page.Landscape = true;
      
    Back to Top

    Add Page Labels

    DsPdf allows to define page labels with meaningful descriptions rather than just page numbers for identifying a page in a PDF document. Page labels allow to subdivide the document into sequences of logically related page ranges. In addition, it allows you to add multiple page labeling ranges in a single PDF document, that do not intersect each other. This can be very helpful when the PDF document contains different sections such as preface, acknowledgment, main body, index etc.

    In DsPdf, the PageLabelingRange class represents a page labeling range which helps in defining the page numbering style for the range and a meaningful prefix that denotes the range. To add page labels in a PDF document, use the PageLabelingRanges property provided by the GcPdfDocument class as shown in the code below.

    C#
    Copy Code
    public void CreatePDF()
    {
        //Initialize GcPdfDocument 
        var doc = new GcPdfDocument();           
    
        //Define text layout                      
        var tl = new TextLayout(72);
        tl.MaxWidth = doc.PageSize.Width;
        tl.MaxHeight = doc.PageSize.Height;           
        TextSplitOptions to = new TextSplitOptions(tl)
        {                
            MinLinesInFirstParagraph = 2,
            MinLinesInLastParagraph = 2
        };            
        doc.Pages.Add();
        // Generate random text for the document
        doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
        tl.Clear();
        tl.Append(Common.Util.LoremIpsum(17));
        tl.PerformLayout(true); 
        // Print the random text
        while (true)
        {               
            var splitResult = tl.Split(to, out TextLayout rest);
            doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
            if (splitResult != SplitResult.Split)
                break;
            tl = rest;
            var p = doc.Pages.Add();
        }
        //Define PageLabelingRange for content pages
        //PageLabelingRange uses DecimalArabic NumberingStyle and "Content Page, p. " as pre 
        //of the page label
        doc.PageLabelingRanges.Add(2, new PageLabelingRange($"Content Page, p. ",
        NumberingStyle.DecimalArabic, 1));
    
        // Done:
        doc.Save("NewPageLabel.pdf");
    }
    

    Working with ContentStreams

    ContentStream object consists a sequence of instructions describing the graphical elements to be rendered on a page. ContentStream is a useful feature, when you are working with multiple graphical elements in a single PDF document. All the content stream added in a PDF document is stored in PageContentStreamCollection. You can access this class to add or remove items to the content stream.

    To use content stream on a page:

    1. Create an object of PageContentStream class.
    2. Add graphic elements to the content stream using the DrawString method of GcPdfGraphics class.
    3. Save the document.
      C#
      Copy Code
      public void CreatePDF(Stream stream)
      {
          GcPdfDocument doc = new GcPdfDocument();
          var page = doc.NewPage();
          var g = page.Graphics;
          const float In = 72;
          var tf = new TextFormat()
          {
              Font = StandardFonts.Times,
              FontSize = 12
          };
      
          // Creating PageContentStream object
          PageContentStream contentStream = new PageContentStream(doc);
          // Adding Graphics to the ContentStream
          contentStream.Doc.Pages[0].Graphics.DrawString(
           "1. Test string. This is a sample string",
           tf, new PointF(In, In));
          // Saving the document
          doc.Save(stream);
      }
      
    Back to Top

    For more information about implementation of pages using DsPdf, see DsPdf sample browser.

    Clone a Page

    DsPdf provides ClonePage method in PageCollection class to clone a particular page from a specified index in the PDF file and insert it into a specified index of the same PDF file. ClonePage method also supports two additional boolean type parameters: cloneAnnotations and cloneFields, which enable a user to allow or restrict the cloning of annotations and fields on the page to be cloned.

    C#
    Copy Code
    // Initialize GcPdfDocument.
    GcPdfDocument doc = new GcPdfDocument();
    
    // Load the PDF file from the stream.
    var fs = new FileStream(Path.Combine("digital-signature-sample.pdf"), FileMode.Open,
                 FileAccess.Read);
    doc.Load(fs);
    
    // Clone page at index 0 and insert it at index 1, copy fields but skip annotations.
    doc.Pages.ClonePage(0, 1, false, true);
    
    // Save the PDF document.
    doc.Save("ClonePDFPageWithoutAnnotationsWithFields.pdf");
    
    Note: DsPdf also provides MergeWithDocument method that can be used to copy pages between different PDF files. For more information on merging documents, see Merge Documents.