import java.awt.Color;
import java.io.File;
import java.io.IOException;
import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfAction;
import com.gnostice.pdfone.PdfBookmark;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfImage;
import com.gnostice.pdfone.PdfMeasurement;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfPageMode;
import com.gnostice.pdfone.PdfPageSize;
import com.gnostice.pdfone.PdfPoint;
import com.gnostice.pdfone.PdfRect;
import com.gnostice.pdfone.PdfTextAnnot;
import com.gnostice.pdfone.PdfTextFormatter;
import com.gnostice.pdfone.PdfWriter;
import com.gnostice.pdfone.encodings.PdfEncodings;
import com.gnostice.pdfone.fonts.PdfFont;
import com.gnostice.pdfone.graphics.PdfPen;
public class PdfPage_Examples
{
// Activates the component PDFOne.jar
static
{
PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
"9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
}
public static void main(String[] args) throws PdfException,
IOException
{
PdfPage_Examples obj = new PdfPage_Examples();
// To try other examples, add the obj.<example_method>
// accordingly. For example, try:
// obj.PdfPage_Example1();
obj.drawLine_Example();
}
// This code segment demonstrates methods used to specify pen and
// brush settings
public void penAndBrush_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_penAndBrush_example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
PdfPage page = new PdfPage();
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Sets default brush color to green
page.setBrushColor(Color.GREEN);
// Sets default pen color to red
page.setPenColor(Color.RED);
// Sets default pen width to 10 times default
page.setPenWidth(PdfPen.DEFAULT_WIDTH * 10);
// Sets default shape of paths that are joined
page.setPenJoinStyle(PdfPen.JOINSTYLE_ROUND);
// Sets default miter limit to twice the default
page.setPenMiterLimit(PdfPen.DEFAULT_MITERLIMIT * 2);
// Creates two arrays of x and y coordinates
double x[] = { 2, 3, 3.5, 4, 3.7 };
double y[] = { 1, 0.5, 1, 3, 1.5 };
// Creates a default page (1) and draws a polygon based on
// the above arrays
page.drawPolygon(x, y, 5, true, true);
// Sets default shape of endpoints of paths that are stroked
page.setPenCapStyle(PdfPen.CAPSTYLE_PROJECTING_SQUARE);
// Draws a straight line
page.drawLine(2, 5, 5, 6);
// Sets gap length of the dash pattern
page.setPenDashGap(2);
// Sets dash length of the dash pattern
page.setPenDashLength(6);
// Sets phase of the dash pattern
page.setPenDashPhase(8);
PdfPen pen = page.getPen();
pen.strokeColor = Color.GREEN;
page.setPen(pen);
// Draws a dashed line
page.drawLine(2, 5, 3, 6);
// Identifies corners of the polygon
page.writeText(". (2, 1)", 2, 1);
page.writeText(". (3, 0.5)", 3, 0.5);
page.writeText(". (3.5, 1)", 3.5, 1);
page.writeText(". (4, 3)", 4, 3);
page.writeText(". (3.7, 1.5)", 3.7, 1.5);
page.writeText(". (3, 6)", 3, 6);
document.add(page);
// Creates and identifies bookmarks used to view specific
// destinations in the document
page.writeText(
"Use the bookmarks to check the cap and join styles");
document.addBookmark("Page 1 with normal zoom",
document.getBookmarkRoot(),
1,
0,
PdfBookmark.FITH);
PdfRect rectangle1 = new PdfRect(3.6 * 72,
2.6 * 72,
4.4 * 72,
3.4 * 72);
document.addBookmark("Check the \"line join style\"",
document.getBookmarkRoot(),
1,
rectangle1);
PdfRect rectangle2 = new PdfRect(1.65 * 72,
4.65 * 72,
2.35 * 72,
5.35 * 72);
page.writeText(". (2, 5)", 2, 5);
page.writeText(". (5, 6)", 5, 6);
document.addBookmark("Check the \"line cap style\"",
document.getBookmarkRoot(),
1,
rectangle2);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment demonstrates the use of AddAction overloaded
// methods
public void AddAction_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_AddAction_example.pdf");
PdfDocument document = new PdfDocument(writer);
PdfPage page;
// Creates five pages and adds them to the document
for (int i = 1; i <= 5; i++)
{
page = new PdfPage();
page.writeText("This is page #" + i);
document.add(page);
}
// Adds page 6, which resolves the Gnostice home page URI
// when it is closed
PdfPage url_page = new PdfPage();
url_page.writeText(
"When you leave this page, a browser window will be "
+ "opened with the Gnostice website URL");
url_page.addAction(PdfAction.PdfEvent.ON_PAGE_CLOSE,
PdfAction.URI,
"http://www.gnostice.com/");
document.add(url_page);
// Adds page 7, which executes a Javascript script when
// it is opened
PdfPage js_page = new PdfPage();
js_page.writeText(
"When you display this page, a Javascript message "
+ "window will be displayed.");
js_page.addAction(
PdfAction.PdfEvent.ON_PAGE_OPEN,
PdfAction.JAVASCRIPT,
"app.alert('This is a Javascript message!')");
document.add(js_page);
// Creates a test PDF file for use it with code segment below
char pathSeparator = File.separatorChar;
String testFile = "."
+ pathSeparator
+ "InputDocs"
+ pathSeparator
+ "test_pdf_file.pdf";
PdfWriter writer2 = PdfWriter.fileWriter(testFile);
PdfDocument document2 = new PdfDocument(writer2);
PdfPage page2;
for (int i =1; i <=5; i++) {
page2 = new PdfPage();
if (i == 2) {
page2.writeText(". (200, 100)", 200, 100);
page2.writeText(". (200, 50)", 200, 50);
}
page2.writeText(
"This is page #" + i + " of " + testFile);
document2.add(page2);
}
document2.write();
writer2.dispose();
// Adds page 8, which prints the above file when it is closed
PdfPage app_page = new PdfPage();
app_page.writeText("When you leave this page, the "
+ testFile
+ " file will be printed");
app_page.addAction(
PdfAction.PdfEvent.ON_PAGE_OPEN,
PdfAction.LAUNCH,
testFile,
true);
document.add(app_page);
// Adds page 9, which leads to page 1 when it is closed
PdfPage switch_page = new PdfPage();
switch_page.writeText(
"When you leave this page, you will be taken to the "
+ "first page");
switch_page.addAction(PdfAction.PdfEvent.ON_PAGE_CLOSE,
PdfAction.NAMED_FIRSTPAGE);
document.add(switch_page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws several squares using overloaded
// methods
public void DrawSquare_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_DrawSquare_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a new page
PdfPage page = new PdfPage();
// Sets default page measurement unit to inch
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Sets pen and brush colors
page.setPenColor(Color.GREEN);
page.setBrushColor(Color.ORANGE);
// Draws square at position (2, 1) with side length at 1 inch
page.drawSquare(2, 1, 1);
// Draws square at position (5, 3) with side length at 1
// inch. The square is also stroked and filled.
page.drawSquare(5, 3, 1, true, true);
// Creates two PdfPoint objects
PdfPoint point1 = new PdfPoint(5, 5);
PdfPoint point2 = new PdfPoint(2, 7);
// Draws a square at the first of the above points.
page.drawSquare(point1, 1);
// Draws a square at the second point. The square as a side
// length of 1 inch. The square is filled but not stroked.
page.drawSquare(point2, 1, true, false);
// Writes text identifying the above squares
page.writeText(
"drawSquare(double x, double y, double length) ~ "
+ "drawSquare(2, 1, 1)",
new PdfRect(2, 0.6, 3, 0.4));
page.writeText(
"drawSquare(double x, double y, double length, boolean "
+ "isFill, boolean isStroke) ~ drawSquare(5, 3, 1, "
+ "true, true)",
new PdfRect(4, 2.6, 4, 0.4));
page.writeText(
"drawSquare(PdfPoint p, double length) ~ "
+ "drawSquare(point1, 1)",
new PdfRect(4, 4.6, 4, 0.4));
page.writeText(
"drawSquare(PdfPoint p, double length, boolean "
+ "isFill, boolean isStroke) ~ drawSquare(point2, "
+ "1, true, false)",
new PdfRect(2, 6.6, 4, 0.4));
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws rectangles with rounded corners using
// overloaded methods
public void DrawRoundRect_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_DrawRoundRect_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a new page
PdfPage page = new PdfPage();
page.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Sets pen and brush colors
page.setPenColor(Color.RED);
page.setBrushColor(Color.GREEN);
// Creates a default page (1) and draws a rectangle with
// rounded corners
page.drawRoundRect(100, 100, // x and y
300, 300, // width and height
50, 100, // arcWidth and arcHeight
true, // isFill
true); // isStroke
// Writes content identifying the above rectangle
page.drawLine(100, 410, 150, 410);
page.drawLine(100, 407, 100, 413);
page.drawLine(150, 407, 150, 413);
page.writeText("arcWidth = 50", 80, 420);
page.drawLine(413, 100, 419, 100);
page.drawLine(413, 200, 419, 200);
page.drawLine(416, 100, 416, 200);
page.writeText("arcHeight = 100", 430, 140);
page.writeText(".", 100, 100);
page.writeText("(x,y) = (100, 100)", 70, 80);
page.writeText(
"drawRoundRect(double x, double y, double width, "
+ "double height, double arcWidth, double "
+ "arcHeight, boolean isFill, boolean isStroke) "
+ "~ drawRoundRect(100, 100, 300, 300, 50, 100, "
+ "true, true)",
new PdfRect(400, 350, 200, 200));
// Creates a rectangle
PdfRect rectangle = new PdfRect(200, 550, 300, 300);
// Draws a rectangle with rounded corners inside the
// above rectangle
page.drawRoundRect(rectangle, 50, 100, true, true);
page.writeText(
"drawRoundRect(PdfRect rect, double arcWidth, double "
+ "arcHeight, boolean isFill, boolean isStroke) ~ "
+ "drawRoundRect(rectangle, 50, 100, true, true)",
new PdfRect(510, 800, 200, 200));
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws several Bézier curves using
// overloaded methods
public void drawBezierCurve_Example2()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawBezier_example2.pdf");
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
// Sets pen and brush colors
page.setPenColor(Color.RED);
page.setBrushColor(Color.GREEN);
// Create starting, control, and end points for Bézier curves
PdfPoint s_point1 = new PdfPoint(150, 100);
PdfPoint c_point1 = new PdfPoint(200, 50);
PdfPoint e_point1 = new PdfPoint(250, 150);
PdfPoint s_point2 = new PdfPoint(250, 100);
PdfPoint c_point2 = new PdfPoint(300, 50);
PdfPoint e_point2 = new PdfPoint(350, 150);
PdfPoint s_point3 = new PdfPoint(350, 100);
PdfPoint c_point3 = new PdfPoint(400, 50);
PdfPoint e_point3 = new PdfPoint(450, 150);
// Draws Bézier curves that require just one control point
page.drawBezierCurve(s_point1, // starting point
c_point1, // control point
e_point1, // end point
false, // fill setting
true); // stroke setting
page.drawBezierCurve(s_point2,
c_point2,
e_point2,
true,
true);
page.drawBezierCurve(s_point3,
c_point3,
e_point3,
true,
false);
// Writes text identifying the above Bézier curves
page.writeText("isFill = false", 160, 185);
page.writeText("isStroke = true", 160, 195);
page.writeText("isFill = true", 260, 185);
page.writeText("isStroke = true", 260, 195);
page.writeText("isFill = true", 360, 185);
page.writeText("isStroke = false", 360, 195);
page.writeText(". (150, 100) starting point ", 150, 100);
page.writeText(". (200, 50) control point", 200, 50);
page.writeText(". (250, 150) end point", 250, 150);
PdfPoint s_point4 = new PdfPoint(150, 400);
PdfPoint c1_point4 = new PdfPoint(200, 650);
PdfPoint c2_point4 = new PdfPoint(250, 330);
PdfPoint e_point4 = new PdfPoint(300, 550);
// Draws a Bézier curve with two control points
page.drawBezierCurve(s_point4, // starting point
c1_point4, // first control point
c2_point4, // second control point
e_point4, // end point
false, // fill setting
true); // stroke setting
// Writes text identifying the above curve
page.writeText(". (150, 400) starting point", s_point4);
page.writeText(
". (200, 650) first control point", c1_point4);
page.writeText(
". (250, 330) second control point", c2_point4);
page.writeText(". (300, 550) end point", e_point4);
page.writeText("isFill = false", 150, 550);
page.writeText("isStroke = true", 150, 560);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws several Bézier curves using
// overloaded methods
public void drawBezierCurve_Example1()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawBezier_example1.pdf");
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
// Sets pen and brush colors
page.setPenColor(Color.RED);
page.setBrushColor(Color.GREEN);
// Draws Bézier curves that require just one control point
page.drawBezierCurve(150, 100, // starting point
200, 50, // control point
250, 150, // end point
false, // fill setting
true); // stroke setting
page.drawBezierCurve(250, 100,
300, 50,
350, 150,
true,
true);
page.drawBezierCurve(350, 100,
400, 50,
450, 150,
true,
false);
// Writes text identifying the above Bézier curves
page.writeText("isFill = false", 160, 185);
page.writeText("isStroke = true", 160, 195);
page.writeText("isFill = true", 260, 185);
page.writeText("isStroke = true", 260, 195);
page.writeText("isFill = true", 360, 185);
page.writeText("isStroke = false", 360, 195);
page.writeText(". (150, 100) starting point ", 150, 100);
page.writeText(". (200, 50) control point", 200, 50);
page.writeText(". (250, 150) end point", 250, 150);
// Draws a Bézier curve with two control points
page.drawBezierCurve(150, 400, // starting point
200, 650, // first control point
250, 330, // second control point
300, 550, // end point
false, // fill setting
true); // stroke setting
// Writes text identifying the above curve
page.writeText(". (150, 400) starting point", 150, 400);
page.writeText(". (200, 650) first control point", 200, 650);
page.writeText(". (250, 330) second control point",250, 330);
page.writeText(". (300, 550) end point", 300, 550);
page.writeText("isFill = false", 150, 550);
page.writeText("isStroke = true", 150, 560);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment demonstrates how to retrieve and set the
// default measurement unit of a page
public void MeasurementUnit_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_MeasurementUnit_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a new page
PdfPage page = new PdfPage();
// Writes text at position (100, 100), which is expressed in
// points, the default measurement unit
page.writeText(". (100, 100) points", 100, 100);
// Checks and sets measurement unit to inches
if (page.getMeasurementUnit() != PdfMeasurement.MU_INCHES)
{
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
}
// Writes text at position (2, 1), which is now expressed in
// inches
page.writeText(". (2, 3) inches", 2, 3);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment demonstrates retrieving and specifying text
// formatting settings of a page
public void TextFormatter_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_TextFormatter_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a page
PdfPage page = new PdfPage();
String s = "Four score and seven years ago our fathers "
+ "brought forth on this continent a new "
+ "nation conceived in liberty and dedicated "
+ "to the proposition that all men are created "
+ "equal. ";
// Writes text on the page using default formatting settings
page.writeText(s, 100, 100);
// Retrieves current text formatting settings of the page
// to a PdfTextFormatter object
PdfTextFormatter format = page.getTextFormatter();
// Changes text alignment of the PdfTextFormatter object to
// centered text
if (format.getAlignment() != PdfTextFormatter.CENTER)
format.setAlignment(PdfTextFormatter.CENTER);
// Specifies new text formatting settings using the
// PdfTextFormatter object
page.setTextFormatter(format);
// Writes text on the page using the new text formatting
// settings
page.writeText(s, 0, 200);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws rectangles on a page with several
// overloaded methods
public void drawRect_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawRect_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a page
PdfPage page = new PdfPage();
// Creates two points
PdfPoint point1 = new PdfPoint(100, 500);
PdfPoint point2 = new PdfPoint(100, 650);
// Creates a rectangle
PdfRect rectangle = new PdfRect(250, 625, 250, 100);
// Sets pen and brush colors for the page
page.setBrushColor(Color.ORANGE);
page.setPenColor(Color.RED);
// Draws a rectangle on the page at position (100, 100) with
// width 400 and height 75
page.drawRect(100, 100, 400, 75);
// Draws a rectangle on the page at position (200, 300) with
// width 400 and height 100. The fill and brush settings are
// enabled.
page.drawRect(200, 300, 400, 100, true, true);
// Draws a rectangle at point 1 with width 300 and height 75
page.drawRect(point1, 300, 75);
// Draws a rectangle at point 2 with width 50 and height 50
page.drawRect(point2, 125, 125, true, false);
// Draws a rectangle on the specified rectangle
page.drawRect(rectangle);
// Writes text identifying the above rectangles
page.writeText(
"drawRect(double x, double y, double width, "
+ "double height) ~ drawRect(100, 100, 400, 75)",
new PdfRect(100, 100, 400, 75));
page.writeText(
"drawRect(double x, double y, double width, "
+ "double height, boolean isFill, boolean "
+ "isStroke) ~ drawRect(200, 300, 400, 100, true, "
+ "true)",
new PdfRect(200, 300, 400, 100));
page.writeText(
"drawRect(PdfPoint p, double width, double "
+ "height) ~ drawRect(point1, 300, 75)",
new PdfRect(100, 500, 300, 75));
page.writeText(
"drawRect(PdfPoint p, double width, double "
+ "height, boolean isFill, boolean isStroke) "
+ "~ drawRect(point2, 125, 125, true, false)",
new PdfRect(100, 650, 125, 125));
page.writeText("drawRect(PdfRect r) ~ drawRect(rectangle)",
rectangle);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws a polygon and a polyline
public void drawPolygon_drawPolyline_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawPolygon_drawPolyline_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Create two pages
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
// Creates two arrays of x and y coordinates
double x[] = { 100, 200, 300, 400, 100 };
double y[] = { 100, 50, 100, 300, 100 };
// Sets pen and brush colors for the two pages
page1.setBrushColor(Color.ORANGE);
page1.setPenColor(Color.RED);
page2.setBrushColor(Color.ORANGE);
page2.setPenColor(Color.RED);
// Draws a polygon using the five coordinates
page1.drawPolygon(x, y, 5, true, true);
// Draws a polyline using the first four coordinates
page2.drawPolyline(x, y, 4);
document.add(page1);
document.add(page2);
// Writes text identifying the coordinates on pages 1 & 2
document.writeText(". (100, 100)", 100, 100, "1,2");
document.writeText(". (200, 50)", 200, 50, "1,2");
document.writeText(". (300, 100)", 300, 100, "1,2");
document.writeText(". (400, 300)", 400, 300, "1,2");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws a pie
public void drawPie_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawPie_example.pdf");
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
// Draws a full pie (from 0 degree to 360 degrees) for
// reference
page.drawPie(100, 200, 300, 100, 0.0, 360.0, false, true);
// Draws the bounding box
page.drawRect(100, 200, 300, 100);
// Sets pen and brush colors
page.setBrushColor(Color.ORANGE);
page.setPenColor(Color.RED);
// Draws a pie segment from 0 degree to 45 degrees.
// It is 300 points wide and 100 points tall.
// Its bounding box is at position (100, 200).
page.drawPie(100, 200,
300, 100,
0.0, 45.0,
true, true);
// Writes text identifying the top-left corner of bounding box
// of the full pie containing the above pie segment
page.writeText(". (100, 200)", 100, 200);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws lines using two overloaded methods
public void drawLine_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawLine_example.pdf");
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
// Creates two points
PdfPoint point1 = new PdfPoint(100, 500);
PdfPoint point2 = new PdfPoint(400, 100);
// Draws a line from position (100, 100) to
// position (400, 500)
page.drawLine(100, 100, 400, 500);
// Draws a line from the first point (100, 500) to second
// point (400, 100)
page.drawLine(point1, point2);
// Writes text identifying the above lines
page.writeText(". (100, 100)", 100, 100);
page.writeText(". (400, 500)", 400, 500);
page.writeText(". (100, 500)", point1);
page.writeText(". (400, 100)", point2);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segments draws images using several overloaded
// methods
public void drawImage_Example2() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawImage_example2.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates three pages
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Sets measurement unit for the pages
page1.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
page2.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
char pathSeparator = File.separatorChar;
// Creates an image object with an image file pathname.
// Assumes that this image exists inside a directory
// "InputDocs" under the current directory.
PdfImage image = PdfImage.create(
"."
+ pathSeparator
+ "InputDocs"
+ pathSeparator
+ "banner_img_468_60.jpg");
// Draws image at position (100, 100)
page1.drawImage(image, 100, 100);
// Draws image, rotated by 5 degrees, at position (100, 300)
page1.drawImage(image, 100, 300, 5.0);
// Draws image, resized to 234 x 30 pixels, at position
// (100, 500)
page1.drawImage(image, 100, 500, 234, 30);
// Draws image, resized to 117 x 15 pixels and rotated by
// 355 degrees, at position (100, 700)
page1.drawImage(image, 100, 700, 117, 15, 355.0);
// Writes text identifying the above images
page1.writeText(
"drawImage(PdfImage img, double x, double y) ~ "
+ "drawImage(image, 100, 100)",
100, 70);
page1.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "double rotation) ~ drawImage(image, 100, 300, "
+ "5.0)",
100, 260);
page1.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "double width, double height) ~ drawImage(image, "
+ "100, 500, 234, 30)",
100, 460);
page1.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "double width, double height, double rotation) "
+ "~ drawImage(image, 100, 700, 117, 15, 355.0)",
100, 660);
// Creates four points
PdfPoint point1 = new PdfPoint(100, 100);
PdfPoint point2 = new PdfPoint(100, 300);
PdfPoint point3 = new PdfPoint(100, 500);
PdfPoint point4 = new PdfPoint(100, 700);
// Draws image at the first point
page2.drawImage(image, point1);
// Draws image, rotated by 5 degrees, at the second point
page2.drawImage(image, point2, 5.0);
// Draws image, resized to 234 x 30 pixels, at the third
// point
page2.drawImage(image, point3, 234, 30);
// Draws image, resized to 117 x 15 pixels and rotated by 5
// degrees, at the fourth point
page2.drawImage(image, point4, 117, 15, 355.0);
page2.writeText(
"drawImage(PdfImage img, PdfPoint p) ~ drawImage"
+ "(image, point1)",
100, 70);
page2.writeText(
"drawImage(PdfImage img, PdfPoint p, double "
+ "rotation) ~ drawImage(image, point2, 5.0)",
100, 260);
page2.writeText(
"drawImage(PdfImage img, PdfPoint p, double width, "
+ "double height) ~ drawImage(image, point3, 234, "
+ "30)",
100, 460);
page2.writeText(
"drawImage(PdfImage img, PdfPoint p, double width, "
+ "double height, double rotation) ~ drawImage("
+ "image, point4, 117, 15, 355.0)",
100, 660);
// Creates two rectangles.
// Rectangles are specified in points.
// 1 pixel = 0.75 point
PdfRect rect1 = new PdfRect(100 * 0.75,
100 * 0.75,
468 * 0.75,
60 * 0.75);
PdfRect rect2 = new PdfRect(100 * 0.75,
300 * 0.75,
468 * 0.75,
60 * 0.75);
// Draws only the second rectangle.
// Drawing the first rectangle would be a waste, as it
// will be obscured by the image drawn in it.
page3.drawRect(rect2);
// Draws image inside first rectangle
page3.drawImage(image, rect1);
// Draws image, rotated by 5 degrees, inside second rectangle
page3.drawImage(image, rect2, 5.0);
page3.writeText("drawImage(PdfImage img, PdfRect rect) ~ "
+ "drawImage(image, rect1)",
100 * 0.75,
70 * 0.75);
page3.writeText("drawImage(PdfImage img, PdfRect rect, "
+ "double rotation) ~ drawImage(image, rect2, "
+ "5.0)",
100 * 0.75,
270 * 0.75);
document.add(page1);
document.add(page2);
document.add(page3);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segments draws images using several overloaded
// methods
public void drawImage_Example1() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawImage_example1.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates three pages
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Sets measurement unit for the pages
page1.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
page2.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
char pathSeparator = File.separatorChar;
// Creates a string representing an image pathname.
// Assumes that this images exists inside a directory
// "InputDocs" under the current directory.
String imagePathname = "."
+ pathSeparator
+ "InputDocs"
+ pathSeparator
+ "banner_img_468_60.jpg";
// Draws image at position (100, 100)
page1.drawImage(imagePathname, 100, 100);
// Draws image, rotated by 5 degrees, at position (100, 300)
page1.drawImage(imagePathname, 100, 300, 5.0);
// Draws image, resized to 234 x 30 pixels, at position
// (100, 500)
page1.drawImage(imagePathname, 100, 500, 234, 30);
// Draws image, resized to 117 x 15 pixels and rotated by
// 355 degrees, at position (100, 700)
page1.drawImage(imagePathname, 100, 700, 117, 15, 355.0);
// Writes text identifying the above images
page1.writeText(
"drawImage(String path, double x, double y) ~ "
+ "drawImage(imagePath, 100, 100)",
100, 70);
page1.writeText(
"drawImage(String path, double x, double y, double "
+ "rotation) ~ drawImage(imagePath, 100, 300, 5.0)",
100, 260);
page1.writeText(
"drawImage(String path, double x, double y, double "
+ "width, double height) ~ drawImage(imagePath, "
+ "100, 500, 234, 30)",
100, 460);
page1.writeText(
"drawImage(String path, double x, double y, double "
+ "width, double height, double rotation) ~ "
+ "drawImage(imagePath, 100, 700, 117, 15, 355.0)",
100, 660);
// Creates four points
PdfPoint point1 = new PdfPoint(100, 100);
PdfPoint point2 = new PdfPoint(100, 300);
PdfPoint point3 = new PdfPoint(100, 500);
PdfPoint point4 = new PdfPoint(100, 700);
// Draws image at the first point
page2.drawImage(imagePathname, point1);
// Draws image, rotated by 5 degrees, at the second point
page2.drawImage(imagePathname, point2, 5.0);
// Draws image, resized to 234 x 30 pixels, at the third
// point
page2.drawImage(imagePathname, point3, 234, 30);
// Draws image, resized to 117 x 15 pixels and rotated by 5
// degrees, at the fourth point
page2.drawImage(imagePathname, point4, 117, 15, 355.0);
page2.writeText(
"drawImage(String path, PdfPoint p) ~ drawImage"
+ "(imagePath, point1)",
100, 70);
page2.writeText(
"drawImage(String path, PdfPoint p, double "
+ "rotation) ~ drawImage(imagePath, point2, 5.0)",
100, 260);
page2.writeText(
"drawImage(String path, PdfPoint p, double width, "
+ "double height) ~ drawImage(imagePath, point3, "
+ "234, 30)",
100, 460);
page2.writeText(
"drawImage(String path, PdfPoint p, double width, "
+ "double height, double rotation) ~ drawImage"
+ "(imagePath, point4, 117, 15, 355.0)",
100, 660);
// Creates two rectangles.
// Rectangles are specified in points.
// 1 pixel = 0.75 point
PdfRect rect1 = new PdfRect(100 * 0.75,
100 * 0.75,
468 * 0.75,
60 * 0.75);
PdfRect rect2 = new PdfRect(100 * 0.75,
300 * 0.75,
468 * 0.75,
60 * 0.75);
// Draws only the second rectangle.
// Drawing the first rectangle would be a waste, as it
// will be obscured by the image drawn in it.
page3.drawRect(rect2);
// Draws image inside first rectangle
page3.drawImage(imagePathname, rect1);
// Draws image, rotated by 5 degrees, inside second rectangle
page3.drawImage(imagePathname, rect2, 5.0);
page3.writeText("drawImage(String path, PdfRect rect) ~ "
+ "drawImage(imagePathname, rect1)",
100 * 0.75,
70 * 0.75);
page3.writeText("drawImage(String path, PdfRect rect, "
+ "double rotation) ~ drawImage("
+ "imagePathname, rect2, 5.0)",
100 * 0.75,
270 * 0.75);
document.add(page1);
document.add(page2);
document.add(page3);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws ellipses using several overloaded
// methods
public void drawEllipse_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawEllipse_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a page
PdfPage page = new PdfPage();
// Sets page measurement unit to inches
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Sets pen and brush colors
page.setBrushColor(Color.ORANGE);
page.setPenColor(Color.RED);
// Creates two points
PdfPoint point1 = new PdfPoint(2, 4);
PdfPoint point2 = new PdfPoint(6, 5);
// Draws an ellipse whose bounding box has its top-left corner
// at (2, 1) and bottom-right corner at (3.5, 3)
page.drawEllipse(2, 1, 3.5, 3, true, true);
// Draws an ellipse whose bounding box has its top-left corner
// at point1 and bottom-right corner at point2
page.drawEllipse(point1, point2, true, true);
// Writes text identifying the above ellipses
page.writeText(". (2, 1)", 2, 1);
page.writeText(". (3.5, 3)", 3.5, 3);
page.writeText(". (2, 4)", 2, 4);
page.writeText(". (6, 5)", 6, 5);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draw circles with several overloaded methods
public void drawCircle_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawCircle_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a page
PdfPage page = new PdfPage();
// Sets page measurement unit to inches
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Creates a point
PdfPoint point = new PdfPoint(5, 7);
// Sets pen and brush colors
page.setBrushColor(Color.ORANGE);
page.setPenColor(Color.RED);
// Draws a circle that will be filled and will have its
// border drawn
page.drawCircle(2, 2, 1, true, true);
// Draws a circle that will not be filled but will have itsx
// border drawn
page.drawCircle(6, 2, 1, false, true);
// Draws a circle that will be filled and will not have its
// border drawn
page.drawCircle(4, 5, 1, true, false);
// Draws a circle at the point (5, 7)
page.drawCircle(point, 1, true, true);
// Writes text identifying the above circles
page.writeText("isFill = true", 1.5, 0.5);
page.writeText("isStroke = true", 1.5, 0.7);
page.writeText("isFill = false", 5.5, 0.5);
page.writeText("isStroke = true", 5.5, 0.7);
page.writeText("isFill = true", 3.5, 3.5);
page.writeText("isStroke = false", 3.5, 3.7);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment demonstrates how margins can be disabled and
// enabled on page.
public void AllMargins_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_AllMargins_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a A4-size page with margins 50-points long
PdfPage page = new PdfPage(PdfPageSize.A4,
50, 50, 50, 50,
PdfMeasurement.MU_POINTS);
String s = "Four score and seven years ago our fathers "
+ "brought forth on this continent a new "
+ "nation conceived in liberty and dedicated "
+ "to the proposition that all men are created "
+ "equal. ";
// Disables all margins on the page
page.disableAllMargins();
// Writes text to illustrate the effect of the above statement
page.writeText("[AFTER disableAllMargins()] " + s);
// Enables all margins on the page
page.enableAllMargins();
// Writes text to illustrate the effect of the above statement
page.writeText("[AFTER enableAllMargins()] " + s);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds watermarks using overloaded methods
public void AddWatermark_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_AddWatermark_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a PdfImage object for use in watermarks
PdfImage image = PdfImage.create(
".\\InputDocs\\banner_img_468_60.jpg");
// Creates pages with specific margins
PdfPage page1 = new PdfPage(
PdfPageSize.A5, 25, 25, 25, 25, PdfMeasurement.MU_PIXELS);
PdfPage page2 = new PdfPage(
PdfPageSize.A5, 25, 25, 25, 25, PdfMeasurement.MU_PIXELS);
PdfPage page3 = new PdfPage(
PdfPageSize.A5, 25, 25, 25, 25, PdfMeasurement.MU_PIXELS);
PdfPage page4 = new PdfPage(
PdfPageSize.A5, 25, 25, 25, 25, PdfMeasurement.MU_PIXELS);
PdfPage page5 = new PdfPage(
PdfPageSize.A5, 25, 25, 25, 25, PdfMeasurement.MU_PIXELS);
PdfPage page6 = new PdfPage(
PdfPageSize.A5, 25, 25, 25, 25, PdfMeasurement.MU_PIXELS);
// Creates two font objects on which will be used to
// write the watermark
PdfFont fontCourier = PdfFont.create("Courier",
PdfFont.BOLD,
32,
PdfEncodings.WINANSI);
PdfFont fontArial = PdfFont.create("Arial",
PdfFont.ITALIC,
18,
PdfEncodings.WINANSI);
// Adds PdfImage object as a watermark. It by default
// attempts to write and align the watermark on the whole of
// the page.
page1.addWatermarkImage(
image,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
45.0,
true);
// Writes text identifying the above watermark
page1.writeText(
"addWatermarkImage(PdfImage image, int position, double "
+ "angle, boolean underlay) ~ addWatermarkImage(image, "
+ "PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE, 45.0, true)",
fontArial);
// Adds PdfImage object as a watermark. It attempts to write
// and align the watermark within the margins on the page.
page2.addWatermarkImage(
image,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
true, // applyMargins
45.0,
true);
page2.writeText(
"addWatermarkImage(PdfImage image, int position, "
+ "boolean applyMargins, double angle, boolean "
+ "underlay) ~ addWatermarkImage(image, PdfPage.HP_"
+ "MIDDLE | PdfPage.VP_CENTRE, true, 45.0, true)",
fontArial);
// Adds image, specified by its pathname, as watermark. It by
// default attempts to write and align the watermark on the
// whole of the page.
page3.addWatermarkImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
45.0,
true);
page3.writeText(
"addWatermarkImage(String path, int position, "
+ "double angle, boolean underlay) ~ "
+ "addWatermarkImage(\".\\InputDocs\\banner_img_"
+ "468_60.jpg\", PdfPage.HP_MIDDLE | "
+ "PdfPage.VP_CENTRE, true,45.0, true)", fontArial);
// Adds image, specified by its pathname, as watermark.
// It attempts to write and align the watermark within
// the margins on the page.
page4.addWatermarkImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
true, // applyMargins
45.0,
true);
page4.writeText(
"addWatermarkImage(String path, int position, "
+ "boolean applyMargins, double angle, boolean "
+ "underlay) ~ .addWatermarkImage(\".\\InputDocs"
+ "\\banner_img_468_60.jpg\", PdfPage.HP_MIDDLE | "
+ "PdfPage.VP_CENTRE, true, 45.0, true)", fontArial);
// Adds text as watermark. It writes and attempts to align
// the watermark within the margins of the page.
page5.addWatermarkText(
"CONFIDENTIAL: For Eyes Only",
fontCourier,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
true, // applyMargins
45.0,
true);
page5.writeText(
"addWatermarkText(String text, PdfFont font, int "
+ "position, boolean applyMargins, double angle, "
+ "boolean underlay) ~ addWatermarkText(\"CONFIDENTIAL:"
+ "For Eyes Only\", fontCourier, PdfPage.HP_MIDDLE | "
+ "PdfPage.VP_CENTRE, true, 45.0, true)", fontArial);
// Adds text as watermark. It by default attempts to
// write and align the watermark on the whole of the page
page6.addWatermarkText("CONFIDENTIAL: For Eyes Only",
fontCourier,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
45.0,
true);
page6.writeText(
"addWatermarkText(String text, PdfFont font, int "
+ "position, double angle, boolean underlay) ~ "
+ "addWatermarkText(\"CONFIDENTIAL: For Eyes Only\", "
+ "fontCourier, PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE, "
+ "45.0, true)", fontArial);
document.add(page1);
document.add(page2);
document.add(page3);
document.add(page4);
document.add(page5);
document.add(page6);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segments adds images, specified by their pathnames,
// to a page's header and footer
public void HeaderNFooter_Example2() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_HeaderNFooter_example2.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a A4-size page with 100-pixel-long header and
// footer
PdfPage page = new PdfPage(PdfPageSize.A4,
100, 100,
50, 50, 50, 50,
PdfMeasurement.MU_PIXELS);
// Adds the first image to top-left corner of the header.
// Assumes that a file with the specified pathname exists.
page.addHeaderImage(".\\InputDocs\\banner_img_160_50.jpg",
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true);
// Adds the second image on the top-left corner of the
// footer. Assumes that a file with the specified pathname
// exists.
page.addFooterImage(".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds text and images to a pages'
// header and footer
public void HeaderNFooter_Example1() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_HeaderNFooter_example1.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a A4-size page with 100-pixel-long header and
// footer
PdfPage page = new PdfPage(PdfPageSize.A4,
100, 100,
50, 50, 50, 50,
PdfMeasurement.MU_PIXELS);
// Creates PdfImage objects for use in header and footer
PdfImage image1 = PdfImage.create(
".\\InputDocs\\banner_img_160_50.jpg");
PdfImage image2 = PdfImage.create(
".\\InputDocs\\banner_img_468_60.jpg");
// Creates a PdfFont object
PdfFont fontArial = PdfFont.create("Arial",
PdfFont.ITALIC,
14,
PdfEncodings.WINANSI);
// Adds the first image on the top-left corner of the header
page.addHeaderImage(image1,
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true);
// Adds the second image on the top-left corner of the footer
page.addFooterImage(image2,
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true);
// Adds text in the bottom-right corner of the header
page.addHeaderText(
"Gnostice Information Technologies Private Limited",
fontArial,
PdfPage.HP_RIGHT | PdfPage.VP_BOTTOM,
true);
// Adds text in the bottom-right corner of the footer
page.addFooterText(
"Bangalore, India",
fontArial,
PdfPage.HP_RIGHT | PdfPage.VP_BOTTOM,
true);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment demonstrates how to add an annotation to a
// page
public void AddAnnotation_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_AddAnnotation_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a text annotation
PdfTextAnnot tAnnot = new PdfTextAnnot(
100, 100,
PdfTextAnnot.ICON_NOTE,
false);
// Sets properties of the annotation
tAnnot.setColor(Color.ORANGE);
tAnnot.setContents(
"This is to demonstrate how to add a text "
+ "annotation to a page");
tAnnot.setTitle("Adding a Text Annotation to a Page");
tAnnot.setSubject("A demonstration");
PdfPage page = new PdfPage();
// Adds the annotation to the page
page.addAnnotation(tAnnot);
page.writeText("Check this out!", 120, 100);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws an arc
public void drawArc_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_drawArc_example.pdf");
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
// Creates a rectangle
PdfRect rectangle = new PdfRect(150, 200, 300, 300);
// Draws an arc
page.drawArc(rectangle, 22.5, 235);
// Draws a rectangle to identify the bounding box of the
// imaginary circle that completes the arc
page.drawRect(rectangle);
document.add(page);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment writes text on pages using several
// overloaded methods
public void writeText_Example4() throws IOException, PdfException
{
// Creates a PdfWriter instance
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_writeText_example4.pdf");
// Creates a PdfDocument instance with the PdfWriter instance
PdfDocument document = new PdfDocument(writer);
PdfPage page1 = new PdfPage();
String s1 = "1. writeText(String str) ~ writeText(s1)";
// Writes specified text
page1.writeText(s1);
PdfPage page2 = new PdfPage(300, 300);
String s2 =
"2. writeText(String str, boolean wrap) ~ writeText(s2, "
+ "PdfTextFormatter.WRAP)";
// Writes wrapped text
page2.writeText(s2, PdfTextFormatter.WRAP);
PdfPage page3 = new PdfPage();
String s3 =
"3. writeText(String str, int alignment) ~ "
+ "writeText(s3, PdfTextFormatter.CENTER)";
// Writes right-aligned text
page3.writeText(s3, PdfTextFormatter.RIGHT);
PdfPage page4 = new PdfPage(500, 500);
String s4 =
"4. writeText(String str, int alignment, boolean "
+ "wrap) ~ writeText(s4, PdfTextFormatter.LEFT, "
+ "PdfTextFormatter.WRAP)";
// Writes left-aligned and wrapped
page4.writeText(s4,
PdfTextFormatter.LEFT,
PdfTextFormatter.WRAP);
String s5 =
"5. writeText(String str, double x, double y) ~ "
+ "writeText(s5, 100, 100)";
// Writes text at position (100, 100)
page4.writeText(s5, 100, 100);
String s6 =
"6. writeText(String str, double x, double y, boolean "
+ "wrap) ~ writeText(s6, 100, 400, "
+ "PdfTextFormatter.NO_WRAP)";
// Writes wrapped text at position (100, 200)
page4.writeText(s6, 100, 200, PdfTextFormatter.NO_WRAP);
String s7 =
"7. writeText(String s, double x, double y, double "
+ "rotation) ~ writeText(s7, 100, 200, 5.0)";
// Writes text rotated by 5 degrees at position (100, 300)
page4.writeText(s7, 100, 300, 5.0);
String s8 =
"8. writeText(String str, double x, double y, int "
+ "alignment) ~ writeText(s8, 400, 300, "
+ "PdfTextFormatter.CENTER)";
// Writes center-aligned text at position (400, 400)
page4.writeText(s8, 400, 400, PdfTextFormatter.CENTER);
String s9 =
"9. writeText(String str, double x, double y, int "
+ "alignment, boolean wrap) ~ writeText(s9, 400, 400, "
+ "PdfTextFormatter.CENTER, PdfTextFormatter.NO_WRAP)";
// Writes center-aligned and non-wrapped text at position
// (100, 500)
page4.writeText(s9,
100, 500,
PdfTextFormatter.CENTER,
PdfTextFormatter.NO_WRAP);
document.add(page1);
document.add(page2);
document.add(page3);
document.add(page4);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment writes text on pages using several
// overloaded methods
public void writeText_Example3() throws IOException, PdfException
{
// Creates a PdfWriter instance
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_writeText_example3.pdf");
// Creates a PdfDocument instance with the PdfWriter instance
PdfDocument document = new PdfDocument(writer);
// Creates an Arial font object
PdfFont fontArial = PdfFont.create(
"Arial",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL,
16,
PdfEncodings.WINANSI);
PdfPage page1 = new PdfPage();
String s1 =
"1. writeText(String str, PdfFont f) ~ writeText(s1, "
+ "fontArial)";
// Writes text with Arial font
page1.writeText(s1, fontArial);
PdfPage page2 = new PdfPage();
String s2 =
"2. writeText(String str, PdfFont f, boolean wrap) ~ "
+ "writeText(s2, fontArial, PdfTextFormatter.WRAP);";
// Writes wrapped text with Arial font
page2.writeText(s2, fontArial, PdfTextFormatter.WRAP);
PdfPage page3 = new PdfPage();
String s3 =
"3. writeText(String str, PdfFont f, double x, double "
+ "y) ~ writeText(s3, fontArial, 100, 100)";
// Writes text with Arial font at position (100, 100)
page3.writeText(s3, fontArial, 100, 100);
PdfPage page4 = new PdfPage();
String s4 =
"4. writeText(String str, PdfFont f, double x, double "
+ "y, boolean wrap) ~ writeText(s4, fontArial, 100, "
+ "100, PdfTextFormatter.NO_WRAP)";
// Writes non-wrapped text with Arial font at position
// (100, 100)
page4.writeText(s4,
fontArial,
100, 100,
PdfTextFormatter.NO_WRAP);
PdfPage page5 = new PdfPage();
String s5 =
"5. writeText(String s, PdfFont f, double x, double "
+ "y, double rotation) ~ writeText(s5, fontArial, "
+ "100.0, 100.0, 5.0)";
// Writes text, rotated by 5 degrees, with Arial font at x
// position (100, 100)
page5.writeText(s5, fontArial, 100.0, 100.0, 5.0);
PdfPage page6 = new PdfPage();
String s6 =
"6. writeText(String str, PdfFont f, int alignment) ~ "
+ "writeText(s6, fontArial, PdfTextFormatter.RIGHT)";
// Writes right-aligned text with Arial font
page6.writeText(s6, fontArial, PdfTextFormatter.RIGHT);
PdfPage page7 = new PdfPage();
String s7 =
"7. writeText(String str, PdfFont f, int alignment, "
+ "boolean wrap) ~ writeText(s7, fontArial, "
+ "PdfTextFormatter.RIGHT, PdfTextFormatter.WRAP)";
// Writes right-aligned wrapped text with Arial font
page7.writeText(s7,
fontArial,
PdfTextFormatter.RIGHT,
PdfTextFormatter.WRAP);
PdfPage page8 = new PdfPage();
String s8 =
"8. writeText(String str, PdfFont f, int alignment, "
+ "double x, double y) ~ writeText(s8, fontArial, "
+ "PdfTextFormatter.RIGHT, 100.0, 100.0)";
// Writes right-aligned text with Arial font at position
// (100, 100)
page8.writeText(s8,
fontArial,
PdfTextFormatter.RIGHT,
100.0, 100.0);
PdfPage page9 = new PdfPage();
String s9 =
"9. writeText(String str, PdfFont f, int alignment, "
+ "double x, double y, boolean wrap) ~ writeText(s9, "
+ "fontArial, PdfTextFormatter.CENTER, 100.0, 100.0, "
+ "PdfTextFormatter.WRAP)";
// Writes right-aligned text with Arial font at position
// (100, 100)
page9.writeText(s9,
fontArial,
PdfTextFormatter.CENTER,
100.0, 100.0,
PdfTextFormatter.WRAP);
document.add(page1);
document.add(page2);
document.add(page3);
document.add(page4);
document.add(page5);
document.add(page6);
document.add(page7);
document.add(page8);
document.add(page9);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment writes text on pages using several
// overloaded method
public void writeText_Example2() throws IOException, PdfException
{
// Creates a PdfWriter instance
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_writeText_example2.pdf");
// Creates a PdfDocument instance with the PdfWriter instance
PdfDocument document = new PdfDocument(writer);
// Creates two pages
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
// Creates some PdfPoint objects
PdfPoint point1 = new PdfPoint(100, 100);
PdfPoint point2 = new PdfPoint(200, 200);
PdfPoint point3 = new PdfPoint(300, 300);
PdfPoint point4 = new PdfPoint(100, 400);
PdfPoint point5 = new PdfPoint(100, 500);
String s1 =
". (100, 100) point1 [writeText(String str, PdfPoint p) "
+ "~ writeText(s1, point1)]";
// Writes text at a specified point
page1.writeText(s1, point1);
String s2 =
". (200, 200) point2 [writeText(String str, PdfPoint p, "
+ "boolean wrap) ~ writeText(s2, point2, "
+ "PdfTextFormatter.WRAP)]";
// Writes wrapped text at a specified point
page1.writeText(s2, point2, PdfTextFormatter.WRAP);
String s3 =
". (300, 300) point3 [writeText(String str, PdfPoint p, "
+ "int alignment) ~ writeText(s3, point3, "
+ "PdfTextFormatter.RIGHT)]";
// Writes right-aligned text a specified point
page1.writeText(s3, point3, PdfTextFormatter.RIGHT);
// String s4 =
// ". (100, 400) point4 [writeText(String s, PdfPoint p, "
// + "double rotation) ~ writeText(s4, point4, 355.0)]";
// String kept deliberately small as the following method
// requires single-line text
String s4 = ". (100, 400) point4 ";
// Writes text tilted by 355 degrees at specified point
page1.writeText(s4, point4, 355.0);
String s5 =
". (100, 500) point5 [writeText(String str, PdfPoint p, "
+ "int alignment, boolean wrap) ~ writeText(s5, point5, "
+ "PdfTextFormatter.LEFT, PdfTextFormatter.NO_WRAP)]";
// Writes text left-aligned and wrapped text at specified
// point
page1.writeText(s5,
point5,
PdfTextFormatter.LEFT,
PdfTextFormatter.NO_WRAP);
// Creates a Helvetica font object
PdfFont fontHelvetica = PdfFont.create("Helvetica",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL, 16,
PdfEncodings.WINANSI);
// Creates a Courier font object
PdfFont fontCourier = PdfFont.create("Courier",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL, 16,
PdfEncodings.WINANSI);
String s6 =
". (100, 100) point1 [writeText(String str, PdfFont f, "
+ "PdfPoint p) ~ writeText(s1, fontHelvetica, point1)]";
// Writes text in Helvetica font at specified point
page2.writeText(s6, fontHelvetica, point1);
String s7 =
". (200, 200) point2 [writeText(String str, PdfFont f, "
+ "PdfPoint p, boolean wrap) ~ writeText(s2, "
+ "fontCourier, point2, PdfTextFormatter.WRAP)]";
// Writes wrapped text in Courier font at specified point
page2.writeText(s7,
fontCourier,
point2,
PdfTextFormatter.WRAP);
String s8 =
". (300, 300) point3 [writeText(String str, PdfFont f, "
+ "int alignment, PdfPoint p) ~ writeText(s8, "
+ "fontHelvetica, PdfTextFormatter.RIGHT, point3)]";
// Writes right-aligned text with Helvetica font at specified
// point
page2.writeText(s8,
fontHelvetica,
PdfTextFormatter.RIGHT,
point3);
String s9 = ". (100, 400) point4 [writeText(String s, "
+ "PdfFont f, PdfPoint p, double rotation) ~ "
+ "writeText(s9, fontCourier, point4, 355.0)]";
// Writes text, tilted by 355 degrees, with Courier font at
// specified point
page2.writeText(s9, fontCourier, point4, 355.0);
String s10 =
". (100, 500) [writeText(String str, PdfFont f, int "
+ "alignment, PdfPoint p, boolean wrap) ~ "
+ "writeText(s10, fontHelvetica, PdfTextFormatter.LEFT, "
+ "point5, PdfTextFormatter.NO_WRAP)]";
// Writes left-aligned non-wrapped text with Helvetica font at
// specified point
page2.writeText(s10,
fontHelvetica,
PdfTextFormatter.LEFT,
point5,
PdfTextFormatter.NO_WRAP);
// Adds the two pages
document.add(page1);
document.add(page2);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment creates pages using several constructors
public void WriteText_Example1() throws IOException, PdfException
{
// Creates a PdfWriter instance
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_writeText_example1.pdf");
// Creates a PdfDocument instance with the PdfWriter instance
PdfDocument document = new PdfDocument(writer);
// Creates two pages
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
// Creates four rectangles
PdfRect rectangle1 = new PdfRect(100, 100, 400, 50);
PdfRect rectangle2 = new PdfRect(100, 175, 500, 75);
PdfRect rectangle3 = new PdfRect(200, 300, 300, 100);
PdfRect rectangle4 = new PdfRect(100, 450, 500, 75);
// Draws the rectangles on the pages
page1.drawRect(rectangle1);
page1.drawRect(rectangle2);
page1.drawRect(rectangle3);
page1.drawRect(rectangle4);
page2.drawRect(rectangle1);
page2.drawRect(rectangle2);
page2.drawRect(rectangle3);
page2.drawRect(rectangle4);
// Creates a Helvetica font
PdfFont fontHelvetica = PdfFont.create(
"Helvetica",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL,
16,
PdfEncodings.WINANSI);
String s1 = "1. writeText(String str, PdfRect rect) ~ "
+ "writeText(s1, rectangle1)";
// Writes text inside the specified rectangle on page 1
page1.writeText(s1, rectangle1);
String s2 = "2. writeText(String str, PdfRect rect, int "
+ "alignment) ~ writeText(s2, rectangle2, "
+ "PdfTextFormatter.CENTER)";
// Writes center-aligned text inside specified rectangle on
// page 1
page1.writeText(s2, rectangle2, PdfTextFormatter.CENTER);
String s3 = "3. writeText(String str, PdfFont f, PdfRect "
+ "rect) ~ writeText(s3, fontHelvetica, "
+ "rectangle3)";
// Writes text with Helvetica font inside specified rectangle
// on page 1
page1.writeText(s3, fontHelvetica, rectangle3);
String s4 = "4. writeText(String str, PdfFont f, PdfRect "
+ "rect, int alignment) ~ writeText(s4, "
+ "rectangle4, PdfTextFormatter.RIGHT)";
// Writes right-aligned text inside specified rectangle on
// page 1
page1.writeText(s4, rectangle4, PdfTextFormatter.RIGHT);
String s5 = "5. writeText(String str, PdfRect rect, double "
+ "rotation, double firstLinePosition) ~ "
+ "writeText(s5, rectangle1, 5.0, 100)";
// Writes text rotated by 5.0 degrees and with its first line
// offset by 100 points inside specified rectangle on page 2
page2.writeText(s5, rectangle1, 5.0, 100);
String s6 =
"6. writeText(String str, PdfRect rect, int alignment, "
+ "double rotation, double firstLinePosition) ~ "
+ "writeText(s6, rectangle2, PdfTextFormatter.LEFT, "
+ "355.0, 50)";
// Writes left-aligned text rotated by 355 degrees and with
// its first line offset by 50 points inside specified
// rectangle on page 2
page2.writeText(s6,
rectangle2,
PdfTextFormatter.LEFT,
355.0,
50);
String s7 =
"7. writeText(String str, PdfFont f, PdfRect rect, "
+ "double rotation, double firstLinePosition) ~ "
+ "writeText(s7, fontHelvetica, rectangle3, 5.0, 125)";
// Writes text rotated by 5 degrees with Helvetica font and
// with its first line offset by 125 points inside specified
// rectangle on page2
page2.writeText(s7, fontHelvetica, rectangle3, 5.0, 125);
String s8 =
"8. writeText(String str, PdfFont f, PdfRect rect, "
+ "int alignment, double rotation, double "
+ "firstLinePosition) ~ writeText(s8, fontHelvetica, "
+ "rectangle4, PdfTextFormatter.LEFT, 355, 25.0)";
// Writes left-aligned text rotated by 355 degrees with
// Helvetica font and with its first line offset by 25 points
// inside specified rectangle on page 2
page2.writeText(s8,
fontHelvetica,
rectangle4,
PdfTextFormatter.LEFT,
355,
25.0);
document.add(page1);
document.add(page2);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment creates pages using several constructors
public void PdfPage_Example() throws IOException, PdfException
{
// Creates a PdfWriter instance
PdfWriter writer = PdfWriter.fileWriter(
"PdfPage_Example.pdf");
// Creates a PdfDocument instance with the PdfWriter instance
PdfDocument document = new PdfDocument(writer);
// Creates a page with the default constructor
PdfPage page1 = new PdfPage();
// Creates a page with a standard page size
PdfPage page2 = new PdfPage(PdfPageSize.A4);
// Creates a page with a standard page size. Also, specifies
// size of its margins in pixels.
PdfPage page3 = new PdfPage(
PdfPageSize.A3, // pageSize
100, // pageLeftMargin
100, // pageTopMargin
100, // pageRightMargin
100, // pageBottomMargin
PdfMeasurement.MU_PIXELS // measurementUnit
);
// Creates a page with a standard page size. Also, specifies
// size of its header, footer, and margins in pixels.
PdfPage page4 = new PdfPage(
PdfPageSize.A3, // pageSize
150, // pageHeaderHeight
150, // pageFooterHeight
100, // pageLeftMargin
100, // pageTopMargin
100, // pageRightMargin
100, // pageBottomMargin
PdfMeasurement.MU_PIXELS // measurementUnit
);
// Creates a page with its width and height specified in
// points
PdfPage page5 = new PdfPage(
600, // width
800 // height
);
// Creates a page with its width and height specified in
// inches
PdfPage page6 = new PdfPage(
600.0/72, // width
800.0/72, // height
PdfMeasurement.MU_INCHES // measurementUnit
);
// Creates a page with its width, height, and margins
// specified in pixels
PdfPage page7 = new PdfPage(
600, // width
800, // height
50, // pageLeftMargin
50, // pageTopMargin
50, // pageRightMargin
50, // pageBottomMargin
PdfMeasurement.MU_PIXELS // measurementUnit
);
// Creates a page with its width, height, header, footer, and
// margins specified in pixels
PdfPage page8 = new PdfPage(
600, // width
800, // height
150, // pageHeaderHeight
150, // pageFooterHeight
50, // pageLeftMargin
50, // pageTopMargin
50, // pageRightMargin
50, // pageBottomMargin
PdfMeasurement.MU_PIXELS // measurementUnit
);
// Sets text alignment to both side margins
page3.getTextFormatter().setAlignment(
PdfTextFormatter.JUSTIFIED);
page4.getTextFormatter().setAlignment(
PdfTextFormatter.JUSTIFIED);
page7.getTextFormatter().setAlignment(
PdfTextFormatter.JUSTIFIED);
page8.getTextFormatter().setAlignment(
PdfTextFormatter.JUSTIFIED);
// Writes text identifying the pages
page1.writeText("This page was created with default "
+ "constructor [PdfPage()]");
page2.writeText(
"This A4-size page was created with constructor "
+ "[PdfPage(int pageSize) ~ "
+ "PdfPage(PdfPageSize.A4)]");
page3.writeText(
"This A3-size page was created with constructor "
+ "[PdfPage(int pageSize, double pageLeftMargin, "
+ "double pageTopMargin, double pageRightMargin, "
+ "double pageBottomMargin, int measurementUnit) "
+ "~ PdfPage(PdfPageSize.A3, 100, 100, 100, 100, "
+ "PdfMeasurement.MU_PIXELS)]");
page4.writeText(
"This B4-size page was created with constructor"
+ "[PdfPage(int pageSize, double pageHeaderHeight, "
+ "double pageFooterHeight, double pageLeftMargin, "
+ "double pageTopMargin, double pageRightMargin, "
+ "double pageBottomMargin, int measurementUnit) ~ "
+ "PdfPage(PdfPageSize.B4, 150, 150, 100, 100, 100, "
+ "100, PdfMeasurement.MU_PIXELS)]");
page5.writeText(
"This page was created with constructor "
+ "[PdfPage(double width, double height) ~ "
+ "PdfPage(600, 800)]");
page6.writeText(
"This page was created with constructor "
+ "[PdfPage(double width, double height, int "
+ "measurementUnit) ~ PdfPage(600.0/72, 800.0/72, "
+ "PdfMeasurement.MU_INCHES)]");
page7.writeText(
"This page was created with constructor "
+ "[PdfPage(double width, double height, double "
+ "pageLeftMargin, double pageTopMargin, double "
+ "pageRightMargin, double pageBottomMargin, int "
+ "measurementUnit) ~ PdfPage(600, 800, 100, 100, "
+ "100, PdfMeasurement.MU_PIXELS)]");
page8.writeText(
"This A4-size page was created with constructor "
+ "[PdfPage(double width, double height, double "
+ "pageHeaderHeight, double pageFooterHeight, double "
+ "pageLeftMargin, double pageTopMargin, double "
+ "pageRightMargin, double pageBottomMargin, int "
+ "measurementUnit) ~ PdfPage(600, 800, 150, 150, "
+ "50, 50, 50, 50, PdfMeasurement.MU_PIXELS)]");
// Creates a font (for use in headers and footers)
PdfFont fontHelvetica = PdfFont.create(
"Helvetica",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL,
24,
PdfEncodings.WINANSI);
// Writes headers
page4.addHeaderText(
"This text goes to the header. This text goes to "
+ "the header. This text goes to the header. This "
+ "text goes to the header. This text goes to the "
+ "header.",
fontHelvetica,
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true);
page8.addHeaderText(
"This text goes to the header. This text goes to "
+ "the header. This text goes to the header. This "
+ "text goes to the header. This text goes to the "
+ "header.",
fontHelvetica,
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true);
// Writes footers
page4.addFooterText(
"This text goes to the footer. This text goes to "
+ "the footer. This text goes to the footer. This "
+ "text goes to the footer. This text goes to the "
+ "footer.",
fontHelvetica,
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true);
page8.addFooterText(
"This text goes to the footer. This text goes to "
+ "the footer. This text goes to the footer. This "
+ "text goes to the footer. This text goes to the "
+ "footer.",
fontHelvetica,
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true);
// Adds the pages to the document
document.add(page1);
document.add(page2);
document.add(page3);
document.add(page4);
document.add(page5);
document.add(page6);
document.add(page7);
document.add(page8);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
}