以编程方式创建 PDF 相册

发布于 2024-10-04 17:13:43 字数 236 浏览 0 评论 0原文

我有一组 PDF 相册模板(它们有空白方块,用于放置文本和照片)。我的需要是使用这些模板来生成实际的专辑。

我的计划是使用 iText 和 Java。我会向应用程序发送一个包含所有要使用的图像 URL 的数组。我将确切地知道图像应该放置在模板上的位置,并使用绝对定位来放置它们。

我只是想知道是否有一种更简单或更干净的方法来做到这一点 - 即使这意味着使用不同的语言? PDF 生成后,会自动发送到需要 PDF 格式的打印机。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒岛晴空 2024-10-11 17:13:43

我认为使用 ImageMagick 在许多语言(甚至是 shell 脚本)中都可以相当轻松地完成此操作。

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

苯莒 2024-10-11 17:13:43

目前尚不清楚照片是否有文字环绕。无论如何,我会看看 Apache-FOP。我经常使用它 - 它可能需要一些定制。

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.

与风相奔跑 2024-10-11 17:13:43
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();

如果您不知道确切位置,您有多种选择。

1) 替换现有图像。

使用此选项,您实际上可以让模板稍微悬垂在图像上,使用 Alpha 混合等。变得奇特。如果您使用 stamper.getUnderContent(1) 并构建具有透明背景的模板,则可以对上述代码执行相同的操作。我刚刚回答了 替换一个应该在这方面有所帮助的图像问题

新图像将继承所有占位符图像的图形状态。 x,y,wid,hei,可选内容组,透明度,各种各样的东西。

2) 在 Acrobat 中(或者我想使用 iText)向模板添加注释(字段很容易访问),并使用它的 RECT 为上述代码提供 x、y、wid、hei。

// 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;

将这些坐标输入到上面的图像代码中,就可以开始了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文