Solved

how to set absolute position with iText and Coldfusion

Posted on 2011-02-28
3
2,651 Views
Last Modified: 2013-12-20
I need genrate PDf form Java class iText from COldfusion and not with CFDOCUMENT that is very bad to generate PDF (too large file, too slow).
I've made some testing and when I use iText class its more quick, and my PDF are better optimized.
So in fact I need to create some text block, images. For the images its realy simple (see code below), you can choose to put the image bloc with point precision but impossible for the text block. I thing probably there are some setting that can be same...
So I created a paragrapgh block, but if I try to use phrase2.setAbsolutePosition(x,y); I have error message... What is the solution to put a text block at x,y position in my PDF page ?


Sample code for image block :

<cfscript>
// create a 'Document' object
rectangle = CreateObject("java", "com.lowagie.text.Rectangle");
pageSize = Rectangle.init(595, 842);
document = CreateObject("java", "com.lowagie.text.Document").init(pageSize, 0, 0, 0, 0);
Image = CreateObject("java", "com.lowagie.text.Image");

// get an outputstream for the PDF Writer
fileIO = CreateObject("java", "java.io.FileOutputStream");


// call the constructor, pass the location where you want
// the pdf to be created
fileIO.init("#expandpath("MYPDF.pdf")#");

// get a PDF Writer var
writer = CreateObject("java", "com.lowagie.text.pdf.PdfWriter");

// call the static 'getInstance' factory method
writer.getInstance(document, fileIO);

// open the document
document.open();
 
// create a new paragraph
paragraph = CreateObject("java", "com.lowagie.text.Paragraph");
phrase = CreateObject("java", "com.lowagie.text.Phrase");

phrase0 = paragraph.init(" ", Font.init(Font.HELVETICA, 15));

phrase = paragraph.init("HELLO", Font.init(Font.HELVETICA, 20));
phrase.setAlignment(Element.ALIGN_LEFT);

phrase2 = paragraph.init("WORLD", Font.init(Font.HELVETICA, 20));
phrase2.setAlignment(Element.ALIGN_RIGHT);

document.add(phrase0);
document.add(phrase);
document.add(phrase2);

//image block
Image.getInstance("#expandpath("logo.jpg")#");
logo1.setDpi(300,300);
logo1.scaleAbsolute(263, 117);
logo1.setAbsolutePosition(166, 32);
document.add(logo1);

document.close();
</cfscript>

Many thanks
0
Comment
Question by:alliancecube
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 250 total points
ID: 35001566
if I try to use phrase2.setAbsolutePosition(x,y); I have error message

com.lowagie.text.Phrase doesn't have a setAbsolutePosition method, only images.  Here's one alternative to positioning text absolutely. Since you're using cfscript, the code should be almost identical.

http://stackoverflow.com/questions/1625455/itext-positioning-text-absolutely
0
 

Author Closing Comment

by:alliancecube
ID: 35007955
I asked for Coldfusion and Java integration not for just the Java code, because my problem was to integrate the Java class in a Coldfusion Script tag...
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35009124
@alliancecube

I'm not sure why you had problems with it.  Especially as you didn't ask any questions.  I guess didn't look at the code too closely. As I said, since you're using cfscript it's almost an exact translation. In fact the only line that's different is the font line

    cb.SetFontAndSize(bf, 12);

instead of

     cb.SetFontAndSize(Font, 12);


Not a huge difference. Good Luck!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 51
Application.cfc - what is the order of events? 4 67
Create sub domain on windows dedicated server. 13 75
spring maven example issues 3 44
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question