SelectPdf for .NET - Pdf Resize/Scale Content - C# / ASP.NET MVC 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:





Sample Code C#



using System;
using System.Web.Mvc;

namespace SelectPdf.Samples.Controllers
{
    public class PdfResizeController : Controller
    {
        // GET: /PdfResize/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubmitAction(FormCollection collection)
        {
            // the test file
            string filePdf = Server.MapPath("~/files/selectpdf.pdf");

            // settings
            string pdf_page_size = collection["DdlPageSize"];
            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 = collection["DdlPageOrientation"];
            PdfPageOrientation pdfOrientation =
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                pdf_orientation, true);

            int topMargin = 0;
            try
            {
                topMargin = Int32.Parse(collection["TxtTopMargin"]);
            }
            catch { }

            int rightMargin = 0;
            try
            {
                rightMargin = Int32.Parse(collection["TxtRightMargin"]);
            }
            catch { }

            int bottomMargin = 0;
            try
            {
                bottomMargin = Int32.Parse(collection["TxtBottomMargin"]);
            }
            catch { }

            int leftMargin = 0;
            try
            {
                leftMargin = Int32.Parse(collection["TxtLeftMargin"]);
            }
            catch { }

            string horizontal_align = collection["DdlHorizontalAlign"];
            PdfHorizontalAlign pdfHorizontalAlign =
                (PdfHorizontalAlign)Enum.Parse(typeof(PdfHorizontalAlign),
                horizontal_align, true);

            string vertical_align = collection["DdlVerticalAlign"];
            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 = collection["ChkAllowScaling"] == "on";
            resizer.KeepAspectRatio = collection["ChkKeepAspectRatio"] == "on";
            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;
        }
    }
}