Skip to main content
Skip table of contents

Changing page sizes of existing PDFs

This example was written in answer to the question Click How to extend the page size of a PDF to add a watermark?


addextramargin

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;

import java.io.File;

public class AddExtraMargin {
    public static final String DEST = "./target/sandbox/stamper/add_extra_margin.pdf";
    public static final String SRC = "./src/main/resources/pdfs/primes.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddExtraMargin().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        // Loop over every page
        for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
            PdfDictionary pageDict = pdfDoc.getPage(i).getPdfObject();
            PdfArray mediaBox = pageDict.getAsArray(PdfName.MediaBox);
            float llx = mediaBox.getAsNumber(0).floatValue();
            float lly = mediaBox.getAsNumber(1).floatValue();
            float ury = mediaBox.getAsNumber(3).floatValue();
            mediaBox.set(0, new PdfNumber(llx - 36));
            PdfCanvas over = new PdfCanvas(pdfDoc.getPage(i));
            over.saveState();
            over.setFillColor(new DeviceGray(0.5f));
            over.rectangle(llx - 36, lly, 36, ury - llx);
            over.fill();
            over.restoreState();
        }

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;

namespace iText.Samples.Sandbox.Stamper 
{
    public class AddExtraMargin 
    {
        public static readonly String DEST = "results/sandbox/stamper/add_extra_margin.pdf";
        public static readonly String SRC = "../../../resources/pdfs/primes.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new AddExtraMargin().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            
            // Loop over every page
            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) {
                PdfDictionary pageDict = pdfDoc.GetPage(i).GetPdfObject();
                PdfArray mediaBox = pageDict.GetAsArray(PdfName.MediaBox);
                float llx = mediaBox.GetAsNumber(0).FloatValue();
                float lly = mediaBox.GetAsNumber(1).FloatValue();
                float ury = mediaBox.GetAsNumber(3).FloatValue();
                mediaBox.Set(0, new PdfNumber(llx - 36));
                PdfCanvas over = new PdfCanvas(pdfDoc.GetPage(i));
                over.SaveState();
                over.SetFillColor(new DeviceGray(0.5f));
                over.Rectangle(llx - 36, lly, 36, ury - llx);
                over.Fill();
                over.RestoreState();
            }
            
            pdfDoc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.