Main.java - PDFKitTutorials - Version 5.4.0
// RCMPDFKitTutorials --- tutorials with examples for PDFKit.framework V5.4.0
// Copyright (c) 2007 WEBAPPZ Systems, Inc. ------------------------------------
package com.webappz.pdfkit_tutorials.components;
import com.webappz.pdfkit.*;
import com.webobjects.appserver.*;
import com.webobjects.foundation.*;
public class Main extends WOComponent {
public static final long serialVersionUID = 20071121L;
public Main(WOContext context) {
super(context);
// all lines below are optional but will provide some extra information
RCMPDFDocument.setLogPerformance(true); // to view performance numbers
RCMPDFDocument.listPaperSizes(); // print list to standard out
RCMPDFFont.listAvailableFonts(); // print list to standard out
}
// Tutorial 1 - Hello World ----------------------------------------------------
public RCMPDFDocument tutorial1() {
RCMPDFDocument doc = new RCMPDFDocument(); // create a new PDF document
RCMPDFPage page1 = new RCMPDFPage(doc); // create a new PDF page
page1.addBox(new RCMPDFBox(50, 50, "Hello World"));
doc.setShowPageNumbers(false); // optional - do not show page numbers
doc.setFileName("Tutorial 1"); // optional - give PDF file a filename
return doc;
}
// Tutorial 2 - Hello World 2x2 ------------------------------------------------
public RCMPDFDocument tutorial2() {
RCMPDFDocument doc = new RCMPDFDocument(true); // LANDSCAPE new PDF doc
RCMPDFPage page1 = new RCMPDFPage(doc); // create a new PDF page
RCMPDFPage page2 = new RCMPDFPage(doc); // create a new PDF page
page1.addBox(new RCMPDFBox(50, 50, "Hello World 1-1"));
page1.addBox(new RCMPDFBox(500, 500, "Hello World 1-2"));
page2.addBox(new RCMPDFBox(50, 500, "Hello World 2-1"));
page2.addBox(new RCMPDFBox(500, 50, "Hello World 2-2"));
doc.setFileName("Tutorial 2"); // optional - give PDF file a filename
return doc;
}
// Tutorial 3 - Hello World Colors ---------------------------------------------
// create several colors we will use for this tutorial
protected RCMPDFColor red = new RCMPDFColor(1.0, 0.12, 0.02); // double RGB
protected RCMPDFColor white = new RCMPDFColor(1.0, 1.0, 1.0); // double RGB
protected RCMPDFColor veryLightGrey = new RCMPDFColor("#f0f2f4"); // HTML
protected RCMPDFColor lightGrey = new RCMPDFColor("#e0e2f4"); // HTML
protected RCMPDFColor grey = new RCMPDFColor("#a2a5a6"); // HTML
protected RCMPDFColor darkBlue = new RCMPDFColor("#0c4989"); // HTML
public RCMPDFDocument tutorial3() {
RCMPDFDocument doc = new RCMPDFDocument(); // create a new PDF document
RCMPDFPage page1 = new RCMPDFPage(doc); // create a new PDF page
RCMPDFBox box1 = new RCMPDFBox(100, 100, 100, 100, "Hello");
RCMPDFBox box2 = new RCMPDFBox(200, 200, 100, 100, "WebObjects");
RCMPDFBox box3 = new RCMPDFBox(300, 300, 100, 100, "World");
box1.setStrokeColor(grey);
box2.setStrokeColor(grey);
box3.setStrokeColor(red);
box3.setStrokeWidth(10); // make the border larger
box1.setFillColor(darkBlue);
box2.setFillColor(veryLightGrey);
box3.setFillColor(lightGrey);
box1.setTextColor(white);
box3.setTextColor(red);
box2.setVAlign("bottom");
box2.setHAlign("right");
box3.setVAlign("middle");
box3.setHAlign("center");
page1.addBox(box1);
page1.addBox(box2);
page1.addBox(box3);
doc.setFileName("Tutorial 3");
return doc;
}
// Tutorial 4 - Hello World Fonts ----------------------------------------------
// Classic API: Return a WOComponent instead of a RCMPDFKitDocument
// create several colors we will use for this tutorial
protected RCMPDFFont helveticaBold28 =new RCMPDFFont("Helvetica","Bold",28);
protected RCMPDFFont timesItalic18 = new RCMPDFFont("Times", "Italic", 18);
protected RCMPDFFont helveticaBold9 = new RCMPDFFont("Helvetica","Bold", 9);
protected RCMPDFFont helvetica8 = new RCMPDFFont("Helvetica", "", 8);
public WOComponent tutorial4() {
RCMPDFDocument doc = new RCMPDFDocument(); // create a new PDF document
RCMPDFPage page1 = new RCMPDFPage(doc); // create a new PDF page
RCMPDFBox box1 = new RCMPDFBox(300, 100, 100, 100, "Hello");
RCMPDFBox box2 = new RCMPDFBox(200, 200, 100, 100,
"Wonderful Apple WebObjects");
RCMPDFBox box3 = new RCMPDFBox(100, 300, 100, 100, "World");
box1.setTextFont(timesItalic18);
box2.setTextFont(timesItalic18);
box3.setTextFont(helveticaBold28);
box1.setStrokeColor(grey);
box2.setStrokeColor(grey);
box3.setStrokeColor(red);
box3.setStrokeWidth(10); // make the border larger
box1.setFillColor(darkBlue);
box2.setFillColor(veryLightGrey);
box3.setFillColor(lightGrey);
box1.setTextColor(white);
box3.setTextColor(red);
box2.setVAlign("bottom");
box2.setHAlign("right");
box3.setVAlign("middle");
box3.setHAlign("center");
page1.addBox(box1);
page1.addBox(box2);
page1.addBox(box3);
// Create a new displayer page for 'doc' and return it as a WOComponent
RCMPDFDisplayer disp = (RCMPDFDisplayer) pageWithName("RCMPDFDisplayer");
disp.setDocument(doc); // disp will be the displayer for doc
disp.setFileName("Tutorial 4");
return disp;
}
// Tutorial 5 - X --------------------------------------------------------------
public RCMPDFDocument tutorial5() {
RCMPDFDocument doc = new RCMPDFDocument(true); // create a new PDF doc
RCMPDFPage page1 = new RCMPDFPage(doc); // create a new PDF page
int i = 0;
for (; i <= 20; i++) {
RCMPDFBox boxA = new RCMPDFBox(i * 20, i * 20, 100, 10,"Box A "+ i);
RCMPDFBox boxB = new RCMPDFBox(i * 20,400-i*20,100,10,"Box B " + i);
boxA.setStrokeWidth(i / 2);
boxB.setStrokeWidth(i / 2);
boxA.setFillColor(darkBlue);
boxB.setFillColor(veryLightGrey);
boxA.setTextColor(white);
boxB.setTextColor(red);
boxA.setStrokeColor(grey);
boxB.setStrokeColor(grey);
boxA.setHAlign("Center");
boxB.setHAlign("Center");
page1.addBox(boxA);
page1.addBox(boxB);
}
doc.setFileName("Tutorial 5");
return doc;
}
// Tutorial 6 - Add Images -----------------------------------------------------
public RCMPDFDocument tutorial6() {
// Get page from previous X tutorial 5 document
RCMPDFDocument doc = tutorial5();
RCMPDFPage page1 = (RCMPDFPage) doc.pages().objectAtIndex(0);
RCMPDFBox box1;
// Load in some images from /Resources
page1.addImage(new RCMPDFImage(0, 200, 100, 36, "1tracker_logo.gif"));
page1.addImage(new RCMPDFImage(200, 400, 100, 36, "bizdav_logo.gif"));
page1.addImage(new RCMPDFImage(400, 200, 100, 100, "colors.png"));
// NOTE: Images can also be loaded from NSData (database) OR a web page
page1.addBox(box1 = new RCMPDFBox(220, 424, "!bizDAV!"));
box1.setTextColor(new RCMPDFColor("#FFFFFF"));
doc.setFileName("Tutorial 6");
return doc;
}
// Tutorial 7, 8, and 9 - Tables -----------------------------------------------
// Define data for the table; normally, this would come from a database, ...
// or your source code filling up such arrays, or would be automatically ...
// filled in by RCMListEdit.framework for all its lists and detail edits.
NSArray<String> columnHeadings = new NSArray<String>(new String[] {
"First Name", "Last Name", "Email", "Phone", "Address", "City",
"P/S", "Country" });
NSArray<String> row1 = new NSArray<String>(new String[] { "Steven", "Jobs",
"sjobs@apple.com", "408.996.1010", "1 Infinite Loop", "Cupertino",
"CA", "USA" });
NSArray<String> row2 = new NSArray<String>(new String[] { "William",
"Gates", "wgates@microsoft.com", "425.882.8080", "1 Microsoft Way",
"Redmond", "WA", "USA" });
NSArray<String> row3 = new NSArray<String>(new String[] { "Larry",
"Ellison", "lellison@oracle.com", "650.506.7000",
"500 Oracle Parkway", "Redwood Shores", "CA", "USA" });
NSArray<String> row4 = new NSArray<String>(new String[] { "Jim",
"Pattison", "jim@jp-group.com", "604.688.6764",
"Österreich Änz Straße Ü12§8", "Munich", "BC", "CANADA" });
NSArray<String> row5 = new NSArray<String>(new String[] { "Tony", "Lo",
"tlo@giant.com.tw", "+866.4.2681.0", "No. 91, Sec. 3, Nanking",
"Taipei", "TW", "TAIWAN" });
@SuppressWarnings("unchecked")
// Line below cannot be fixed for Generics
NSArray<NSArray<String>> rows = new NSArray<NSArray<String>>(new NSArray[] {
row1, row2, row3, row4, row5 });
// For the sake of tutorials 8+9, make large tables by repeating above data.
public NSArray<NSArray<String>> rowsN(int n) { // repeat above rows array n
// times
int i;
NSArray<NSArray<String>> result = new NSArray<NSArray<String>>();
for (i = 0; i < n; i++)
result = result.arrayByAddingObjectsFromArray(rows);
return result;
}
// Basic table with no options
public RCMPDFDocument tutorial7() {
RCMPDFDocument doc = new RCMPDFDocument(); // create a new PDF document
RCMPDFPage page1 = new RCMPDFPage(doc); // create a new PDF page
RCMPDFTable table1 = new RCMPDFTable(page1, columnHeadings, rows);
table1.draw(); // Required - it will create all the boxes for the table
doc.setFileName("Tutorial 7");
return doc;
}
// Multiple Tables with above data repeated
public RCMPDFDocument tutorial8() {
RCMPDFDocument doc = new RCMPDFDocument(); // create a new PDF document
RCMPDFPage page1 = new RCMPDFPage(doc); // create a new PDF page
// Loop variables
int i; // iteration
RCMPDFPage currentPage = page1; // current page location
double yPosition = 0; // vertical position
for (i = 1; i < 75; i++) { // LOOP 75 tables - yielding 407 pages
RCMPDFTable table = new RCMPDFTable
(currentPage, columnHeadings, rowsN(i)); // define table
table.setYTop(yPosition); // set correct starting pos
table.setTitle("Table number " + i); // give the table a title
// to make this tutorial more interesting, set more attributes
table.setTitleColor(randDarkColor());
table.setHeadingColor(randDarkColor());
table.setHeadingTextColor(randLightColor());
table.setRowColor(randLightColor());
table.setAltRowColor(randLightColor());
table.setRowTextColor(randDarkColor());
table.setStrokeColor(randDarkColor());
table.setTitleFont(timesItalic18);
table.setHeadingFont(helveticaBold9);
table.setRowFont(helvetica8);
table.draw(); // draw the table
currentPage = table.lastPage(); // last page of table
yPosition = table.yBottomLast() + 50; // update location + 50
// points
if (yPosition > currentPage.bottomYPos()) { // have we reached
// bottom?
yPosition = 0; // reset y position
currentPage = new RCMPDFPage(doc); // self create a new page
}
}
doc.setFileName("Tutorial 8");
return doc;
}
// Formatted table with images that are repeating for every page
public RCMPDFDocument tutorial9() {
RCMPDFDocument doc = new RCMPDFDocument(); // create a new PDF document
RCMPDFPage page1 = new RCMPDFPage(doc); // create a new PDF page
RCMPDFTable table = new RCMPDFTable(page1, columnHeadings, rowsN(100));
table.setTitle("Table for Tutorial 9");
// set colors and fonts
table.setTitleColor(darkBlue);
table.setHeadingColor(darkBlue);
table.setHeadingTextColor(white);
table.setRowColor(lightGrey);
table.setAltRowColor(veryLightGrey);
table.setStrokeColor(new RCMPDFColor("#b2b5b6"));
table.setTitleFont(timesItalic18);
table.setHeadingFont(helveticaBold9);
table.setRowFont(helvetica8);
// Define images at the top and make sure table starts below them
// first page - special handling
page1.addImage(new RCMPDFImage(155, 0, 200, 72, "1tracker_logo.gif"));
table.setYTop(100); // 100 points from the top for first page
// repeating pages - pages created by the table
table.setRepeatingPageImages(new NSMutableArray<RCMPDFImage>(
new RCMPDFImage[] {
new RCMPDFImage(0, 0, 100, 36, "1tracker_logo.gif"),
new RCMPDFImage(418, 0, 100, 36, "bizdav_logo.gif") }));
table.setYTopRepeat(50); // 50 points from the top for subsequent pages
// Set column alignments
table.setColHorizontalAlignments(new NSArray<String>(new String[] {
"left", "left", "left", "right", "left", "center", "center",
"right" }));
table.draw(); // Required - it will create all the boxes for the table
doc.setFileName("Tutorial 9");
return doc;
}
// Generate a random light color
public RCMPDFColor randLightColor() {
return new RCMPDFColor(0.60 + Math.random() * 0.4,
0.60 + Math.random() * 0.4, 0.60 + Math.random() * 0.4);
}
// Generate a random dark color
public RCMPDFColor randDarkColor() {
return new RCMPDFColor(Math.random() * 0.4, Math.random() * 0.4, Math
.random() * 0.4);
}
// Tutorial 10 - Client-side Font Test
// -----------------------------------------
public String fontTestText = "Tutorial 10 attempts to generate a page for every font that has been bundled with the PDFKit framework.\nSince these fonts are only provided as a convenience, the firm or person publishing any of these must own a legal license for each font that is being used.\n"
+ " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü"
+ "†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø¿¡¬√ƒ≈∆«»…ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔÒÚÛÙıˆ˜¯˘˙˚¸˝˛ˇ";
public RCMPDFDocument tutorial10() {
RCMPDFDocument doc = new RCMPDFDocument(); // create a new PDF document
RCMPDFPage page;
RCMPDFBox box;
int i, im;
String afmFilesList[] = RCMPDFFont.fontFilesList();
String fontName;
for (i = 0, im = afmFilesList.length; i < im; i++) { // Loop all
// fonts found
// in kit
fontName = afmFilesList[i];
page = new RCMPDFPage(doc); // create a new PDF page in doc
// Put a font descriptor box at top
box = new RCMPDFBox(0, 50, fontName);
box.setTextFont(helveticaBold28);
page.addBox(box);
// Put big size 18 box at left
box = new RCMPDFBox(0, 100, 240, 50, "Size = 18 \n\n"
+ fontTestText);
box.setTextFont(new RCMPDFFont(fontName, 18));
page.addBox(box);
// Put size 12 box at right
box = new RCMPDFBox(272, 100, 240, 50, "Size = 12 \n\n"
+ fontTestText);
box.setTextFont(new RCMPDFFont(fontName, 12));
page.addBox(box);
// Put small size 8 box 50 pixels below right box
box.wrapText(); // get the right height from previous box
box = new RCMPDFBox(272, 100 + box.height() + 50, 240, 50,
"Size = 8 \n\n" + fontTestText);
box.setTextFont(new RCMPDFFont(fontName, 8));
page.addBox(box);
}
doc.setFileName("Tutorial 10"); // optional - give PDF file a filename
return doc;
}
// Tutorial 11 - Show different page sizes -------------------------------------
public RCMPDFDocument tutorial11() {
RCMPDFDocument doc = new RCMPDFDocument(); // create a new PDF document
RCMPDFPage page;
RCMPDFBox box;
int i, im;
String papersizes[] = RCMPDFDocument.paperSizes();
String paperSize;
for (i = 0, im = papersizes.length; i < im; i++) { // Loop all sizes
// found in kit
paperSize = papersizes[i];
page = new RCMPDFPage(doc); // create a new PDF page in doc
page.setPaperSizeString(paperSize);
if (paperSize.indexOf("Env_") != -1)
page.setLandScape(true); // flip envelopes to print in landscape
// set margins based on the paper size for the sake of this tutorial
page.setMarginTop(0.10 * page.height());
page.setMarginBottom(0.10 * page.height());
page.setMarginLeft(0.10 * page.width());
page.setMarginRight(0.10 * page.width());
// Put a full blue box around the margin lines to show margins.
box = new RCMPDFBox(0, 0, page.rightXPos(), page.bottomYPos(),
paperSize + "\n" + // show paper size and dimensions
Math.round(page.width()) + " x "
+ Math.round(page.height()));
box.setStrokeColor(grey);
box.setFillColor(darkBlue);
box.setTextColor(white);
// make font size as big as possible
box.setTextFont(new RCMPDFFont("Courier", 0.1 * page.width()));
box.setVAlign("Middle");
box.setHAlign("Center");
page.addBox(box);
}
doc.setShowPageNumbers(false); // optional - do not show page numbers
doc.setFileName("Tutorial 11"); // optional - give PDF file a filename
return doc;
}
// ------------------------------------------------------------------------------
}
Main.wod - WAPDFKitTutorials
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML>
<HEAD>
<TITLE>RCMPDFKit.framework Tutorials</TITLE>
</HEAD>
<BODY>
<H2>
<WEBOBJECT NAME=Tutorial1>Tutorial 1 - Hello World</WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial2>Tutorial 2 - Hello World 2x2</WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial3>Tutorial 3 - Hello World Colors</WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial4>Tutorial 4 - Hello World Fonts</WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial5>Tutorial 5 - X</WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial6> Tutorial6 - Add Images</WEBOBJECT><BR/>
<BR/>
<WEBOBJECT NAME=Tutorial7>Tutorial 7 - Basic Table</WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial8>Tutorial 8 - Multiple Tables (75 tables - 407 pages) </WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial9>Tutorial 9 - Table with images</WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial10>Tutorial 10 - Fonts Test (366 pages - 366 fonts) </WEBOBJECT><BR/>
<WEBOBJECT NAME=Tutorial11>Tutorial 11 - International Paper Sizes </WEBOBJECT><BR/>
</H2>
</BODY>
</HTML>
Main.html - WAPDFKitTutorials
Tutorial1: RCMPDFHyperlink { displayerMethod = "tutorial1"; }
Tutorial2: RCMPDFHyperlink { displayerMethod = "tutorial2"; }
Tutorial3: RCMPDFHyperlink { displayerMethod = "tutorial3"; }
Tutorial4: WOHyperlink { action = tutorial4; target = "_blank";} //TRY CLASSIC API
Tutorial5: RCMPDFHyperlink { displayerMethod = "tutorial5"; }
Tutorial6: RCMPDFHyperlink { displayerMethod = "tutorial6"; }
Tutorial7: RCMPDFHyperlink { displayerMethod = "tutorial7"; }
Tutorial8: RCMPDFHyperlink { displayerMethod = "tutorial8"; }
Tutorial9: RCMPDFHyperlink { displayerMethod = "tutorial9"; }
Tutorial10:RCMPDFHyperlink { displayerMethod = "tutorial10";}
Tutorial11:RCMPDFHyperlink { displayerMethod = "tutorial11";}