You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Peter Coppens <pc...@gmail.com> on 2009/01/14 12:45:19 UTC

Scale PDF pages

Hello,

I am trying to find a way to scale each of the pages in a pdf document.

Also see 
http://www.nabble.com/Scale-fop-created-pages-for-pdf-output-td21445071.html

I can make it work with

* A two step Apache FOP Œrendering¹
* Multvalent¹s impose command line tool

Both suffer some disadvantages and I¹d like to use PDFBox to implement the
scaling. 

(I assume this could become part of the command line utilities (if someone
would be interested))

But....I need some help to get started

On the mail thread listed higher, Jeremias suggests to use the FOP pdf
external image approach. As far as I understand that approach, this would
work like

* For each page in the original file
> * Make a pdf form and store that as an Xobject in the new pdf
> * Create a page in the new pdf with the scaled page size and populate the page
> with a single image pointing to the previously created Xobject

Not knowing too much about PDF in general and even less about PDFBox, can
someone please outline the things I would need to do (classes to use etc)

Many thanks indeed!

Peter

Re: Scale PDF pages

Posted by Peter Coppens <pc...@gmail.com>.
PDFBox fans,

By browsing some of the examples and experimenting a bit, I made some
progress.

What I currently have is provided lower.

While it works for my pdf¹s (for what I have tested is) I am sure it won¹t
always be correct.  E.g. The scaling of the mediabox could be incomplete and
e.g. Cropbox is not handled at all.

But, fwiw, here it is.....


  public void scalePages(String  file, float scale ) throws IOException,
COSVisitorException {      PDDocument doc1 = null;      try {          doc1
= PDDocument.load( file );          List allPages =
doc1.getDocumentCatalog().getAllPages();          for( int i=0;
i<allPages.size(); i++ ) {              PDPage page1 = (PDPage)allPages.get(
i );              
page1.getMediaBox().setUpperRightX(page1.getMediaBox().getUpperRightX()*scal
e);              
page1.getMediaBox().setUpperRightY(page1.getMediaBox().getUpperRightY()*scal
e);              PDFStreamParser parser = new
PDFStreamParser(page1.getContents());              parser.parse();
List tokens = parser.getTokens();              tokens.add(0,new
COSFloat(scale));              tokens.add(1,new COSInteger(0));
tokens.add(2,new COSInteger(0));              tokens.add(3,new
COSFloat(scale));              tokens.add(4,new COSInteger(0));
tokens.add(5,new COSInteger(0));
tokens.add(6,PDFOperator.getOperator("cm"));              PDStream
newContents = new PDStream( doc1 );              ContentStreamWriter writer
= new ContentStreamWriter( newContents.createOutputStream() );
writer.writeTokens( tokens );              newContents.addCompression();
page1.setContents(newContents);          }          doc1.save( file );
}      finally  {          if( doc1 != null )   {              doc1.close();
}      }  }  




From: Peter Coppens <pc...@gmail.com>
Date: Wed, 14 Jan 2009 12:45:19 +0100
To: <pd...@incubator.apache.org>
Conversation: Scale PDF pages
Subject: Scale PDF pages

Hello,

I am trying to find a way to scale each of the pages in a pdf document.

Also see 
http://www.nabble.com/Scale-fop-created-pages-for-pdf-output-td21445071.html

I can make it work with

* A two step Apache FOP Œrendering¹
* Multvalent¹s impose command line tool

Both suffer some disadvantages and I¹d like to use PDFBox to implement the
scaling. 

(I assume this could become part of the command line utilities (if someone
would be interested))

But....I need some help to get started

On the mail thread listed higher, Jeremias suggests to use the FOP pdf
external image approach. As far as I understand that approach, this would
work like

* For each page in the original file
> * Make a pdf form and store that as an Xobject in the new pdf
> * Create a page in the new pdf with the scaled page size and populate the page
> with a single image pointing to the previously created Xobject

Not knowing too much about PDF in general and even less about PDFBox, can
someone please outline the things I would need to do (classes to use etc)

Many thanks indeed!

Peter