Working with PDF document Pages

16 Oct 202324 minutes to read

Adding a new page to the document

The following code sample explains you on how to add a PdfPage in a PDF document. When multiple pages are added, the new page is always added to the end of the document.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Create a solid brush.
PdfBrush brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);
//Draw the text.
graphics.DrawString("Hello world!", font, brush, new Syncfusion.Drawing.PointF(20, 20));

//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document as stream.
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Create a solid brush.
PdfBrush brush = new PdfSolidBrush(Color.Black);
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);
//Draw the text.
graphics.DrawString("Hello world!", font, brush, new PointF(20, 20));

//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a page.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics

'Create a solid brush.
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
'Set the font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14)
'Draw the text.
graphics.DrawString("Hello world!", font, brush, New PointF(20, 20))

'Save and close the document.
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Inserting pages in a document

You can insert an empty page at any location in the existing PDF document using Insert method. The below code snippet explains the same.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Insert a new page in the beginning of the document.
loadedDocument.Pages.Insert(0);

//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document as stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close(true);
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Insert a new page in the beginning of the document.
loadedDocument.Pages.Insert(0);
//Save and close the document.
loadedDocument.Save("Output.pdf");
loadedDocument.Close(true);
'Load the PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Insert a new page in the beginning of the document.
loadedDocument.Pages.Insert(0)
'Save and close the document.
loadedDocument.Save("Output.pdf")
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Adding margin to the PDF pages

You can add margin to all the PDF pages of the PDF document using the PageSettings property. The following code snippet illustrates the same.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Set margin for all the pages.
document.PageSettings.Margins.All = 10;
//Add a page.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Creates a solid brush.
PdfBrush brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);
//Draw the text.
graphics.DrawString("Hello world!", font, brush, new Syncfusion.Drawing.PointF(20, 20));

//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document as stream.
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Set margin for all the pages
document.PageSettings.Margins.All = 10;
//Add a page.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Creates a solid brush.
PdfBrush brush = new PdfSolidBrush(Color.Black);
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);
//Draw the text.
graphics.DrawString("Hello world!", font, brush, new PointF(20, 20));

//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Set margin for all the pages.
document.PageSettings.Margins.All = 10
'Add a page.
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics

'Create a solid brush.
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
'Set the font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14)
'Draw the text.
graphics.DrawString("Hello world!", font, brush, New PointF(20, 20))

'Save ad close the document.
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Adding sections with different page settings

Essential PDF supports adding sections with different page settings like Height, Margins, Orientation, Rotate, Size, Transition and Width. You can add sections to a PDF document by using the PdfSection available in PdfDocument instance and create page settings to the PdfSection using the PageSettings property.

The following code snippet explains how to add more sections to a PDF document with different page settings.

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Create a solid brush and standard font
PdfBrush brush = new PdfSolidBrush(Color.Black);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);

//Section - 1
//Add new section to the document
PdfSection section = document.Sections.Add();
//Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle0;
section.PageSettings.Size = PdfPageSize.A5;
section.PageSettings.Width = 300;
section.PageSettings.Height = 400;
//Add page to the section and initialize graphics for the page
PdfPage page = section.Pages.Add();
PdfGraphics graphics = page.Graphics;
//Draw simple text on the page
graphics.DrawString("Rotated by 0 degrees", font, brush, new PointF(20, 20));

//Section - 2
//Add new section to the document
section = document.Sections.Add();
//Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
section.PageSettings.Width = 300;
section.PageSettings.Height = 400;
//Add page to the section and initialize graphics for the page
page = section.Pages.Add();
graphics = page.Graphics;
//Draw simple text on the page
graphics.DrawString("Rotated by 90 degrees", font, brush, new PointF(20, 20));

//Section - 3
//Add new section to the document
section = document.Sections.Add();
//Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
section.PageSettings.Width = 500;
section.PageSettings.Height = 200;
//Add page to the section and initialize graphics for the page
page = section.Pages.Add();
graphics = page.Graphics;
//Draw simple text on the page
graphics.DrawString("Rotated by 180 degrees", font, brush, new PointF(20, 20));

//Section - 4
//Add new section to the document
section = document.Sections.Add();
//Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle270;
section.PageSettings.Width = 300;
section.PageSettings.Height = 200;
//Add page to the section and initialize graphics for the page
page = section.Pages.Add();
graphics = page.Graphics;
//Draw simple text on the page
graphics.DrawString("Rotated by 270 degrees", font, brush, new PointF(20, 20));

//Saving the PDF to the MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream);

//Close the document.
document.Close(true);
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Create a solid brush and standard font
PdfBrush brush = new PdfSolidBrush(Color.Black);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);

