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.
To insert an empty page in a PDF document:
Page.cs |
Copy Code
|
---|---|
GcPdfDocument doc = new GcPdfDocument(); // Adds a new blank page var page = doc.NewPage(); |
To get a particular page from a document:
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); |
To get page properties:
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); |
To set page properties:
C# |
Copy Code
|
---|---|
GcPdfDocument doc = new GcPdfDocument(); // Adds a new blank page var page = doc.NewPage(); // Set the page property page.Rotate = 90; |
To set a new page size and orientation in a document:
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; |
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"); } |
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:
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); } |
For more information about implementation of pages using DsPdf, see DsPdf sample browser.
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"); |