Page Setting

Page Setting (26)

Thursday, 24 October 2019 07:46

Get PDF page size in C#

Written by support iceblue

With Spire.PDF for .NET, developers can set page size for PDF in C#. This article will demonstrates how to get the PDF page size using Spire.PDF.

Detail steps:

Step 1: Create a PdfDocument instance and load the sample.pdf file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Sample.pdf");

Step 2: Get the width and height of the first page in the PDF file.

PdfPageBase page = doc.Pages[0];
float pointWidth = page.Size.Width;
float pointHeight = page.Size.Height;

Step 3: Convert the size with other measurement unit, such as in Inch, Centimeter, Unit or Pixel.

//Create PdfUnitConvertor to convert the unit
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

//Convert the size with "pixel"
float pixelWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
float pixelHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);

//Convert the size with "inch"
float inchWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);
float inchHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);

//Convert the size with "centimeter"
float centimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
float centimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);

Step 4: Save to a .txt file.

//Create StringBuilder to save 
StringBuilder content = new StringBuilder();
//Add pointSize string to StringBuilder
content.AppendLine("The page size of the file is (width: " + pointWidth + "pt, height: " + pointHeight + "pt).");
content.AppendLine("The page size of the file is (width: " + pixelWidth + "pixel, height: " + pixelHeight + "pixel).");
content.AppendLine("The page size of the file is (width: " + inchWidth + "inch, height: " + inchHeight + "inch).");
content.AppendLine("The page size of the file is (width: " + centimeterWidth + "cm, height: " + centimeterHeight + "cm.)");

String output = "GetPageSize_out.txt";
//Save them to a txt file
File.WriteAllText(output, content.ToString());

Output:

Get PDF page size in C#

Full code:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetPDFPageSize
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("Sample.pdf");

            //Get the first page of the loaded PDF file
            PdfPageBase page = doc.Pages[0];
            //Get the width of page based on "point"
            float pointWidth = page.Size.Width;
            //Get the height of page
            float pointHeight = page.Size.Height;

            //Create PdfUnitConvertor to convert the unit
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

            //Convert the size with "pixel"
            float pixelWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
            float pixelHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);

            //Convert the size with "inch"
            float inchWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);
            float inchHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch);

            //Convert the size with "centimeter"
            float centimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
            float centimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);

            //Create StringBuilder to save 
            StringBuilder content = new StringBuilder();
            
            //Add pointSize string to StringBuilder
            content.AppendLine("The page size of the file is (width: " + pointWidth + "pt, height: " + pointHeight + "pt).");
            content.AppendLine("The page size of the file is (width: " + pixelWidth + "pixel, height: " + pixelHeight + "pixel).");
            content.AppendLine("The page size of the file is (width: " + inchWidth + "inch, height: " + inchHeight + "inch).");
            content.AppendLine("The page size of the file is (width: " + centimeterWidth + "cm, height: " + centimeterHeight + "cm.)");

            String output = "GetPageSize_out.txt";

            //Save them to a txt file
            File.WriteAllText(output, content.ToString());        

        }
    }
}
Tuesday, 26 March 2019 07:12

Get PDF Page Labels in C#

Written by support iceblue

Page labels are used to identify each page visually on the screen or in print. This article demonstrates how to get the PDF page labels using Spire.PDF.

Below is the screenshot of the sample PDF document:

Get PDF Page Labels in C#

Detail steps:

Step 1: Create a PdfDocument instance and load the sample.pdf file.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");

Step 2: Get the labels of the pages in the PDF file.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdf.Pages.Count; i++)
{
    sb.AppendLine(pdf.Pages[i].PageLabel);
}

Step 3: Save to a .txt file.

File.WriteAllText("PageLabels.txt", sb.ToString());

Output:

Get PDF Page Labels in C#

Full code:

using System.IO;
using System.Text;
using Spire.Pdf;

namespace Get_PDF_Page_Labels
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load the PDF file
            pdf.LoadFromFile("sample.pdf");

            //Create a StringBuilder instance
            StringBuilder sb = new StringBuilder();
            //Get the lables of the pages in the PDF file
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                sb.AppendLine(pdf.Pages[i].PageLabel);
            }

            //Save to a .txt file
            File.WriteAllText("PageLabels.txt", sb.ToString());
        }
    }
}

Spire.PDF supports to horizontally and vertically split a PDF page into two or more pages. This article will show you how to use Spire.PDF to accomplish this function.

The sample PDF file:

Horizontally and Vertically Split a PDF Page into multiple Pages in C#

Detail steps:

Step 1: Load the sample PDF file and get the first page.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("New Zealand.pdf");