//Section - 1
//Add new section to the document
PdfSection section = document.Sections.Add();
//Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle0;
section.PageSettings.Size = PdfPageSize.A5;
section.PageSettings.Width = 300;
section.PageSettings.Height = 400;
//Add page to the section and initialize graphics for the page
PdfPage page = section.Pages.Add();
PdfGraphics graphics = page.Graphics;
//Draw simple text on the page
graphics.DrawString("Rotated by 0 degrees", font, brush, new PointF(20, 20));

//Section - 2
//Add new section to the document
section = document.Sections.Add();
//Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
section.PageSettings.Width = 300;
section.PageSettings.Height = 400;
//Add page to the section and initialize graphics for the page
page = section.Pages.Add();
graphics = page.Graphics;
//Draw simple text on the page
graphics.DrawString("Rotated by 90 degrees", font, brush, new PointF(20, 20));

//Section - 3
//Add new section to the document
section = document.Sections.Add();
//Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
section.PageSettings.Width = 500;
section.PageSettings.Height = 200;
//Add page to the section and initialize graphics for the page
page = section.Pages.Add();
graphics = page.Graphics;
//Draw simple text on the page
graphics.DrawString("Rotated by 180 degrees", font, brush, new PointF(20, 20));

//Section - 4
//Add new section to the document
section = document.Sections.Add();
//Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle270;
section.PageSettings.Width = 300;
section.PageSettings.Height = 200;
//Add page to the section and initialize graphics for the page
page = section.Pages.Add();
graphics = page.Graphics;
//Draw simple text on the page
graphics.DrawString("Rotated by 270 degrees", font, brush, new PointF(20, 20));

//Save the document
document.Save("Output.pdf");
//Close the instance of PdfDocument
document.Close(true);
'Create a new PDF document
Dim document As PdfDocument = New PdfDocument
'Create a solid brush and standard font
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14)

'Section - 1
'Add new section to the document
Dim section As PdfSection = document.Sections.Add
'Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle0
section.PageSettings.Size = PdfPageSize.A5
section.PageSettings.Width = 300
section.PageSettings.Height = 400
'Add page to the section and initialize graphics for the page
Dim page As PdfPage = section.Pages.Add
Dim graphics As PdfGraphics = page.Graphics
'Draw simple text on the page
graphics.DrawString("Rotated by 0 degrees", font, brush, New PointF(20, 20))

'Section - 2
'Add new section to the document
section = document.Sections.Add
'Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90
section.PageSettings.Width = 300
section.PageSettings.Height = 400
'Add page to the section and initialize graphics for the page
page = section.Pages.Add
graphics = page.Graphics
'Draw simple text on the page
graphics.DrawString("Rotated by 90 degrees", font, brush, New PointF(20, 20))

'Section - 3
'Add new section to the document
section = document.Sections.Add
'Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180
section.PageSettings.Width = 500
section.PageSettings.Height = 200
'Add page to the section and initialize graphics for the page
page = section.Pages.Add
graphics = page.Graphics
'Draw simple text on the page
graphics.DrawString("Rotated by 180 degrees", font, brush, New PointF(20, 20))

'Section - 4
'Add new section to the document
section = document.Sections.Add
'Create page settings to the section
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle270
section.PageSettings.Width = 300
section.PageSettings.Height = 200
'Add page to the section and initialize graphics for the page
page = section.Pages.Add
graphics = page.Graphics
'Draw simple text on the page
graphics.DrawString("Rotated by 270 degrees", font, brush, New PointF(20, 20))

'Save the document
document.Save("Output.pdf")
'Close the instance of PdfDocument
document.Close(True)

You can download a complete working sample from GitHub.

Get number of pages from a PDF document

You can get page count from the existing PDF document as shown in the following code snippet.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Get the page count.
int pageCount = loadedDocument.Pages.Count;
//Close the document.
loadedDocument.Close(true);
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Get the page count.
int pageCount = loadedDocument.Pages.Count;
//Close the document.                     
loadedDocument.Close(true);
'Load the PDF document.
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Get the page count.
Dim pageCount As Integer = loadedDocument.Pages.Count
'Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Importing pages from an existing document.

Essential PDF allows you to import a page or import a range of pages from one document to the other. The following code sample illustrates how to import a range of pages from an existing document using ImportPageRange method.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
int startIndex = 0;
int endIndex = loadedDocument.Pages.Count - 1;
//Import all the pages to the new PDF document.
document.ImportPageRange(loadedDocument, startIndex, endIndex);

