Quantcast

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
height=714:

--------------------
[...]
   public static void main(String[] args) {
     try {
       if (args.length < 2) {
        System.out.println("Pdf resizer");
        System.out.println("Args: pdf_in pdf_out");
        System.exit(2);
       }
       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
FileOutputStream(args[1]));
       doc.open();
       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);
        doc.newPage();
       }
       doc.close();
       System.exit(0);
     }
     catch (Exception e) {
       e.printStackTrace(System.out);
       System.exit(1);
     }
   }
}
--------------------

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

Many thanks in advance,

--
Cesar


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

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: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Loading...