使用ColdFusion和iText将动态图像添加到PDF

时间:2009-03-30 13:08:53

标签: pdf coldfusion itext

我将一些代码拼凑在一起,使用ColdFusioniText将动态图像插入PDF,同时填写一些表单字段。在我开始工作并写博客后,我忍不住想到可能有更好的方法来实现这一目标。我现在正在生产应用程序中使用这个基本概念,所以任何评论或建议都会受到欢迎。

<cfscript>
// full path to PDF you want to add image to
readPDF = expandpath(”your.pdf”);
// full path to the PDF we will output. Using creatUUID() to create
// a unique file name so we can delete it afterwards
writePDF = expandpath(”#createUUID()#.pdf”);
// full path to the image you want to add
yourimage = expandpath(”dynamic_image.jpg”);

// JAVA STUFF!!!
// output buffer to write PDF
fileIO = createObject(”java”,”java.io.FileOutputStream”).init(writePDF);
// reader to read our PDF
reader = createObject(”java”,”com.lowagie.text.pdf.PdfReader”).init(readPDF);
// stamper so we can modify our existing PDF
stamper = createObject(”java”,”com.lowagie.text.pdf.PdfStamper”).init(reader, fileIO);
// get the content of our existing PDF
content = stamper.getOverContent(reader.getNumberOfPages());
// create an image object so we can add our dynamic image to our PDF
image = createobject(”java”, “com.lowagie.text.Image”);
// get the form fields
pdfForm = stamper.getAcroFields();
// setting a value to our form field
pdfForm.setField(”our_field”, “whatever you want to put here”);
// initalize our image
img = image.getInstance(yourimage);
// centering our image top center of our existing PDF with a little margin from the top
x = (reader.getPageSize(1).width() - img.scaledWidth()) - 50;
y = (reader.getPageSize(1).height() - img.scaledHeight()) / 2 ;
// now we assign the position to our image
img.setAbsolutePosition(javacast(”float”, y),javacast(”float”, x));
// add our image to the existing PDF
content.addImage(img);
// flattern our form so our values show
stamper.setFormFlattening(true);
// close the stamper and output our new PDF
stamper.close();
// close the reader
reader.close();
</cfscript>
<!— write out new PDF to the browser —>
<cfcontent type=”application/pdf” file = “#writePDF#” deleteFile = “yes”>

2 个答案:

答案 0 :(得分:2)

<cfpdf> + DDX似乎有可能。

See http://forums.adobe.com/thread/332697

答案 1 :(得分:0)

我用另一种方法用itext库制作了它 我不想用要插入的图像覆盖我现有的pdf,所以只需修改插入图像的原始pdf,只需用itext插入就不适合我。

所以,我必须将图像插入空白pdf(http://itextpdf.com/examples/iia.php?id=59) 然后加入我原来的pdf和新的pdf-image。获得一个包含多个页面的pdf。 (http://itextpdf.com/examples/iia.php?id=110

之后,您可以使用这个很酷的概念覆盖pdf页面 http://itextpdf.com/examples/iia.php?id=113