Documents for PDF .NET Edition
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. GcPdf 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

    GcPdf 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 GcPdf, 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 GcPdf, see GcPdf sample browser.