使用ColdFusion和iText将动态图像添加到PDF [英] Adding a dynamic image to a PDF using ColdFusion and iText
本文介绍了使用ColdFusion和iText将动态图像添加到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我拼凑了一些代码,使用 ColdFusion 将动态图片插入PDF和 iText ,同时填写某些表单字段。在我得到它的工作和博客,我不禁想到,可能有一个更好的方法来完成这一点。我现在正在使用生产应用程序中的基本想法,所以任何评论或建议都将受到欢迎。
I pieced together some code to insert a dynamic image into a PDF using both ColdFusion and iText, while filling in some form fields as well. After I got it working and blogged about it, I couldn't help but think that there might be a better way to accomplish this. I'm using the basic idea of this in a production app right now so any comments or suggestion would be most welcomed.
<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">
推荐答案
< cfpdf& / code> + DDX似乎可能。
<cfpdf>
+ DDX seems possible.
请参阅http://forums.adobe.com/thread/332697
这篇关于使用ColdFusion和iText将动态图像添加到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文