Click or drag to resize
Pdf Library for .NET

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.

Code Sample

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.

protected void BtnCreatePdf_Click(object sender, EventArgs e)
{
    // the test file
    string filePdf = Server.MapPath("~/files/selectpdf.pdf");

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

    int topMargin = 0;
    try
    {
        topMargin = Convert.ToInt32(TxtTopMargin.Text);
    }
    catch { }

    int rightMargin = 0;
    try
    {
        rightMargin = Convert.ToInt32(TxtRightMargin.Text);
    }
    catch { }

    int bottomMargin = 0;
    try
    {
        bottomMargin = Convert.ToInt32(TxtBottomMargin.Text);
    }
    catch { }

    int leftMargin = 0;
    try
    {
        leftMargin = Convert.ToInt32(TxtLeftMargin.Text);
    }
    catch { }

    string horizontal_align = DdlHorizontalAlign.SelectedValue;
    PdfHorizontalAlign pdfHorizontalAlign =
        (PdfHorizontalAlign)Enum.Parse(typeof(PdfHorizontalAlign),
        horizontal_align, true);

    string vertical_align = DdlVerticalAlign.SelectedValue;
    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 = ChkAllowScaling.Checked;
    resizer.KeepAspectRatio = ChkKeepAspectRatio.Checked;
    resizer.HorizontalAlign = pdfHorizontalAlign;
    resizer.VerticalAlign = pdfVerticalAlign;

    // save pdf document
    resizer.Save(Response, false, "Sample.pdf");

    // close pdf document
    resizer.Close();
}
See Also