Techidiot
Techidiot

Reputation: 1947

Setting new page size to existing PDF document using Java IText

I want to set a new page size to my existing PDF document without cropping the contents. I am writing the following code but it just crops my PDF file from the bottom resulting in the loss of content.

The current size is 8.26" X 11.69 " and I need to make it 8.5" X 11".

My code only converts 11.59 to 11. Tried to change 8.26 but its not woking.

Can any one please help? I am using itextpdf-5.5.8 with Java.

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfRectangle;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



public class PageSize {

    public static final String SRC = "C:/Temp/BC.pdf";
    public static final String DEST = "C:/Temp/BC_New.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new PageSize().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        // properties
        PdfContentByte over;
        PdfDictionary pageDict;
        PdfRectangle rect = new PdfRectangle(55, 76, 560, 816);
        PdfArray mediabox;
        float llx, lly, ury,llz;
        // loop over every page
        for (int i = 1; i <= n; i++) {
            pageDict = reader.getPageN(i);
            mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
            llx = mediabox.getAsNumber(0).floatValue();
            lly = mediabox.getAsNumber(1).floatValue();
            llz = mediabox.getAsNumber(2).floatValue();
            ury = mediabox.getAsNumber(3).floatValue();
            mediabox.set(1, new PdfNumber((lly + 50)));
            over = stamper.getOverContent(i);
            over.saveState();
                       over.restoreState();
        }



        stamper.close();
        reader.close();
    }
}

UPDATED Here is the code I used to reduce 11.69" to 11". It works fine. But, it won't increase the width 8.26 to 8.5"

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfRectangle;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



public class PageSize {

    public static final String SRC = "C:/Temp/Test.pdf";
    public static final String DEST = "C:/Temp/BC_New.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new PageSize().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        // properties
        PdfContentByte over;
        PdfDictionary pageDict;
        PdfArray mediabox;
        float llx, lly, ury,llz;
        // loop over every page
        for (int i = 1; i <= n; i++) {
            pageDict = reader.getPageN(i);
            mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
            llx = mediabox.getAsNumber(0).floatValue();
            lly = mediabox.getAsNumber(1).floatValue();
            ury = mediabox.getAsNumber(3).floatValue();
            mediabox.set(0, new PdfNumber((llx - 17)));
            mediabox.set(1, new PdfNumber((lly + 50)));
            over = stamper.getOverContent(i);
            over.saveState();
                       over.restoreState();
        }



        stamper.close();
        reader.close();
    }
}

SOLVED:

It was a problem with the source file which I was trying to modify. The original source file was a 8.26" X 11.69". A hava program adds a logo to the file and saves it into a new file.

Somehow due to this conversion, the new source file's X-axis got locked and hence my program was not able to modify the x axis.

Hence, I move my program as the first step and it worked. My program first modifies the x and y axis and then passes the file to the other program which does the logo adding stuff.

Thanks Bruno on this. :)

Upvotes: 0

Views: 7407

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You are changing the page size without changing the content.

If you want to shrink the content, then you need code that is more complex. See Shrinking the contents of a pdf page for an example. There's also an example in the official documentation: How to shrink pages in an existing PDF?

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
int n = reader.getNumberOfPages();
PdfDictionary page;
PdfArray crop;
PdfArray media;
for (int p = 1; p <= n; p++) {
    page = reader.getPageN(p);
    media = page.getAsArray(PdfName.CROPBOX);
    if (media == null) {
        media = page.getAsArray(PdfName.MEDIABOX);
    }
    crop = new PdfArray();
    crop.add(new PdfNumber(0));
    crop.add(new PdfNumber(0));
    crop.add(new PdfNumber(media.getAsNumber(2).floatValue() / 2));
    crop.add(new PdfNumber(media.getAsNumber(3).floatValue() / 2));
    page.put(PdfName.MEDIABOX, crop);
    page.put(PdfName.CROPBOX, crop);
    stamper.getUnderContent(p).setLiteral("\nq 0.5 0 0 0.5 0 0 cm\nq\n");
    stamper.getOverContent(p).setLiteral("\nQ\nQ\n");
}
stamper.close();
reader.close();

If you want to enlarge the content, then you can simply change the UserUnit. Changing the UserUnit is also explained in the official documentation: How to rotate and scale pages in an existing PDF?

float factor = 2.5f;
PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfDictionary page;
for (int p = 1; p <= n; p++) {
    page = reader.getPageN(p);
    if (page.getAsNumber(PdfName.USERUNIT) == null)
        page.put(PdfName.USERUNIT, new PdfNumber(factor));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();

This code will scale up the page and its content with a factor 2.5 (because that's what we defined as factor).

UPDATE

In your update, you have this:

    mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
    llx = mediabox.getAsNumber(0).floatValue();
    lly = mediabox.getAsNumber(1).floatValue();
    ury = mediabox.getAsNumber(3).floatValue();
    mediabox.set(0, new PdfNumber((llx - 17)));
    mediabox.set(1, new PdfNumber((lly + 50)));

By doing so, you add 0.236111 inches in x direction (llx -17) and you subtract 0.69445 inches in y direction. If you say that this changes 11.69 to 11 inch, then I assume that you're talking about the Y direction. If the original width in X direction was originally 8.26, it should be about 8.50.

I don't understand your problem. Of course: my calculations assume that there's no CropBox in your document. Is there a CropBox present?

Upvotes: 1

Related Questions