You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by "Mamedov I.Z." <il...@mail.ru> on 2015/05/25 11:49:06 UTC

Using PDFBox for placing other PDF as layer

 Hello. I'm using PDFBox in my application to modify existing PDF files. 
I need to place barcode to first page of PDF document. 

So first i create new PDF file with inserted barcode:

BufferedImage awtImage = ImageIO.read(new ByteArrayInputStream(out.toByteArray()));
PDDocument document = new PDDocument();
PDPage pag = new PDPage(new PDRectangle(60, 60));
document.addPage(pag);
PDXObjectImage ximage = new PDPixelMap(document, awtImage);
PDPageContentStream stream = new PDPageContentStream(document, pag, false, false);
stream.drawXObject(ximage, 0, 0, ximage.getWidth(), ximage.getHeight());
stream.close();

If i save it to PDF it's OK.
Then i try to place it as new layer into existing PDF:

Point barlocation = new Point(0,0);
int offset = Math.round(25);
float Height = page.getMediaBox().getHeight();
float Width = page.getMediaBox().getWidth();
barlocation.setLocation(Width - pag.getMediaBox().getWidth(), Height - pag.getMediaBox().getHeight()); //I need to place it in the top right corner
LayerUtility layerUtility = new LayerUtility(doc);
List bigPages = doc.getDocumentCatalog().getAllPages();
PDXObjectForm firstForm = layerUtility.importPageAsForm(document, 0);
AffineTransform affineTransform = new AffineTransform(); // Identity... your requirements may differ
affineTransform.scale(10,10);
affineTransform.translate(barlocation.x , barlocation.y);
layerUtility.appendFormAsLayer((PDPage) bigPages.get(0), firstForm, affineTransform, "barcode layer");
doc.save(path);
doc.close(); But there is something wrong with position and size of my inserted PDF (as new layer). It's too small and position is wrong. I'm confused, i think myabe there are some dependencies on scale, but i'm not sure.
Please, help me with some clues. Sorry my bad English.

I have attached java file with source code and some PDF files (i get different results on different PDF documents, it depends on document size) named  files.zip

Kind regards, Mamedov Ilgar.

Re: Using PDFBox for placing other PDF as layer

Posted by Tilman Hausherr <TH...@t-online.de>.
I looked at the SuperimposePage example... that one uses q....Q 
operators around the layer... and a bit more.

https://svn.apache.org/viewvc/pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java?view=markup&sortby=date

Tilman

Am 25.05.2015 um 11:49 schrieb Mamedov I.Z.:
> Hello. I'm using PDFBox in my application to modify existing PDF files.
> I need to place barcode to first page of PDF document.
>
> So first i create new PDF file with inserted barcode:
>
> *BufferedImage awtImage = ImageIO.read(new 
> ByteArrayInputStream(out.toByteArray()));*
> *PDDocument document = new PDDocument();*
> *PDPage pag = new PDPage(new PDRectangle(60, 60));*
> *document.addPage(pag);*
> *PDXObjectImage ximage = new PDPixelMap(document, awtImage);*
> *PDPageContentStream stream = new PDPageContentStream(document, pag, 
> false, false);*
> *stream.drawXObject(ximage, 0, 0, ximage.getWidth(), ximage.getHeight());*
> *stream.close();
>
> *If i save it to PDF it's OK.
> Then i try to place it as new layer into existing PDF:
>
> *Point barlocation = new Point(0,0);*
> *int offset = Math.round(25);*
> *float Height = page.getMediaBox().getHeight();*
> *float Width = page.getMediaBox().getWidth();*
> *barlocation.setLocation(Width - pag.getMediaBox().getWidth(), Height 
> - pag.getMediaBox().getHeight()); //I need to place it in the top 
> right corner*
> *LayerUtility layerUtility = new LayerUtility(doc);*
> *List bigPages = doc.getDocumentCatalog().getAllPages();*
> *PDXObjectForm firstForm = layerUtility.importPageAsForm(document, 0);*
> *AffineTransform affineTransform = new AffineTransform(); // 
> Identity... your requirements may differ*
> *affineTransform.scale(10,10);*
> *affineTransform.translate(barlocation.x , barlocation.y);*
> *layerUtility.appendFormAsLayer((PDPage) bigPages.get(0), firstForm, 
> affineTransform, "barcode layer");*
> *doc.save(path);*
> *doc.close();*
>
> But there is something wrong with position and size of my inserted PDF 
> (as new layer). It's too small and position is wrong. I'm confused, i 
> think myabe there are some dependencies on scale, but i'm not sure.
> Please, help me with some clues. Sorry my bad English.
>
> I have attached java file with source code and some PDF files (i get 
> different results on different PDF documents, it depends on document 
> size) named files.zip
>
> Kind regards, Mamedov Ilgar.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org