How to resize WinForms Pdf Page from an existing using C# and VB.NET?
Syncfusion Essential® PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, form, compress, and secure PDF files. You can resize the pdf page from the existing pdf document using this library.
Steps to resize a Pdf Page from an existing PDF document:
1. Create a new C# console application project.
2. Install the Syncfusion.PDF.WinForms NuGet packages as a reference to your .NET Framework application from NuGet.org.
3. Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Parsing
4. The following code example shows how to resize a Pdf Page from an existing PDF document using C#.
C#
string inputFilePath = @"../../Data/sample.pdf"; string outputFilePath = @"../../Data/output.pdf"; byte[] data = System.IO.File.ReadAllBytes(inputFilePath); //Load a PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(data); //Create a PDF document. PdfDocument document = new PdfDocument(); //Set the Left Margin for a PdfDocument page. document.PageSettings.Margins.Left = 50; //Set the Right margin for a PdfDocument page. document.PageSettings.Margins.Right = 50; //Set the Top margin for a PdfDocument page. document.PageSettings.Margins.Top = 0; //Set the Bottom margin for a PdfDocument page. document.PageSettings.Margins.Bottom = 0; for (int i = 0; i < loadedDocument.Pages.Count; i++) { //Get the graphics client size. SizeF size = loadedDocument.Pages[i].Graphics.ClientSize; //Create the template from the page. PdfTemplate template = loadedDocument.Pages[i].CreateTemplate(); // Set page orientation based on the existing size (by default Portrait). if (size.Width > size.Height) document.PageSettings.Orientation = PdfPageOrientation.Landscape; //Set the existing page rotation to the current page. document.PageSettings.Rotate = loadedDocument.Pages[0].Rotation; //Add a page. PdfPage page = document.Pages.Add(); // Create Pdf graphics for a page. PdfGraphics graphics = page.Graphics; //Draw a template. graphics.DrawPdfTemplate(template,new PointF(0,0), new SizeF(page.GetClientSize().Width, page.GetClientSize().Height)); } //Creating a stream object. MemoryStream stream = new MemoryStream(); //Save a document into a stream. document.Save(stream); //Saves a document to disk. System.IO.File.WriteAllBytes(outputFilePath, stream.ToArray());
VB.NET
Dim inputFilePath As String = "../../Data/sample.pdf" Dim outputFilePath As String = "../../Data/output.pdf" Dim data As Byte() = System.IO.File.ReadAllBytes(inputFilePath) ‘Load a PDF document. Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(data) ‘Create a PDF document. Dim document As PdfDocument = New PdfDocument() ‘Set the Left Margin for a PdfDocument page. document.PageSettings.Margins.Left = 50 ‘Set the Right Margin for a PdfDocument page. document.PageSettings.Margins.Right = 50 ‘Set the Top Margin for a PdfDocument page. document.PageSettings.Margins.Top = 0 ‘Set the Bottom Margin for a PdfDocument page. document.PageSettings.Margins.Bottom = 0 For i As Integer = 0 To loadedDocument.Pages.Count - 1 ‘Get the graphics client size. Dim size As SizeF = loadedDocument.Pages(i).Graphics.ClientSize ‘Create a template from a page. Dim template As PdfTemplate = loadedDocument.Pages(i).CreateTemplate() ‘Set page orientation based on the existing size (by default Portrait). If size.Width > size.Height Then document.PageSettings.Orientation = PdfPageOrientation.Landscape ‘Set the existing page rotation to the current page. document.PageSettings.Rotate = loadedDocument.Pages(0).Rotation ‘Add a page. Dim page As PdfPage = document.Pages.Add() ‘Create Pdf graphics for a page. Dim graphics As PdfGraphics = page.Graphics ‘Draw a template. graphics.DrawPdfTemplate(template, New PointF(0, 0), New SizeF(page.GetClientSize().Width, page.GetClientSize().Height)) Next ‘Creating a stream object. Dim stream As MemoryStream = New MemoryStream() ‘Save a document into a stream. document.Save(stream) ‘Saves a document to disk. System.IO.File.WriteAllBytes(outputFilePath, stream.ToArray())
A complete working sample can be downloaded from PdfPageResizeSample.Zip.
By executing the program, you will get the PDF document as follows.
Refer to this link to explore a rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.