Resizing a pdf and keeping original metadata

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Resizing a pdf and keeping original metadata

Cesar Romani
I'm using the following code to resize a pdf to width=553 and

   public static void main(String[] args) {
     try {
       if (args.length < 2) {
        System.out.println("Pdf resizer");
        System.out.println("Args: pdf_in pdf_out");
       float Width=553;
       float Height=714;
       PdfReader reader = new PdfReader(args[0]);
       Document doc = new Document(new Rectangle(Width, Height));
       PdfWriter writer = PdfWriter.getInstance(doc, new
       PdfContentByte cb = writer.getDirectContent();

       PdfImportedPage tp;
       int pageNumber;
       int NumberOfPages=reader.getNumberOfPages();
       float Width2, Height2, WidthFactor, HeightFactor, factor;
       float offsetX, offsetY;

       for (pageNumber=1; pageNumber<=NumberOfPages; pageNumber++) {
        tp = writer.getImportedPage(reader, pageNumber);
        Width2 = reader.getPageSize(pageNumber).getWidth();
        Height2 = reader.getPageSize(pageNumber).getHeight();
        WidthFactor = Width / Width2;
        HeightFactor = Height / Height2;
        factor = Math.min(WidthFactor, HeightFactor);
        offsetX = (Width - (Width2*factor))/2;
        offsetY = (Height - (Height2*factor))/2;
        cb.addTemplate(tp, factor, 0, 0, factor, offsetX, offsetY);
     catch (Exception e) {

The problem is that the metadata gets lost. How can I keep the original
metadata on the resized pdf?

Many thanks in advance,


Check out the vibrant tech community on one of the world's most
engaging tech sites,!
iText-questions mailing list
[hidden email]

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book:
Please check the keywords list before you ask for examples: