2009-03-30 4 views
4

ColdFusioniText을 사용하여 동적 이미지를 PDF에 삽입하는 코드를 함께 작성하면서 일부 양식 필드도 채 웁니다. 내가 그것에 대해 일하고 그것에 대해 블로깅 한 후에, 나는 이것을 도울 수있는 더 좋은 방법이있을 것이라고 생각하지만 도움을 줄 수는 없다. 지금 프로덕션 앱에서이 기본 아이디어를 사용하고있어 어떤 의견이나 제안이라도 가장 환영받을 것입니다.ColdFusion 및 iText를 사용하여 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”> 
+0

하면 워터 마크가 필요하십니까이 멋진 개념 PDF로 페이지를 오버레이 할 수 있습니까? – Sergii

+0

CF8을 사용하면이 작업이 매우 쉽습니다. (그래도 멋진 예를 들어! :)) –

+1

난 그냥 기존의 PDF로 이미지를 삽입하는 워터 마크를 사용하지 않을거야. – rip747

답변

0

그래서 방금하여 itext는 아무튼으로 삽입, 이미지를 삽입하는 원래의 PDF 파일을 수정, 내가 삽입 할 이미지와 기존의 PDF를 덮어 싶어하지는 iText를 라이브러리 또 다른 방법으로 그것을 만든 나에게 도움이 안된다.

그래서 빈 이미지 (http://itextpdf.com/examples/iia.php?id=59) 에 이미지를 삽입하고 내 원본 PDF와 새 PDF 이미지에 가입해야합니다. 여러 페이지로 하나의 pdf를 구하십시오. (http://itextpdf.com/examples/iia.php?id=110)

그 후 당신은 http://itextpdf.com/examples/iia.php?id=113

+0

저는 이제 모든 PDF 페이지를 오버레이하지 않고보다 직접적으로이를 수행 할 수있는 방법을 개선하고 있습니다. ** ittext에서 PdfContentByte **의 addtemplate 사용 –