Getting started with iText - Part 5 (Margins.java)
This next entry translates the Margins.java example. This simple class shows how you can modify the margins of document object.
One caveat, if you try and run this example using MX7's built in iText.jar you may see this error
The selected method setMarginMirroring was not found.
How to determine your iText version. Apparently the version that comes with MX7 does not contain the Document.setMarginMirroring() method. But you can use Mark Mandel's JavaLoader.cfc to load a newer version without disturbing CF.
UPDATE: It was recently brought to my attention that I may not have stated this point as strongly as I should have. MX users that are running the JavaLoader.cfc must read this article Using a Java URLClassLoader in CFMX Can Cause a Memory Leak.
In summary it is recommended that you store a single instance of the javaLoader the server scope, rather than creating a new object each time. This prevents a memory leak caused by a bug in ColdFusion MX. I do not know if this bug exists in ColdFusion 8.
And now without further ado, the code
Source: http://itextdocs.lowagie.com/tutorial/general/
Documentation: See Step 1 for more information
Example: Margins.java
<h1>Document margins</h1>
<cfscript>
savedErrorMessage = "";
// by default outputs to current directory. change as needed
fullPathToOutputFile = ExpandPath("./Margins.pdf");
// step 1: creation of a document-object
pageSize = createObject("java", "com.lowagie.text.PageSize");
document = createObject("java", "com.lowagie.text.Document").init(PageSize.A5,
javacast("float", 36),
javacast("float", 72),
javacast("float", 108),
javacast("float", 180));
WriteOutput("Version="& document.getVersion() &"<br>");
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
outStream = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile);
writer = createObject("java", "com.lowagie.text.pdf.PdfWriter").getInstance(document, outStream);
// step 3: we open the document
document.open();
// step 4:
//cfSearching: create a single paragraph object and reuse it
paragraph = createObject("java", "com.lowagie.text.Paragraph");
element = createObject("java", "com.lowagie.text.Element");
document.add(paragraph.init("The left margin of this document is 36pt (0.5 inch); the right margin 72pt (1 inch); the top margin 108pt (1.5 inch); the bottom margin 180pt (2.5 inch). "));
//cfSearching: create a new paragraph instance for modifying
newParagraph = paragraph.init();
newParagraph.setAlignment(Element.ALIGN_JUSTIFIED);
for (i = 0; i LT 20; i = i + 1) {
newParagraph.add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. ");
}
document.add(newParagraph);
document.setMargins(javacast("float", 180),
javacast("float", 108),
javacast("float", 72),
javacast("float", 36));
document.add(paragraph.init("Now we change the margins. You will see the effect on the next page."));
document.add(newParagraph);
document.setMarginMirroring(javacast("boolean", true));
document.add(paragraph.init("Starting on the next page, the margins will be mirrored."));
document.add(newParagraph);
// display final status
WriteOutput("Finished!");
}
catch (com.lowagie.text.DocumentException de) {
savedErrorMessage = de;
}
catch (java.io.IOException ioe) {
savedErrorMessage = ioe;
}
catch (coldfusion.runtime.Cast$NumberConversionException nce) {
savedErrorMessage = nce;
}
// step 5: we close the document
document.close();
// close the output stream
outStream.close();
</cfscript>
<!--- show any errors --->
<cfif len(savedErrorMessage) gt 0>
Error - unable to create document
<cfdump var="#savedErrorMessage#">
</cfif>
0 comments:
Post a Comment