Resize/Scale The Content of an Existing Pdf | |
SelectPdf PDF Library for .NET can be used to modify existing PDF documents and resize the content of the pages (change PDF page size, change PDF page orientation, add margins to the PDF pages).
The PDF resize or scaling can be done using PdfResizeManager. PdfResizeManager exposes the following methods:
Load - can be used to load the existing PDF document, password protected or not, from a file, Stream, byte array or PdfDocument object.
Save - can be used to save the modified PDF document to a file, Stream, byte array or directly into the browser's response Stream (for web applications).
GetDocument - returns the modified PDF document as a PdfDocument object.
PdfResizeManager exposes the following properties that can be used to customize the way content is resized/scaled:
PageSize - The generated document (resized) page size. The default value is A4.
PageOrientation - The generated document (resized) page orientation. The default value is Portrait.
PageMargins - The generated document (resized) page margins. The default value is 0 for all margins. Important: Margins specified are added to the existing margins.
AllowScale - Specifies if the content can be scaled (resized) in the generated document (resized).
KeepAspectRatio - Specifies if the content aspect ratio (proportion) is preserved in the generated document (resized).
HorizontalAlign - Specifies how the content is horizontally aligned in the destination rectangle, when the aspect ratio is preserved. This is taken into consideration only if the content is rendered at (0,0) in the client rectangle, scaling is allowed and aspect ratio is preserved.
VerticalAlign - Specifies how the content is vertically aligned in the destination rectangle, when the aspect ratio is preserved. This is taken into consideration only if the content is rendered at (0,0) in the client rectangle, scaling is allowed and aspect ratio is preserved.
StartPosition - The start position of the content in the generated document (resized). By default, this is (0,0) in the client rectangle.
ContentSize - The size of the content in the generated document (resized). By default, this is the client rectange, minus page margins.
KeepOriginalPageSettings - When true, the original per-page size and orientation of the input document are preserved, and only the other resize properties (margins, alignment, etc.) are applied. Useful when the input mixes page sizes (e.g. an A4 body with A3 foldouts) and a single uniform output size would distort some pages. The default value is false.
ViewerPreferences - The viewer preferences of the generated document. They start as the preferences of the loaded document and can be changed before saving.
Security - The security settings (passwords and permissions) of the generated document. They start as the settings of the loaded document. They are applied when KeepOriginalPageSettings is true or when any of them is changed; otherwise the generated document is not encrypted.
Resizing reflows the same document, so the generated document keeps what belongs to it: links and internal link destinations, bookmarks, document information, viewer preferences, attachments, named destinations, the open action, page labels and the XMP metadata. Form fields are carried and stay fillable. A document that conforms to PDF/A, PDF/UA or PDF/X-1a is produced to the same standards, with its logical structure carried onto the resized pages; a document that declares an accessibility standard it does not meet is produced without that declaration.
The code sample below shows how to use SelectPdf Library for .NET to resize the pages of an existing PDF document, add margins, change page orientation. That can be done keeping the aspect ratio of the content or not, allowing scaling or not.
// the test file string filePdf = @"C:\Path\to\document.pdf"; // settings string pdf_page_size = pageSizeValue; PdfPageSize pdfPageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true); PdfCustomPageSize pageSize = PdfCustomPageSize.A4; switch (pdfPageSize) { case PdfPageSize.A1: pageSize = PdfCustomPageSize.A1; break; case PdfPageSize.A2: pageSize = PdfCustomPageSize.A2; break; case PdfPageSize.A3: pageSize = PdfCustomPageSize.A3; break; case PdfPageSize.A5: pageSize = PdfCustomPageSize.A5; break; case PdfPageSize.Letter: pageSize = PdfCustomPageSize.Letter; break; case PdfPageSize.HalfLetter: pageSize = PdfCustomPageSize.HalfLetter; break; case PdfPageSize.Ledger: pageSize = PdfCustomPageSize.Ledger; break; case PdfPageSize.Legal: pageSize = PdfCustomPageSize.Legal; break; } string pdf_orientation = pageOrientationValue; PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), pdf_orientation, true); int topMargin = 0; try { topMargin = Convert.ToInt32(topMarginValue); } catch { } int rightMargin = 0; try { rightMargin = Convert.ToInt32(rightMarginValue); } catch { } int bottomMargin = 0; try { bottomMargin = Convert.ToInt32(bottomMarginValue); } catch { } int leftMargin = 0; try { leftMargin = Convert.ToInt32(leftMarginValue); } catch { } string horizontal_align = horizontalAlignValue; PdfHorizontalAlign pdfHorizontalAlign = (PdfHorizontalAlign)Enum.Parse(typeof(PdfHorizontalAlign), horizontal_align, true); string vertical_align = verticalAlignValue; PdfVerticalAlign pdfVerticalAlign = (PdfVerticalAlign)Enum.Parse(typeof(PdfVerticalAlign), vertical_align, true); // resize the content PdfResizeManager resizer = new PdfResizeManager(); resizer.Load(filePdf); resizer.PageMargins = new PdfMargins( leftMargin, rightMargin, topMargin, bottomMargin); resizer.PageSize = pageSize; resizer.PageOrientation = pdfOrientation; resizer.AllowScale = allowScaling; resizer.KeepAspectRatio = keepAspectRatio; resizer.HorizontalAlign = pdfHorizontalAlign; resizer.VerticalAlign = pdfVerticalAlign; // save pdf document resizer.Save("Sample.pdf"); // close pdf document resizer.Close();