//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document as stream.
document.Save(stream);
//Close the document instances.
document.Close(true);
loadedDocument.Close(true);
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Create a new PDF document.
PdfDocument document = new PdfDocument();
int startIndex = 0;
int endIndex = loadedDocument.Pages.Count - 1;
//Import all the pages to the new PDF document.
document.ImportPageRange(loadedDocument, startIndex, endIndex);

//Save the document.
document.Save("Output.pdf");
//Close both document instances.
loadedDocument.Close(true);
document.Close(true);
'Load the PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Create a new PDF document.
Dim document As New PdfDocument()
Dim startIndex As Integer = 0
Dim endIndex As Integer = loadedDocument.Pages.Count - 1
'Import all the pages to the new PDF document.
document.ImportPageRange(loadedDocument, startIndex, endIndex)

'Save the document.
document.Save("Output.pdf")
'Close both document instances.
loadedDocument.Close(True)
document.Close(True)

You can download a complete working sample from GitHub.

Importing pages from an existing document without bookmarks.

You can import a page or range of pages from one document to other without bookmarks using ImportPageRange method. Refer to the following code sample.

NOTE

Performance will be effective only in the large PDF document.

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Create the new PDF document
PdfDocument document = new PdfDocument();
int startIndex = 0;
int endIndex = loadedDocument.Pages.Count - 1;
//Import all the pages to the new PDF document
document.ImportPageRange(loadedDocument, startIndex, endIndex, false);

//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document as stream
document.Save(stream);
//Close the document instances
document.Close(true);
loadedDocument.Close(true);
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");

//Create the new PDF document
PdfDocument document = new PdfDocument();
int startIndex = 0;
int endIndex = loadedDocument.Pages.Count - 1;
//Import all the pages to the new PDF document without bookmarks
document.ImportPageRange(loadedDocument, startIndex, endIndex,false);

//Save the document
document.Save("Output.pdf");
//Close both document instances
loadedDocument.Close(true);
document.Close(true);
System.Diagnostics.Process.Start("Output.pdf");
'Load the PDF document
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Create the new PDF document
Dim document As PdfDocument = New PdfDocument()
Dim startIndex As Integer = 0
Dim endIndex As Integer = loadedDocument.Pages.Count - 1
'Import all the pages to the new PDF document without bookmarks
document.ImportPageRange(loadedDocument, startIndex, endIndex, False)

'Save the document
document.Save("Output.pdf")
'Close both the document instances
loadedDocument.Close(True)
document.Close(True)

You can download a complete working sample from GitHub.

Rearranging pages in an existing document

You can rearrange the pages in an existing PDF document using ReArrange method. This method uses zero based start index. The following code snippet illustrates the same.

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Rearrange the page by index
loadedDocument.Pages.ReArrange(new int[] { 1, 0 });//Creating the stream object
MemoryStream stream = new MemoryStream();

//Save the document as stream
loadedDocument.Save(stream);
//Close the document
loadedDocument.Close(true);
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Rearrange the page by index
loadedDocument.Pages.ReArrange(new int[] {1, 0});
//Save and close the document
loadedDocument.Save("Output.pdf");
loadedDocument.Close(true);
'Load the PDF document
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Rearrange the page by index
loadedDocument.Pages.ReArrange(New Integer() {1, 0})
'Save and close the document
loadedDocument.Save("Output.pdf")
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Changing the page numbers in a PDF document

You can alter the page label for the existing PDF document using PdfPageLabel class. Refer to the following code example.

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
// Create a page label
PdfPageLabel pageLabel = new PdfPageLabel();
//Set the number style with upper case roman letters
pageLabel.NumberStyle = PdfNumberStyle.UpperRoman;
//Set the staring number as 1
pageLabel.StartNumber = 1;
loadedDocument.LoadedPageLabel = pageLabel;

//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document as stream
loadedDocument.Save(stream);
//Close the document
loadedDocument.Close(true);
//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Create a page label
PdfPageLabel pageLabel = new PdfPageLabel();
//Set the number style with upper case roman letters
pageLabel.NumberStyle = PdfNumberStyle.UpperRoman;
//Set the staring number as 1
pageLabel.StartNumber = 1;
loadedDocument.LoadedPageLabel = pageLabel;

//Save the document
loadedDocument.Save("Output.pdf");
//Close the document
loadedDocument.Close(true);
'Load the PDF document
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Create a page label
Dim pageLabel As New PdfPageLabel()
'Set the number style with upper case roman letters
pageLabel.NumberStyle = PdfNumberStyle.UpperRoman
'Set the staring number as 1
pageLabel.StartNumber = 1
loadedDocument.LoadedPageLabel = pageLabel

'Save the document
loadedDocument.Save("Output.pdf")
'Close the document
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Removing pages from a document