PdfPageBase page = pdf.Pages[0];

Step 2: Create a new PDF file and remove page margins.

PdfDocument newPdf = new PdfDocument();
newPdf.PageSettings.Margins.All = 0;

Step 3: Set page width and height in order to horizontally or vertically split the first page into 2 pages.

//Horizontally Split
newPdf.PageSettings.Width = page.Size.Width;
newPdf.PageSettings.Height = page.Size.Height / 2;
//Vertically split
//newPdf.PageSettings.Width = page.Size.Width / 2;
//newPdf.PageSettings.Height = page.Size.Height;

Step 5: Add a new page to the new PDF file.

PdfPageBase newPage = newPdf.Pages.Add();

Step 6: Create layout format.

PdfTextLayout format = new PdfTextLayout();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;

Step 7: Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format.

page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);

Step 8: Save and close.

newPdf.SaveToFile("SplitPage.pdf");
newPdf.Close();
pdf.Close();  

Horizontally split:

Horizontally and Vertically Split a PDF Page into multiple Pages in C#

Vertically split:

Horizontally and Vertically Split a PDF Page into multiple Pages in C#

Full code:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace SplitPDFPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the sample PDF
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("New Zealand.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Create a new PDF
            PdfDocument newPdf = new PdfDocument();

            //Remove page margins
            newPdf.PageSettings.Margins.All = 0;

            //Set page width and height in order to horizontally split the first page into 2 pages
            newPdf.PageSettings.Width = page.Size.Width;
            newPdf.PageSettings.Height = page.Size.Height / 2;

            //Set page width and height in order to vertically split the first page into 2 pages
            //newPdf.PageSettings.Width = page.Size.Width / 2;
            //newPdf.PageSettings.Height = page.Size.Height;

            //Add a new page to the new PDF
            PdfPageBase newPage = newPdf.Pages.Add();

            //Create layout format
            PdfTextLayout format = new PdfTextLayout();
            format.Break = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;

            //Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format
            page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);

            //Save and close
            newPdf.SaveToFile("SplitPage.pdf");
            newPdf.Close();
            pdf.Close();                                               
        }
    }
}

A tiled background usually refers to the background that is filled with one or more repetitions of a small image. In this article, you will learn how to tile an image in PDF and make a tile background for your PDFs in C# and VB.NET.

Step 1: Create a PdfDocument object and load a sample PDF document.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");

Step 2: Load an image file to PdfImage object.

PdfImage image = PdfImage.FromFile("logo.png");

Step 3: Create a PdfTilingBrush object specifying its size, set the transparency of the brush, and draw an image at the specified position of the brush.

PdfTilingBrush brush = new PdfTilingBrush(new SizeF(pdf.Pages[1].Canvas.Size.Width / 3, pdf.Pages[1].Canvas.Size.Height / 5));
brush.Graphics.SetTransparency(0.2f);
brush.Graphics.DrawImage(image,new PointF((brush.Size.Width-image.Width)/2,(brush.Size.Height-image.Height)/2));

Step 4: Draw rectangles on the PDF page using the brush.

pdf.Pages[1].Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));

Step 5: Save the file.

pdf.SaveToFile("output.pdf");

Output:

How to Add a Tiled Background Image to PDF in C#, VB.NET

Full Code:

[C#]
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");

PdfImage image = PdfImage.FromFile("logo.png");
foreach (PdfPageBase page in pdf.Pages)
{
    PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5));
    brush.Graphics.SetTransparency(0.2f);
    brush.Graphics.DrawImage(image,new PointF((brush.Size.Width-image.Width)/2,(brush.Size.Height-image.Height)/2));
    page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));
}
pdf.SaveToFile("output.pdf");
[VB.NET]
Dim pdf As PdfDocument = New PdfDocument()
pdf.LoadFromFile("sample.pdf")
Dim image As PdfImage = PdfImage.FromFile("logo.png")
For Each page As PdfPageBase In pdf.Pages
    Dim brush As PdfTilingBrush = New PdfTilingBrush(New SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5))
    brush.Graphics.SetTransparency(0.2F)
    brush.Graphics.DrawImage(image, New PointF((brush.Size.Width - image.Width) / 2, (brush.Size.Height - image.Height) / 2))
    page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.Size))
Next
pdf.SaveToFile("output.pdf")
Wednesday, 05 April 2017 07:29

Rearrange Pages in an Existing PDF Document

Written by support iceblue

Spire.PDF allows us to rearrange the pages in an existing PDF document by using the Rearrange(int[] orderArray) method. This article explains how we can use Spire.PDF to implement this function.

Below is the screenshot of the source PDF document:

Rearrange Pages in an Existing PDF Document

