Добавление динамического изображения в PDF с использованием ColdFusion и iText - PullRequest
4 голосов
/ 30 марта 2009

Я собрал некоторый код, чтобы вставить динамическое изображение в PDF, используя ColdFusion и iText , заполняя также некоторые поля формы. После того, как я заработал это и написал об этом в блоге, я не мог не думать, что мог бы быть лучший способ достигнуть этого. Я использую основную идею этого в производственном приложении прямо сейчас, поэтому любые комментарии или предложения будут приветствоваться.

<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 ]

2 голосов
/ 04 июня 2009

<cfpdf> + DDX представляется возможным.

См. http://forums.adobe.com/thread/332697

0 голосов
/ 04 сентября 2015

Я сделал это по-другому с библиотекой itext Я не хочу перезаписывать свой существующий PDF-файл изображением для вставки, поэтому просто измените исходный PDF-файл, вставив изображение, просто вставка с использованием itext не работает для меня.

Итак, я должен вставить изображение в пустой PDF-файл (http://itextpdf.com/examples/iia.php?id=59) А потом присоединяйтесь к моему оригинальному pdf и новому pdf-изображению. Получение одного PDF с несколькими страницами. (http://itextpdf.com/examples/iia.php?id=110)

После этого вы можете наложить PDF-страницы с этой классной концепцией http://itextpdf.com/examples/iia.php?id=113

...