You can remove the pages from the existing PDF document using RemoveAt method as shown in the below code example.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Remove the first page in the PDF document
loadedDocument.Pages.RemoveAt(0);
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document as stream
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close(true);
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Remove the first page in the PDF document
loadedDocument.Pages.RemoveAt(0);
//Save the document.
loadedDocument.Save("Output.pdf");
//Close the document.            
loadedDocument.Close(true);
'Load the PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Remove the first page in the PDF document
loadedDocument.Pages.RemoveAt(0)
'Save the document.
loadedDocument.Save("Output.pdf")
'Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Rotating a PDF page

You can rotate a particular PDF page in the PDF document using PdfPageRotateAngle Enum as shown the following code example.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a section.
PdfSection section = document.Sections.Add();
//Rotate a section/page
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
PdfPage page = section.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Create a solid brush.
PdfBrush brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);
//Draws the text.
graphics.DrawString("Rotated by 90 degree", font, brush, new Syncfusion.Drawing.PointF(20, 20));

//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document as stream
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a section.
PdfSection section = document.Sections.Add();
//Rotate a section/page
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
PdfPage page = section.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

//Create a solid brush.
PdfBrush brush = new PdfSolidBrush(Color.Black);
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);
//Draws the text.
graphics.DrawString("Rotated by 90 degree", font, brush, new PointF(20, 20));

//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As New PdfDocument()
'Add a section.
Dim section As PdfSection = document.Sections.Add()
'Rotate a section/page
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90
Dim page As PdfPage = section.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics

'Create a solid brush.
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
'Set the font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 14)
'Draw the text.
graphics.DrawString("Rotated by 90 degree", font, brush, New PointF(20, 20))

'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)

You can download a complete working sample from GitHub.

Rotating an existing PDF page

You can also rotate a PDF page in the existing PDF document using PdfPageRotateAngle as shown in the following code example.

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Gets the page
PdfPageBase loadedPage = loadedDocument.Pages[0] as PdfPageBase;
//Set the rotation for loaded page
loadedPage.Rotation = PdfPageRotateAngle.RotateAngle90;            

//Save the document into stream 
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the document 
loadedDocument.Close(true);
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Gets the page.
PdfPageBase loadedPage = loadedDocument.Pages[0] as PdfPageBase;
//Set the rotation for loaded page.
loadedPage.Rotation = PdfPageRotateAngle.RotateAngle90;
             
//Save the document.
loadedDocument.Save("Output.pdf");
//Close the Document.
loadedDocument.Close(true);
'Load the PDF document. 
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Gets the page.
Dim page As PdfPageBase = TryCast(loadedDocument.Pages(0), PdfPageBase)
'Set the rotation for loaded page.
page.Rotation = PdfPageRotateAngle.RotateAngle90

'Save the document. 
loadedDocument.Save("Output.pdf")
'Closes the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Detect empty pages from a PDF document

You can find the empty pages from the PDF document using the IsBlank property as shown in the following code sample.

//Load the PDF document.
FileStream docStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Gets the page.
PdfPageBase loadedPage = loadedDocument.Pages[0] as PdfPageBase;
//get the page is blank or not.
bool isEmpty = loadedPage.IsBlank;

//Save the document into a stream. 
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close(true);
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Gets the page.
PdfPageBase loadedPage = loadedDocument.Pages[0] as PdfPageBase;
//Get the page is blank or not.
bool isEmpty = loadedPage.IsBlank;

//Save the document.
loadedDocument.Save("Output.pdf");
//Close the document.
loadedDocument.Close(true);
'Load the PDF document.
 Dim loadedDocument As New PdfLoadedDocument("input.pdf")
'Gets the page.
 Dim page As PdfPageBase = TryCast(loadedDocument.Pages(0), PdfPageBase)
'Get the page is blank or not.
 bool isEmpty = loadedPage.IsBlank
 
'Save the document.
loadedDocument.Save("Output.pdf")
'Closes the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Splitting a PDF file to individual pages

Essential PDF allows to split the pages of an existing PDF document into multiple individual PDF documents using Split method of PdfLoadedDocument class. The following code snippet explains the same.

//Due to platform limitations, Essential PDF supports splitting a PDF file into individual pages only in Windows Forms, WPF, ASP.NET, and ASP.NET MVC platforms. However this can be achieved by using the following code snippet. 