Refer to below detail steps:

Step 1: Load the existing PDF document.

PdfDocument doc = new PdfDocument("New Zealand.pdf");

Step 2: Rearrange pages by page index.

doc.Pages.ReArrange(new int[] { 2, 1, 0 });

Step 3: Save and close the document.

doc.SaveToFile("Output.pdf");
doc.Close();

Screenshot after rearranging pages:

Rearrange Pages in an Existing PDF Document

Full code:

using Spire.Pdf;

namespace Rearrange_page_in_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the PDF document
            PdfDocument doc = new PdfDocument("New Zealand.pdf");

            //Rearrange pages by page index
            doc.Pages.ReArrange(new int[] { 2, 1, 0 });

            //Save and close the document
            doc.SaveToFile("Output.pdf");
            doc.Close();
        }
    }
}

Page numbers of a document are helpful for readers to remember where they leave last time or which page they would like to continue with next time. Adding page numbers helps to make a PDF document well-organized, especially when the PDF document contains a huge number of pages. In this article, we will introduce how to add page numbers to an existing PDF document using Spire.PDF in C#.

Spire.PDF provides a PdfPageNubmerField class, which when added to specific page will shows the page number automatically. Follow parts show you how to add page number field to a PDF document at the specified location.

Code Snippet:

Step 1: Define a DrawPageNumber() method that takes PdfPageCollection and PdfMargins as two arguments. In this method, we create an object of PdfPageNubmerField class, set its properties, and draw the field at the center of the bottom in PDF page.

private static void DrawPageNumber(PdfPageCollection collection, PdfMargins margin)
{
    foreach (PdfPageBase page in collection)
    {
        PdfBrush brush = PdfBrushes.Black;
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
        PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);              
        int x = Convert.ToInt32(page.Canvas.ClientSize.Width / 2);
        int y = Convert.ToInt32(page.Canvas.ClientSize.Height-margin.Bottom);
        Rectangle bounds = new Rectangle(x, y, 50, 20);        
        PdfPageNumberField field = new PdfPageNumberField();
        field.Font = font;
        field.Brush = brush;
        field.StringFormat = format;
        field.Bounds = bounds;
        field.Draw(page.Canvas);
    }
}

Step 2: Create an object of PdfDocument class and load an existing PDF file to it.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

Step 3: Set the margins.

PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;

Step 4: Call DrawPageNumber() to add page numbers to the PDF document.

DrawPageNumber(doc.Pages, margin);

Step 5: Save the file.

doc.SaveToFile("AddNumber.pdf", FileFormat.PDF);

Output:

How to Add Page Numbers to Existing PDF Document in C#

Full Code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using Spire.Pdf.Widget;
using System.Drawing;
using Spire.Pdf.AutomaticFields;

namespace AddPageNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
            //set the margins
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            //draw page number
            DrawPageNumber(doc.Pages, margin);
            //save the file
            doc.SaveToFile("AddPageNumbers.pdf", FileFormat.PDF);
        }
        private static void DrawPageNumber(PdfPageCollection collection, PdfMargins margin)
        {
            foreach (PdfPageBase page in collection)
            {
                PdfBrush brush = PdfBrushes.Black;
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);              
                int x = Convert.ToInt32(page.Canvas.ClientSize.Width / 2);
                int y = Convert.ToInt32(page.Canvas.ClientSize.Height-margin.Bottom);
                Rectangle bounds = new Rectangle(x, y, 50, 20);        
                PdfPageNumberField field = new PdfPageNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }
        }
    }
}

Page transitions give visual sparkle by creating a "PowerPoint-like" effect when switching between pages. Page transitions are useful when you create a slideshow in PDF format. This article will introduce how to add page transition effects to an existing PDF document using Spire.PDF.

Code Snippet:

Step 1: Initialize an object of PdfDocument class and load the sample PDF which you want to add transition effects to.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

Step 2: Create a new PDF document, add sections to it based on the page count of source file and transfer every individual page to a section. Spire.PDF provides PageSettings property in PdfSection class for setting transition effects, that's reason why we should add pages to sections within a new PDF document.

PdfDocument newDoc = new PdfDocument();
for (int i = 0; i < doc.Pages.Count; i++)
{
    PdfSection secetion = newDoc.Sections.Add();
    PdfPageBase page = doc.Pages[i];
    PdfTemplate template = page.CreateTemplate();
    PdfNewPage newpage = secetion.Pages.Add();
    newpage.Canvas.DrawTemplate(template, new PointF(0 - doc.PageSettings.Margins.Left, 0- doc.PageSettings.Margins.Top));
}

