SelectPdf for .NET 10 - Pdf Resize/Scale Content - C# / .NET 10 Sample

This sample 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 sample uses the following (existing) test PDF:
Test PDF document

The file has A4 pages, Portrait orientation, 5pt margins.

Resize settings
The new file will have the following settings:
(Note: Margins bellow are added to the existing margins. Alignments are only taken into account if the scaling is allowed and the aspect ratio is preserved.)

Pdf Page Size:


Pdf Page Orientation:

Top Margin:
pt

Right Margin:
pt

Allow Scaling
Bottom Margin:
pt

Left Margin:
pt

Keep Aspect Ratio
Horizontal Alignment:


Vertical Alignment:


Sample code · C#
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class PdfResizeModel : PageModel
    {
        public void OnGet()
        {
            DdlPageSize = "A4";
            DdlHorizontalAlign = "Center";
            DdlVerticalAlign = "Middle";
        }

        [BindProperty]
        public string DdlPageSize { get; set; }

        public List<SelectListItem> PageSizes { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "A1", Text = "A1" },
            new SelectListItem { Value = "A2", Text = "A2" },
            new SelectListItem { Value = "A3", Text = "A3" },
            new SelectListItem { Value = "A4", Text = "A4" },
            new SelectListItem { Value = "A5", Text = "A5" },
            new SelectListItem { Value = "Letter", Text = "Letter" },
            new SelectListItem { Value = "HalfLetter", Text = "HalfLetter" },
            new SelectListItem { Value = "Ledger", Text = "Ledger" },
            new SelectListItem { Value = "Legal", Text = "Legal" },
        };

        [BindProperty]
        public string DdlPageOrientation { get; set; }

        public List<SelectListItem> PageOrientations { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Portrait", Text = "Portrait" },
            new SelectListItem { Value = "Landscape", Text = "Landscape" },
        };

        [BindProperty]
        public int TxtTopMargin { get; set; }

        [BindProperty]
        public int TxtRightMargin { get; set; }

        [BindProperty]
        public int TxtBottomMargin { get; set; }

        [BindProperty]
        public int TxtLeftMargin { get; set; }

        [BindProperty]
        public bool ChkAllowScaling { get; set; }

        [BindProperty]
        public bool ChkKeepAspectRatio { get; set; }

        [BindProperty]
        public string DdlHorizontalAlign { get; set; }

        public List<SelectListItem> HorizontalAligns { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Left", Text = "Left" },
            new SelectListItem { Value = "Center", Text = "Center" },
            new SelectListItem { Value = "Right", Text = "Right" },
        };

        [BindProperty]
        public string DdlVerticalAlign { get; set; }

        public List<SelectListItem> VerticalAligns { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Top", Text = "Top" },
            new SelectListItem { Value = "Middle", Text = "Middle" },
            new SelectListItem { Value = "Bottom", Text = "Bottom" },
        };


        public IActionResult OnPost()
        {
            // the test file
            string filePdf = Path.Combine(Startup.ServerPath, "wwwroot", "files", "selectpdf.pdf");

            // settings
            PdfPageSize pdfPageSize = SampleForm.ParseEnum(DdlPageSize, PdfPageSize.A4);

            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;
            }

            PdfPageOrientation pdfOrientation =
                SampleForm.ParseEnum(DdlPageOrientation, PdfPageOrientation.Portrait);

            PdfHorizontalAlign pdfHorizontalAlign =
                SampleForm.ParseEnum(DdlHorizontalAlign, PdfHorizontalAlign.Center);

            PdfVerticalAlign pdfVerticalAlign =
                SampleForm.ParseEnum(DdlVerticalAlign, PdfVerticalAlign.Middle);

            // resize the content
            PdfResizeManager resizer = new PdfResizeManager();
            resizer.Load(filePdf);

            resizer.PageMargins = new PdfMargins(
                TxtLeftMargin, TxtRightMargin, TxtTopMargin, TxtBottomMargin);
            resizer.PageSize = pageSize;
            resizer.PageOrientation = pdfOrientation;
            resizer.AllowScale = ChkAllowScaling;
            resizer.KeepAspectRatio = ChkKeepAspectRatio;
            resizer.HorizontalAlign = pdfHorizontalAlign;
            resizer.VerticalAlign = pdfVerticalAlign;

            // save pdf document
            byte[] pdf = resizer.Save();

            // close pdf document
            resizer.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;

        }
    }
}