These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Arranging the content tree

andyg2
Registered: Oct 19 2010
Posts: 7
Answered

Hi,
I have the task of adding photos to a recruitment company's portfolio of 12000 workers PDF profiles. The initial PDF contains all the workers' details and I need to add an image three layers back from the front.
I've automated all of this but I can't work out how to move the image back 3 levels in the content tree.
Any help on this would save my life!
 
To recap, in javascript I need to find the only image and move it from the front to level 4
Each PDF has just a single page
 
Thank you in advance :)

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
When asking questions on a highly technical subject you need to be very specific. But I think I get what you are saying. When you say layers you mean visual layers of content, right? Acrobat draws PDF page content in the order in which it appears in the content stream. In this sense there aren't any real "layers" of visual content, just the order in which things were drawn. And you want the image to appear at a specific location in the drawing order?

Acrobat provides some nifty tools in the "Content" navigation panel for moving individual pieces of content around. But this functionality is not available in the JavaScript Model. There's nothing even close to it. This is the kind of thing you would write a plug-in for.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

andyg2
Registered: Oct 19 2010
Posts: 7
Thank you for the information, it explains why I've been scratching my head for 3 days. In the unlikely event I find a way round it I'll let you guys know. I might look at a way to extract the paths, text and image, then rebuild it in the right order. :)
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There is a trick for creating ordered content using JavaScript. It's awkward, but it can work. The "doc.addWatermarkFromFile()" function will add content to the top or bottom of the drawing order. So if you have your "Layers" in separate files you can can in fact write a script to build the page content in a specific order by sequentially adding watermarks, but it has to be done carefully.
Here's an article on using this function.
http://acrobatusers.com/tutorials/2006/create_use_layers


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

rbogie
Registered: Apr 28 2008
Posts: 432
what do you mean by "rebuild it in the right order"? do you want a logo or some text to be visible on (in front of) the photo. i suppose you explored the appearance options on the add watermark tool. correct? i can automate the process of adding a watermark or multiple watermarks (and making appearance settings) to any number of files.
andyg2
Registered: Oct 19 2010
Posts: 7
When the PDFs were created they placed a barcode (constructed from 3 paths) within the area intended for the employee photo. When I add the employee photo it's obscuring the barcode. I've tried using addWatermarkFromFile() to the bottom of the drawing order but there is a grey path covering the entire page already at the bottom of the drawing order. I suppose now the obvious solution would be to add the employee photo on top and recreate the barcode on top of that.

Is there any way to export the existing barcode paths as an image?

My current idea for this is to save a copy of the entire PDF as an image.
With another scripting language, maybe PHP I could crop down to the barcode and then use addWatermarkFromFile() to import it into the PDF over the image but it's in no way perfect.

Thanks again for your continued help
rbogie
Registered: Apr 28 2008
Posts: 432
interesting problem, for sure. i doubt you'd want to export the barcode as an image. have you tried to re-position the bar code using the touch-up object tool? in the content panel you can drag/drop (re-align) a path in the content tree. one option would be to copy/paste the barcode to a blank PDF (barcode.pdf) and position the barcode as needed and then bring barcode.pdf into the main.pdf as a watermark. could you send me an example (without violating any privacy barriers)? you can send me a private email via the link in my profile.
andyg2
Registered: Oct 19 2010
Posts: 7
rbogie wrote:
interesting problem, for sure. i doubt you'd want to export the barcode as an image. have you tried to re-position the bar code using the touch-up object tool? in the content panel you can drag/drop (re-align) a path in the content tree. one option would be to copy/paste the barcode to a blank PDF (barcode.pdf) and position the barcode as needed and then bring barcode.pdf into the main.pdf as a watermark. could you send me an example (without violating any privacy barriers)? you can send me a private email via the link in my profile.
You're right I could do that but I have 12000 of them so it needs to be done pragmatically. It's easy enough to use PHP's GD library to export the PDF as a TIFF, crop the image to the barcode (as it's in the same position on each PDF) then add the image over the top of the employee photo.

Do you know how to import from file in a batch? Assuming I have 123.pdf and 123.tiff in the same folder and I need to position it 72 points from the top and 72 points from the right?

Thanks
rbogie
Registered: Apr 28 2008
Posts: 432
the process, of course, is to figure out the simplest steps and then see if they can be automated. To your question ("do you know how ...), the answer is yes. but not using Acrobat's batch or javascript (except supplementally). try a keyboard macro programming tool, but it can be complicated. i'd like to see one of your files.
andyg2
Registered: Oct 19 2010
Posts: 7
OK, I found an "acceptable" solution and although it's not in javascript people might look here for an answer.
It uses ImageMagick, PHP and the PHP::Zend Library.

First I convert the PDF to 600dpi TIFF (copy) and crop down to the barcode using ImageMagick
Then I use the PHP Zend library to position and add the employee photo and the barcode to the PDF

PHP Code follows

<?php
$cwd = getcwd();
$pdf_template = $cwd.'\\123.pdf';//original PDF
$barcode = $cwd.'\\123.tiff';//name for barcode tiff file
$employee_photo = $cwd.'\\123.jpg';//filename of the employee photo

//location of barcode in original PDF
$tl_x = 2791;
$tl_y = 4216;

//width and height of barcode in original PDF
$w = 1058;
$h = 608;

//get filename without pdf extension
$name = substr($pdf_template, 0, strrpos($pdf_template, '.'));

//build the imagemagick command line to convert PDF to TIFF
$command = 'convert -density 600 +antialias -quality 100 '.$pdf_template.' '.$name.'.'.$newtype;
shell_exec($command);


$command = 'convert '.$barcode.' +antialias -crop '.$w.'x'.$h.'+'.$tl_x.'+'.$tl_y.' '.$barcode;
shell_exec($command);


//next you'll need Zend PDF
include('Zend/Pdf.php');

//set the dimentions of the original PDF (in points 72 per inch)
$doc_x = 1080;
$doc_y = 864;
$right_margin = 72;

//load the PDF into Zend
$pdf = Zend_Pdf::load($pdf_template);

foreach ($pdf->pages as $page)
{
//load the employee photo into Zend
$photo = Zend_Pdf_Image::imageWithPath($photo);

//work out the dimentions of the photo
$i_height = $photo->getPixelHeight()/8.333333333;
$i_width = $photo->getPixelWidth()/8.333333333;//position the photo based on the right margin
//the barcode positioning is easier
//this is more tricky because some of the photos were odd dimentions
//but needed to aligned to the right
$i_left = $doc_x-$right_margin-$i_width;
$i_bottom = $doc_y-$i_height;
$i_right = $doc_x-$right_margin;
$i_top = $doc_y;

//add the photo to the PDF
$page->drawImage($image, $i_left, $i_bottom, $i_right, $i_top);//now for the barcode

//load the barcode into Zend
$image = Zend_Pdf_Image::imageWithPath($barcode);

//position the photo - PDFs Y axis start at zero at the bottom
$i_left = 334;
$i_bottom = $doc_y-578;
$i_right = 461;
$i_top = $doc_y-506;

//add the barcode to the PDF
$page->drawImage($image, $i_left, $i_bottom, $i_right, $i_top);}
//save the PDF (maintaining PDF creator and existing document structure) - Zend is great!
$pdf->save($pdf_template);
?>