Step 3: As demonstrated above, we know that each section in the new document contains a page. Then we could set transition effect for each page through PageSettings.Transition.

PdfSection secetion1 = newDoc.Sections[0];
secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover;
secetion1.PageSettings.Transition.Duration = 3;
secetion1.PageSettings.Transition.PageDuration = 2;

PdfSection secetion2 = newDoc.Sections[1];
secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter;
secetion2.PageSettings.Transition.Duration = 3;
secetion2.PageSettings.Transition.PageDuration = 2;

PdfSection secetion3 = newDoc.Sections[2];
secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
secetion3.PageSettings.Transition.Duration = 3;
secetion3.PageSettings.Transition.PageDuration = 2;

Step 4: Save and launch the file.

newDoc.SaveToFile("Transition.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("Transition.pdf");

Output:

How to set page transitions for existing PDF document in C#, VB.NET

Full Code:

[C#]
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

PdfDocument newDoc = new PdfDocument();
for (int i = 0; i < doc.Pages.Count; i++)
{
    PdfSection secetion = newDoc.Sections.Add();
    PdfPageBase page = doc.Pages[i];
    PdfTemplate template = page.CreateTemplate();
    PdfNewPage newpage = secetion.Pages.Add();
    newpage.Canvas.DrawTemplate(template, new PointF(0 - doc.PageSettings.Margins.Left, 0- doc.PageSettings.Margins.Top));
}

PdfSection secetion1 = newDoc.Sections[0];
secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover;
secetion1.PageSettings.Transition.Duration = 3;
secetion1.PageSettings.Transition.PageDuration = 2;

PdfSection secetion2 = newDoc.Sections[1];
secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter;
secetion2.PageSettings.Transition.Duration = 3;
secetion2.PageSettings.Transition.PageDuration = 2;

PdfSection secetion3 = newDoc.Sections[2];
secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
secetion3.PageSettings.Transition.Duration = 3;
secetion3.PageSettings.Transition.PageDuration = 2;

newDoc.SaveToFile("Transition.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("Transition.pdf");
[VB.NET]
Dim doc As New PdfDocument()
doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

Dim newDoc As New PdfDocument()
For i As Integer = 0 To doc.Pages.Count - 1
	Dim secetion As PdfSection = newDoc.Sections.Add()

    Dim page As PdfPageBase = doc.Pages(i)

    Dim template As PdfTemplate = page.CreateTemplate()

    Dim newpage As PdfNewPage = secetion.Pages.Add()

    newpage.Canvas.DrawTemplate(template, New PointF(0 - doc.PageSettings.Margins.Left, 0 - doc.PageSettings.Margins.Top))
Next

Dim secetion1 As PdfSection = newDoc.Sections(0)
secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover
secetion1.PageSettings.Transition.Duration = 3
secetion1.PageSettings.Transition.PageDuration = 2

Dim secetion2 As PdfSection = newDoc.Sections(1)
secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter
secetion2.PageSettings.Transition.Duration = 3
secetion2.PageSettings.Transition.PageDuration = 2

Dim secetion3 As PdfSection = newDoc.Sections(2)
secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade
secetion3.PageSettings.Transition.Duration = 3
secetion3.PageSettings.Transition.PageDuration = 2

newDoc.SaveToFile("Transition.pdf", FileFormat.PDF)
System.Diagnostics.Process.Start("Transition.pdf")

PDF margins are white spaces between body contents and page edge. Unlike Word, margins in PDF document are not easy to be modified as Adobe does not provide any functionality for users to manipulate margins freely. However, you can change the page scaling (enlarge/compress content) or crop page to get fitted margins. In this article, you will learn how to enlarge PDF margins by compressing content.

Step 1: Create a PdfDocument object to load the original PDF document.

PdfDocument origDoc = new PdfDocument();
origDoc.LoadFromFile("sample.pdf");

Step 2: Create another PdfDocument object.

PdfDocument destDoc = new PdfDocument();

Step 3: Set the increments that you want to add to the margins in the existing PDF document.

float top = 50;
float bottom = 50;
float left = 50;
float right = 50;

Step 4: Transfer the compressed content from the original document to the new PDF document.

foreach (PdfPageBase page in origDoc.Pages)
{
    PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0));
    newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width,
                        (page.ActualSize.Height - top - bottom) / page.ActualSize.Height);
    newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top));
}

Step 5: Save to file.

destDoc.SaveToFile("result.pdf", FileFormat.PDF);

Original PDF:

How to Enlarge PDF Margins without Changing Page Size in C#, VB.NET

Result:

How to Enlarge PDF Margins without Changing Page Size in C#, VB.NET

Full Code:

