1024programmer Asp.Net How to adjust the page margins of PDF documents through C#/VB.NET code

How to adjust the page margins of PDF documents through C#/VB.NET code

How to adjust the page margins of PDF documents through C#/VB.NET code

PDF margins are the distance between the main content area of ​​the page and the edge of the page. Unlike Word margins, PDF document margins are difficult to change. Because Adobe does not provide a direct method to manipulate margins. However, you can change the margins by scaling the page content. This article will introduce how to use C#/VB.NET code to adjust the margins of a PDF document without changing the page size.

  • Increase the page margins of PDF documents
  • Shorten the margins of PDF documents

Increase the page margins of PDF documents

The way to expand the margins of a PDF document is to create a new PDF with a larger page size, and then draw the source page at a suitable position on the large page. Here are the steps to increase the margins of a PDF document.

  • Initialize the PdfDocument object.
  • Create another PdfDocument object, which is used to create a new PDF document with a larger page size.
  • Set the margin increment.
  • Calculate the page size of the new PDF document.
  • Loop through the pages in the original document and use the PdfPageBase.CreateTemplate() method to create a template based on a specific page.
  • Use the PdfDocument.Pages.Add() method to add pages to a new PDF document.
  • Use the PdfTemplate.Draw() method to draw the template at coordinates (0, 0) on the page.
  • Use the PdfDocument.SaveToFile() method to save a new PDF document to a file.

Complete code

C#

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

 namespace IncreaseMargins
 {
     class Program
     {
         static void  span> Main(string[] args  )
         {
             //Load the original  PDF document
             PdfDocument originalPdf = new PdfDocument("How are polar day and polar night formed.pdf");

             //Get the  page
             PdfPageBase firstPage = originalPdf.Pages[0  ];

             //Create new  PdfDocument object
             PdfDocument newPdf = new PdfDocument();

             //Set edge  Increased value of distance
             PdfMargins margins = newPdf.PageSettings.Margins;
             margins.Top = 40;
             margins.Bottom = 40;
             margins.Left = 40;
             margins.Right = 40;

             //Calculate new  Page size
             SizeF sizeF = new SizeF(firstPage.Size.Width + margins.Left + margins.Right, firstPage.Size.Height + margins  .Top + margins.Bottom);

             //Loop traversal  Page from original document
             for (int i  = 0; i < originalPdf.Pages.Count; i++)
             {
                 //Based on specific  Page creation template
                 PdfTemplate pdfTemplate = originalPdf.Pages[i].C1)">;
             float right = -  20;
             float top = -  20;
             float bottom = -  20;

             //Calculate new  Page size
             SizeF sizeF = new SizeF(firstPage.Size.Width + left + right, firstPage.Size.Height + top + bottom);

             //Loop traversal  Page from original document
             for (int i  = 0; i < originalPdf.Pages.Count; i++)
             {
                 //Based on specific  Page creation template
                 PdfTemplate pdfTemplate = originalPdf.Pages[i].CreateTemplate();

                 //Change the page  Add to new PDF
                 PdfPageBase page = newPdf.Pages.Add(sizeF, new PdfMargins(0));

                 //On the page  Draw template
  pdfTemplate.Draw(page, left, top);
             }

             //Save new  Documentation
             newPdf.SaveToFile("Reduce page margins  Distance.pdf"  , FileFormat.PDF);
         }
     }
 }

VB.NET

Imports Spire.Pdf
 Imports Spire.Pdf  .Graphics
 Imports System.Drawing

 Namespace DecreaseMargins
     Friend Class  span> Program
         Private Shared  span> Sub Main(ByVal args As String())
             'Load original PDF  Documentation
             Dim originalPdf As PdfDocument  = New PdfDocument("How are polar day and polar night formed.pdf")

             'Get first  page
             Dim firstPage As PdfPageBase  = originalPdf.Pages(0)

             'Create new  PdfDocument object
             Dim newPdf As PdfDocument  = New PdfDocument()

             'Set margins  The reduction value
             Dim left As Single = -20
             Dim right As Single = -20
             Dim top As Single = -20
             Dim bottom As Single = -20

             'Calculate new page size
             Dim sizeF As SizeF  = New SizeF(firstPage.Size.Width + left + right, firstPage.Size.Height + top +bottom)

             'Loop through the original  Pages in the document
             For i As Integer = 0 To originalPdf.Pages.Count - 1  span>
                 'Create a template based on a specific page  span>
                 Dim pdfTemplate As PdfTemplate  = originalPdf.Pages(i).CreateTemplate()

                 'Add the page  to new PDF
                 Dim page As PdfPageBase  = newPdf.Pages.Add(sizeF, New PdfMargins(0))

                 'on the page  Draw template
                 pdfTemplate.Draw(page, left,  top)
             Next

             'Save new document
             newPdf.SaveToFile("Reduce page margins  Distance.pdf"  , FileFormat.PDF)
         End Sub
     End Class
 End Namespace

Renderings

—End of article—

 

1)”>Set the margin reduction value
Dim left As Single = –20
Dim right As Single = –20
Dim top As Single = –20
Dim bottom As Single = –20

Calculate new page size
Dim sizeF As SizeF = New SizeF(firstPage.Size.Width + left + right, firstPage.Size.Height + top +bottom)

Loop through the original Pages in the document
For i As Integer = 0 To originalPdf.Pages.Count – 1 span>
Create a template based on a specific page span>
Dim pdfTemplate As PdfTemplate = originalPdf.Pages(i).CreateTemplate()

Add the page to new PDF
Dim page As PdfPageBase = newPdf.Pages.Add(sizeF, New PdfMargins(0))

on the page Draw template
pdfTemplate.Draw(page, left, top)
Next

Save new document
newPdf.SaveToFile(Reduce page margins Distance.pdf , FileFormat.PDF)
End Sub
End Class
End Namespace

Renderings

—End of article—

 
This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-to-adjust-the-page-margins-of-pdf-documents-through-c-vb-net-code/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索