How to Add 20 Percent Margins to PDF Pages using C#

This example demonstrates how to add 20% margins to all sides of PDF pages using Aspose.PDF for .NET. This is useful when preparing documents for printing or compliance requirements that mandate specific whitespace around content. The operation scales margins relative to page dimensions, ensuring consistent spacing regardless of page size.

Steps to Add 20 Percent Margins to PDF Pages using C#

  1. Verify the input PDF file exists to prevent runtime errors.
  2. Create an instance of the PdfFileEditor class from Aspose.Pdf.Facades.
  3. Call the AddMarginsPct method with source and destination paths, specifying 20% for left, right, top, and bottom margins.
  4. Pass null for the pages parameter to apply margins to all pages in the document.
  5. Check the boolean return value to confirm whether the operation succeeded.

The code uses percentage-based margins, meaning the added whitespace scales with the page size—20% of the page width for horizontal margins and 20% of the page height for vertical margins. This approach ensures uniform spacing across documents with mixed page sizes.

Code to Add 20 Percent Margins to PDF Pages using C#

The example successfully demonstrates how to programmatically pad PDF pages with 20% margins on all sides using Aspose.PDF for .NET, preserving the original content while adding required whitespace.