[C#]
PdfDocument origDoc = new PdfDocument();
origDoc.LoadFromFile("sample.pdf");
PdfDocument destDoc = new PdfDocument();

float top = 50;
float bottom = 50;
float left = 50;
float right = 50;

foreach (PdfPageBase page in origDoc.Pages)
{
    PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0));
    newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width,
                        (page.ActualSize.Height - top - bottom) / page.ActualSize.Height);
    newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top));
}

destDoc.SaveToFile("result.pdf", FileFormat.PDF);
[VB.NET]
Dim origDoc As PdfDocument = New PdfDocument()
origDoc.LoadFromFile("sample.pdf")
Dim destDoc As PdfDocument = New PdfDocument()
Dim top As Single = 50
Dim bottom As Single = 50
Dim left As Single = 50
Dim right As Single = 50

For Each page As PdfPageBase In origDoc.Pages
    Dim newPage As PdfPageBase = destDoc.Pages.Add(page.Size, New PdfMargins(0))
    newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height)
    newPage.Canvas.DrawTemplate(page.CreateTemplate(), New PointF(left, top))
Next

destDoc.SaveToFile("result.pdf", FileFormat.PDF)

With the help of Spire.PDF, we can easily set the custom properties for PDF files. Sometimes, we may need to get the custom properties and update some information on it. This article will show you how to get and remove the value of custom properties area for PDF file in C#.

Spire.PDF provides a DocumentInformation class and we can set PDF custom properties through setting properties of DocumentInformation.SetCustomerDefined. We can also get the custom properties via DocumentInformation.GetCustomerDefined and remove the custom properties via DocumentInformation.RemoveCustomerDefined.

Here comes to the code snippet of how to get and remove the value of custom properties for PDF file in C#. Firstly, view the value of custom properties area for the PDF file:

Get and remove the value of custom properties for PDF file in C#

Step 1: Create a new PDF document and load from the file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");

Step 2: Get the value at the custom properties area of PDF file.

//Get the value for Name at the custom property area of pdf document information
string name = doc.DocumentInformation.GetCustomerDefined("Name");
//Get the value for Number at the custom property area of pdf document information
string number = doc.DocumentInformation.GetCustomerDefined("Number");

Step 3: Remove the value for Name from the custom property area of PDF file.

doc.DocumentInformation.RemoveCustomerDefined("Name");

Step 4: Save the document to file and launch to preview it.

doc.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");

Effective screenshot after remove the Name value from the custom properties area for PDF file:

Get and remove the value of custom properties for PDF file in C#

Full codes:

static void Main(string[] args)

 {
     PdfDocument doc = new PdfDocument();
     doc.LoadFromFile("sample.pdf");

     string name = doc.DocumentInformation.GetCustomerDefined("Name");
     string number = doc.DocumentInformation.GetCustomerDefined("Number");

     doc.DocumentInformation.RemoveCustomerDefined("Name");
     doc.SaveToFile("result.pdf");
 }
Wednesday, 14 October 2015 08:42

How to rotate the PDF file in C#

Written by support iceblue

With the help of Spire.PDF for .NET, developers can easily rotate the PDF in C# and VB.NET. We have already demonstrated how to set the rotation for the newly added PDF files via setting of section.PageSettings.Rotate. Spire.PDF also supports to rotate the existing PDF files. When we need to rotate an existing PDF file, usually it has already rotated with some angle. This article will show you to how to rotate an existing PDF page in C#.

Please check the original PDF file already with 90 rotation angle:

How to rotate the PDF file in C#

Here comes to the code snippet of how to rotate an existing PDF file in C#.

Step 1: Create a new instance of PdfDocument and load a PDF document from file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Sample.pdf");

Step 2: Get the first page of the loaded PDF file.

PdfPageBase page = doc.Pages[0];

Step 3: Rotate the PDF page based on the original rotation angle.

int rotation = (int)page.Rotation;
rotation += (int)PdfPageRotateAngle.RotateAngle270;
page.Rotation = (PdfPageRotateAngle)rotation;

Step 4: Save the document to file.

doc.SaveToFile("Result.pdf");

The effective screenshot of the PDF page which rotated 270 rotation angle:

How to rotate the PDF file in C#

Full codes:

{
    PdfDocument pdf = new PdfDocument();
    pdf.LoadFromFile("Sample.PDF");

    PdfPageBase page = pdf.Pages[0];

    int rotation = (int)page.Rotation;
    rotation += (int)PdfPageRotateAngle.RotateAngle270;
    page.Rotation = (PdfPageRotateAngle)rotation;
    pdf.SaveToFile("Result.pdf");
}
Page 1 of 2