Programmatically create PDF photo albums

泄露秘密 提交于 2020-01-04 06:02:54

问题


I have a set of PDF photo album templates (they have blank squares where text and photos should go). My need is to use these templates to generate actual albums.

My plan was to use iText and Java. I would send the app an array of all the image URLs to use. I will know exactly where the images should be placed on the templates and use absolute positioning to place them.

I was just wondering if there was an easier or cleaner way of doing this - even if it means using a different language? After the PDFs are generated they are automatically sent to a printers who require them to be PDFs.


回答1:


I think this could be done fairly easily in many languages (even a shell script) using ImageMagick.




回答2:


It's not clear whether you have text wrapping the photos. Anyway I would look at Apache-FOP. I've used this a lot - it might take a bit of customization.




回答3:


PdfReader reader = new PdfReader("template.pdf");
PdfStamper stamper = new PdfStamper( reader, new FileOutputStream("output.pdf"));

PdfContentByte content = stamper.getOverContent(1); // first page == 1

Image image = Image.createInstance("someImage.png");
image.setAbsolutePosition( x, y );
image.setAbsoluteHeight( hei );
image.setAbsoluteWidth( wid );

content.addImage(image);

// instead of absolutely positioning the image, you can do:
//content.addImage( image, wid, 0f, 0f, hei, x, y ); 
// that's a transformation matrix, you can skew, rotate, scale, etc.

stamper.close();

If you don't know the exact location, you have a couple options.

1) Replace an existing image.

With this option you can actually have the template overhang the image a bit, use alpha blending, and so on. Get Fancy. You could do the same thing with the above code if you used stamper.getUnderContent(1) and built your template with a transparent background. I just answered a replace an image question that should help on that front.

The new image will inherit all the placeholder image's graphic state. x,y,wid,hei, optional content groups, transparency, all manner of things.

2) Add an annotation (fields are easy to access) to the template in Acrobat (or with iText I suppose) and use it's RECT to provide x,y,wid,hei for the above code.

// given the above stamper
AcroFields fields = stamper.getAcroFields();
PdfDictionary replaceAnnot = fields.getFieldItem("ReplaceMe").getMerged(0);

//we can remove the field from the PDF without breaking the info in replaceAnnot.
fields.removeField("ReplaceMe");

// rects are laid out [llx, lly, urx, ury]
PdfArray rect = replaceAnnot.getAsArray(PdfName.RECT);
float x = rect.getAsNumber(0).floatValue();
float y = rect.getAsNumber(1).floatValue();
float width = rect.getAsNumber(2).floatValue() - x;
float height = rect.getAsNumber(3).floatValue() - y;

Feed those coordinates to the above image code and you're good to go.



来源:https://stackoverflow.com/questions/4267309/programmatically-create-pdf-photo-albums

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!