//Load the PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
for (int i=0;i<loadedDocument.PageCount;i++)
{
//Creates a new document
PdfDocument document = new PdfDocument();
//Imports the pages from the loaded document
document.ImportPage(loadedDocument, i);

//Create a memory stream 
MemoryStream stream = new MemoryStream();
//Save the document to stream
document.Save(stream);
//Close the document
document.Close(true);
}
//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Sets pattern.
const string destinationFilePattern = "Output" + "{0}.pdf";
//Split the pages into separate documents.
loadedDocument.Split(destinationFilePattern);
//Close the document.
loadedDocument.Close(true);
'Load the PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Sets pattern.
Const destinationFilePattern As String = "Output" + "{0}.pdf"
'Split the pages into separate documents.
loadedDocument.Split(destinationFilePattern)
'Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Span a text element to multiple pages and draw the next element

The ‘PdfLayoutFormat’ class helps to allow the text to flow across pages. The ‘PdfLayoutResult’ class provides the rendered bounds of the previously added text, which can be used to place successive elements without overlapping.
The ‘Syncfusion PDF library’ provides ‘PageAddedEventArgs’ to get the current Page details, and we can draw the next new text element from where the last text element ends.
The following code example illustrates the same.

//Create a PDF document instance.
PdfDocument document = new PdfDocument();
//Add the event.
document.Pages.PageAdded += Pages_PageAdded;
//Create a new page and add it as the last page of the document.
PdfPage page = document.Pages.Add();
PdfGraphics graphics = page.Graphics;

//Read the long text from the text file.
FileStream inputStream = new FileStream("Input.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(inputStream, Encoding.ASCII);
string text = reader.ReadToEnd();
reader.Dispose();
const int paragraphGap = 10;
//Create a text element with the text and font.
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
layoutFormat.Break = PdfLayoutBreakType.FitPage;
//Draw the first paragraph.
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);
//Draw the second paragraph from the first paragraph’s end position.
result = textElement.Draw(result.Page, new RectangleF(0, result.Bounds.Bottom + paragraphGap, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);

//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into memory stream.
document.Save(stream);
//Close the document.
document.Close(true);

//Event handler for PageAdded event.
void Pages_PageAdded(object sender, PageAddedEventArgs args)
{
PdfPage page = args.Page;
page.Graphics.DrawRectangle(PdfPens.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height));
}
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add the event.
document.Pages.PageAdded += Pages_PageAdded;
//Create a new page and add it as the last page of the document.
PdfPage page = document.Pages.Add();
PdfGraphics graphics = page.Graphics;

//Read the long text from the text file.
StreamReader reader = new StreamReader(@"input.txt", Encoding.ASCII);
string text = reader.ReadToEnd();
reader.Close();
const int paragraphGap = 10;
//Create a text element with the text and font.
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14));
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
layoutFormat.Break = PdfLayoutBreakType.FitPage;

//Draw the first paragraph.
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);
//Draw the second paragraph from the first paragraph’s end position.
result = textElement.Draw(result.Page, new RectangleF(0, result.Bounds.Bottom + paragraphGap, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat);

//Save and close the document.
document.Save("Sample.pdf");
document.Close(true);

//Event handler for PageAdded event.
void Pages_PageAdded(object sender, PageAddedEventArgs args)
{
PdfPage page = args.Page;
page.Graphics.DrawRectangle(PdfPens.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height));
}
'Create a new PDF document.
Dim document As New PdfDocument()
'Add the event.
AddHandler document.Pages.PageAdded, AddressOf Pages_PageAdded
'Create a new page and add it as the last page of the document.
Dim page As PdfPage = document.Pages.Add()
Dim graphics As PdfGraphics = page.Graphics

'Read the RTL text from the text file.
Dim reader As New StreamReader("input.txt", Encoding.ASCII)
Dim text As String = reader.ReadToEnd()
reader.Close()
Const paragraphGap As Integer = 10
'Create a text element with the text and font.
Dim textElement As New PdfTextElement(text, New PdfStandardFont(PdfFontFamily.TimesRoman, 14))
Dim layoutFormat As New PdfLayoutFormat()
layoutFormat.Layout = PdfLayoutType.Paginate
layoutFormat.Break = PdfLayoutBreakType.FitPage

'Draw the first paragraph.
Dim result As PdfLayoutResult = textElement.Draw(page, New RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat)
'Draw the second paragraph from the first paragraph’s end position.
result = textElement.Draw(result.Page, New RectangleF(0, result.Bounds.Bottom + paragraphGap, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat)

'Save and close the document.
document.Save("Sample.pdf")
document.Close(True)

'Event handler for PageAdded event.
Private Sub Pages_PageAdded(sender As Object, args As PageAddedEventArgs)
Dim page As PdfPage = args.Page
page.Graphics.DrawRectangle(PdfPens.Black, New RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height))
End Sub

You can download a complete working sample from GitHub.