import java.awt.Color;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfAction;
import com.gnostice.pdfone.PdfBookmark;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfEncryption;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfImage;
import com.gnostice.pdfone.PdfMeasurement;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfPageLayout;
import com.gnostice.pdfone.PdfPageMode;
import com.gnostice.pdfone.PdfPageSize;
import com.gnostice.pdfone.PdfPoint;
import com.gnostice.pdfone.PdfReader;
import com.gnostice.pdfone.PdfRect;
import com.gnostice.pdfone.PdfTextFormatter;
import com.gnostice.pdfone.PdfWriter;
import com.gnostice.pdfone.encodings.PdfEncodings;
import com.gnostice.pdfone.filters.PdfFilter;
import com.gnostice.pdfone.filters.PdfFlateFilter;
import com.gnostice.pdfone.fonts.PdfFont;
import com.gnostice.pdfone.graphics.PdfPen;
public class PdfDocument_Examples
{
// Activates the component PDFOne.jar
static
{
PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
"9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
}
public static void main(String[] args) throws IOException,
PdfException
{
PdfDocument_Examples obj = new PdfDocument_Examples();
// To try other examples, add the obj.<example_method>
// accordingly. For example, try:
// obj.PdfDocument_PdfWriter_Example();
obj.readerAndWriter_Example();
}
// This code segment demonstrates the use of an overloaded
// addBookmark() method.
//
// Call this example method with the following arguments:
// 1. pathname of an application
// 2. pathname of a document that the application can open
//
// For example:
// obj.addBookmark_String_PDFBookmark_String_boolean_Example(
// "C:\\windows\\notepad.exe",
// "C:\\A_Text_File.txt");
//
public void addBookmark_String_PDFBookmark_String_boolean_Example(
String applicationFilePathname, String documentFilePathname)
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"addBookmark_String_PDFBookmark_String_boolean_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
// Create 3 new pages
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Write text on the pages
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Add the pages to the document
document.add(page1);
document.add(page2);
document.add(page3);
// Add bookmark that launches an application
PdfBookmark bmRoot = document.getBookmarkRoot();
document.addBookmark(
"Launch " + applicationFilePathname,
bmRoot,
documentFilePathname,
false);
// Adds a bookmark that prints a file
document.addBookmark(
"Print " + documentFilePathname,
bmRoot,
documentFilePathname,
true);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code snippet adds a bookmark that links to a rectangle
// specified by x-y coordinates of its top left corner, its width
// and its height.
public void addBookmark_String_PdfBookmark_int_double_double_double_double_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"addBookmark_String_PdfBookmark_"
+ "int_double_double_double_double_Example().pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage(PdfPageSize.A4);
PdfPage page2 = new PdfPage(PdfPageSize.A4);
PdfPage page3 = new PdfPage(PdfPageSize.A4);
// Writes identifying text to the PdfPage objects
page1.writeText("This is page 1. ");
page2.writeText("This is page 2. ");
page3.writeText("This is page 3. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates a new PdfBookmark object and assigns it to the
// root of the document outline
PdfBookmark bm = document.getBookmarkRoot();
// Adds a bookmark that leads to a rectangular area on page 2
bm = document.addBookmark(
// bookmark text
"Go to rectangular area on page 2",
bm, // parent bookmark
2, // pageNo
200, // x-coordinate of top-left corner
200, // y-coordinate of top-left corner
300, // width
300); // height
// Draw a rectangle illustrating the position of the
// rectangular area
document.drawRect(200, 200, 300, 300, "2");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code snippet illustrates how to add a go-to action
// to a document.
public void addAction_int_int_Example() throws IOException,
PdfException {
PdfWriter writer =
PdfWriter.fileWriter(
"pdfdocument_addAction_int_int_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Adds a go-to action to the document.
document.addAction(PdfAction.GOTO, 2);
// When the document is opened in the viewer,
// it immediately jumps to page 2.
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code snippet illustrates how to add a
// Javascript action to a document-level event.
public void addAction_int_int_String_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"pdfdocument_addAction_int_int_"
+ "String_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Adds a Javascript action to a document-level event
document.addAction(
PdfAction.PdfEvent.ON_BEFORE_DOCUMENT_PRINT,
PdfAction.JAVASCRIPT,
"app.alert(\"You are about to print!\")");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// 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);
document.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Sets default brush color to green
document.setBrushColor(Color.GREEN);
// Sets default pen color to red
document.setPenColor(Color.RED);
// Sets default pen width to 10 times default
document.setPenWidth(PdfPen.DEFAULT_WIDTH * 10);
// Sets default shape of paths that are joined
document.setPenJoinStyle(PdfPen.JOINSTYLE_ROUND);
// Sets default miter limit to twice the default
document.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
document.drawPolygon(x, y, 5, true, true);
// Sets default shape of endpoints of paths that are stroked
document.setPenCapStyle(PdfPen.CAPSTYLE_PROJECTING_SQUARE);
// Draws a straight line
document.drawLine(2, 5, 5, 6);
// Sets gap length of the dash pattern
document.setPenDashGap(2);
// Sets dash length of the dash pattern
document.setPenDashLength(6);
// Sets phase of the dash pattern
document.setPenDashPhase(8);
// Draws a dashed line
document.drawLine(2, 5, 3, 6);
// Identifies corners of the polygon
document.writeText(". (2, 1)", 2, 1);
document.writeText(". (3, 0.5)", 3, 0.5);
document.writeText(". (3.5, 1)", 3.5, 1);
document.writeText(". (4, 3)", 4, 3);
document.writeText(". (3.7, 1.5)", 3.7, 1.5);
document.writeText(". (3, 6)", 3, 6);
// Creates and identifies bookmarks used to view specific
// destinations in the document
document.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);
document.writeText(". (2, 5)", 2, 5);
document.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 retrieves the XML metadata of a PDF file and
// prints it on the system console
public void getXMLMetadata_Example() throws IOException,
PdfException
{
// Creates a PdfReader instance
// Assumes that there is a file named
// "pdf_with_XML_Metadata.pdf"
// is present in the current directory.
PdfReader reader = PdfReader.fileReader(
"pdf_with_XML_Metadata.pdf");
// Creates a PdfDocument instance with the PdfReader instance
PdfDocument document = new PdfDocument(reader);
// Obtains the XML metadata of the file and prints it on the
// system console
System.out.println(document.getXMLMetadata());
// Closes all I/O streams associated with this reader object
reader.dispose();
}
// This code segment writes text on pages using several
// overloaded method
public void Encryptor_Example() throws IOException, PdfException
{
// Creates a PdfWriter instance
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_Encryptor_example.pdf");
// Creates a PdfDocument instance with the PdfWriter instance
PdfDocument document = new PdfDocument(writer);
// Writes some text on the document
document.writeText("This is an encrypted document.");
// Obtains the PdfEncryption object of the document
PdfEncryption crypt = document.getEncryptor();
// Sets a owner password
crypt.setOwnerPwd("key");
// Sets a user password
crypt.setUserPwd("secret");
// Sets an encryption level
crypt.setLevel(PdfEncryption.LEVEL_128_BIT);
// Updates the encryption settings of the document
document.setEncryptor(crypt);
// 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 two PdfWriter objects and two
// PdfReader objects. A single PdfDocument object is associated
// with these four objects using getReader, getWriter, setReader,
// and setWriter methods.
public void readerAndWriter_Example() throws IOException,
PdfException
{
// Creates two PdfWriter objects
PdfWriter writer1 = PdfWriter.fileWriter(
"PdfDocument_readerAndWriter_example_file1.pdf");
PdfWriter writer2 = PdfWriter.fileWriter(
"PdfDocument_readerAndWriter_example_file2.pdf");
// Creates a PdfDocument object with the first PdfWriter
// object
PdfDocument document = new PdfDocument(writer1);
// Creates 5 PdfPage objects and adds them to the document
for (int i = 1; i <= 5; i++)
{
PdfPage page = new PdfPage();
document.add(page, true);
document.writeText(
"This is page #"
+ i
+ " of "
+ "PdfDocument_readerAndWriter_example_file1.pdf");
}
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O stream associated with the first writer
// object
writer1.dispose();
// Sets the PdfDocument object to use the second PdfWriter
// object
document.setWriter(writer2);
// Writes text identifying the file used by the new
// PdfReader object
document.writeText(
"This text is however on "
+ "PdfDocument_readerAndWriterWriter_example_file2.pdf",
100, 100, "1-5");
document.setOpenAfterSave(true);
document.write();
// Closes all I/O stream associated with the second writer
// object.
// Same as calling writer2.dispose().
document.getWriter().dispose();
// Sets the reader objects to read from the above two
// documents
PdfReader reader1 = PdfReader.fileReader(
"PdfDocument_readerAndWriter_example_file1.pdf",
"PdfDocument_readerAndWriter_example_file3.pdf");
PdfReader reader2 = PdfReader.fileReader(
"PdfDocument_readerAndWriter_example_file2.pdf",
"PdfDocument_readerAndWriter_example_file4.pdf");
document = new PdfDocument(reader1);
// Writes text identifying the file used by the new
// PdfReader object
document.writeText(
"This text is however on "
+ "PdfDocument_readerAndWriter_example_file3.pdf",
100, 100, "1-5");
document.setOpenAfterSave(true);
document.write();
// Closes all I/O stream associated with the first reader
// object
reader1.dispose();
// Sets the PdfDocument object to use the second PdfReader
// object
document.setReader(reader2);
document.writeText(
"This text is however on "
+ "PdfDocument_readerAndWriter_example_file4.pdf",
100, 100, "1-5");
document.setOpenAfterSave(true);
document.write();
// Closes all I/O streams associated with the second reader
// object.
// Same as calling reader2.dispose().
document.getReader().dispose();
}
// This code segment merges a document identified by its pathname
public void merge_String_Example() throws IOException,
PdfException
{
// Creates a document that is to be merged to be with another
// one
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_merge_String_example_file1.pdf");
PdfDocument document1 = new PdfDocument(writer);
// Creates 20 PdfPage objects and adds them to the document
for (int i = 1; i <= 20; i++)
{
PdfPage page = new PdfPage();
document1.add(page, true);
document1.writeText(
"This is page #"
+ i
+ " of PdfDocument_merge_String_example_file1.pdf");
}
// Sets the file to be opened after it is written to
document1.setOpenAfterSave(true);
// Writes the document object to file
document1.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
// Creates a document to which the previous document is to be
// merged
writer = PdfWriter.fileWriter(
"PdfDocument_merge_String_example_file2.pdf");
PdfDocument document2 = new PdfDocument(writer);
document2.writeText(
"This is page #1 of PdfDocument_merge_String_"
+ "example_file2.pdf");
// Merges the previous document referred here by its pathname
document2.merge(
"PdfDocument_merge_String_example_file1.pdf");
document2.setOpenAfterSave(true);
document2.write();
writer.dispose();
}
// This code segment merges two documents with a List object
public void merge_List_Example() throws IOException,
PdfException
{
// Creates the first document to be merged
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_merge_List_example_file1.pdf");
PdfDocument document1 = new PdfDocument(writer);
// Creates 20 PdfPage objects and adds them to the document
for (int i = 1; i <= 20; i++)
{
PdfPage page = new PdfPage();
document1.add(page, true);
document1.writeText(
"This is page #"
+ i
+ " of PdfDocument_merge_List_example_file1.pdf");
}
// Sets the file to be opened after it is written to
document1.setOpenAfterSave(true);
// Writes the document object to file
document1.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
// Creates the second document to be merged
writer = PdfWriter.fileWriter(
"PdfDocument_merge_example_file2.pdf");
PdfDocument document2 = new PdfDocument(writer);
for (int i = 1; i <= 20; i++)
{
PdfPage page = new PdfPage();
document2.add(page, true);
document2.writeText(
"This is page #"
+ i
+ " of PdfDocument_merge_List_example_file2.pdf");
}
document2.setOpenAfterSave(true);
document2.write();
writer.dispose();
// Creates a document which will have the merged content of
// the above two documents
writer = PdfWriter.fileWriter(
"PdfDocument_merge_example_file3.pdf");
PdfDocument document3 = new PdfDocument(writer);
document3.writeText(
"This is page #1 of PdfDocument_merge_List_"
+ "example_file3.pdf");
// Creates a list object
List list = new ArrayList();
// Adds PdfDocument object of first file to the list
list.add(document1);
// Adds string pathname of second file to the list
list.add("PdfDocument_merge_List_example_file2.pdf");
// Merges the two documents in the list
document3.merge(list);
document3.setOpenAfterSave(true);
document3.write();
writer.dispose();
}
// This code segment creates a document. It writes text
// identifying the default measurement unit - points. It then
// changes the measurement unit to inches and writes some new text
// to demonstrate the effect.
public void MeasurementUnit_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_MeasurementUnit_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Writes text identifying position (100, 100), expressed in
// points
document.writeText(". (100, 100) points", 100, 100);
// Checks and sets measurement unit to inches
if (document.getMeasurementUnit()
!= PdfMeasurement.MU_INCHES)
{
document.setMeasurementUnit(PdfMeasurement.MU_INCHES);
}
// Writes text identifying position (2, 1), expressed in
// inches
document.writeText(". (2, 3) inches", 2, 3);
// 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 a document with 20 pages.
// It then writes some text on the fifth page.
public void getPage_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_getPage_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates 20 PdfPage objects and adds them to the document
for (int i = 1; i <= 20; i++)
{
PdfPage page = new PdfPage();
document.add(page);
}
// Writes text on the first page, which is still the
// document's current page
document.writeText(
"This text is written on the first page. (See page 5)");
// Writes text on page 5 of the document
document.getPage(5).writeText(
"This text is written on page 5");
// 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 using several overloaded methods
public void writeText_Example9() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_example8.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates a reference to a point
PdfPoint point = new PdfPoint(100, 100);
// Creates a Helvetica font
PdfFont fontHelvetica = PdfFont.create(
"Helvetica",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL,
8,
PdfEncodings.WINANSI);
String s1 = "writeText(String str, PdfFont f, PdfPoint pt)";
// Creates page 1 and writes text with Helvetica font
// at specified point
document.writeText(s1, fontHelvetica, point);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Writes text with Helvetica font at specified point
// on pages 3 and 4
document.writeText(s1, fontHelvetica, point, "3,4");
// 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 using several overloaded methods
public void writeText_Example8() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_"
+ "example8.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates points
PdfPoint point1 = new PdfPoint(100, 100);
PdfPoint point2 = new PdfPoint(300, 300);
// Creates a Helvetica font
PdfFont fontHelvetica = PdfFont.create("Helvetica",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL, 8,
PdfEncodings.WINANSI);
String s1 =
"1. [writeText(java.lang.String str, PdfFont f, PdfPoint "
+ "pt, boolean wrap) ~ writeText(s1, fontHelvetica, "
+ "point1, PdfTextFormatter.WRAP)]";
// Creates page 1 and writes wrapped text with Helvetica font
// at specified point
document.writeText(s1,
fontHelvetica,
point1,
PdfTextFormatter.WRAP);
String s2 =
"2. [writeText(java.lang.String str, PdfFont f, PdfPoint "
+ "pt, double rotation) - writeText(s2, fontHelvetica, "
+ "point1, 5.0)]";
// Writes text rotated by 5.0 degrees with Helvetica font at
// specified point on page 1
document.writeText(s2, fontHelvetica, point2, 5.0);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
String s3 =
"3. [writeText(java.lang.String str, PdfFont f, PdfPoint "
+ "pt, boolean wrap) ~ writeText(s1, fontHelvetica, "
+ "point1, PdfTextFormatter.WRAP)]";
// Writes wrapped text with Helvetica font at specified point
// on pages 3 and 4
document.writeText(s3,
fontHelvetica,
point1,
PdfTextFormatter.WRAP,
"3, 4");
String s4 =
"4. [writeText(java.lang.String str, PdfFont f, PdfPoint "
+ "pt, double rotation) - writeText(s2, fontHelvetica, "
+ "point1, 5.0)]";
// Writes text rotated by 5.0 degrees with Helvetica font on
// pages 3 and 4
document.writeText(s4, fontHelvetica, point2, 5.0, "3, 4");
// 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 using several overloaded methods
public void writeText_Example7() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_"
+ "example7.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates 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);
String s1 =
"1. [writeText(String str, PdfPoint pt) ~ "
+ "writeText(s1, point1)]";
// Creates page 1 and writes text at specified point
document.writeText(s1, point1);
String s2 =
"2. [writeText(String str, PdfPoint pt, boolean wrap) ~ "
+ "writeText(s2, point2, PdfTextFormatter.WRAP)]";
// Writes wrapped text at specified point on page 1
document.writeText(s2, point2, PdfTextFormatter.WRAP);
String s3 =
"3. [writeText(String str, PdfPoint pt, double rotation) "
+ "~ writeText(s3, point3, 5.0)]";
// Writes text, tilted by 5.0, at specified point on page 1
document.writeText(s3, point3, 5.0);
String s4 =
"4. [writeText(String str, PdfPoint pt, int alignment, "
+ "boolean wrap) ~ writeText(s4, point4, "
+ "PdfTextFormatter.RIGHT, PdfTextFormatter.WRAP)]";
// Writes wrapped and right-aligned text at specified point on
// page 1
document.writeText(s4,
point4,
PdfTextFormatter.RIGHT,
PdfTextFormatter.WRAP);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
String s5 =
"5. [writeText(String str, PdfPoint pt, String '"
+ "pageRange) ~ writeText(s5, point1, \"3-4\")]";
// Writes text at specified point on pages 3 and 4
document.writeText(s5, point1, "3, 4");
String s6 =
"6. [writeText(String str, PdfPoint pt, boolean wrap, "
+ "String pageRange) ~ writeText(s6, point2, "
+ "PdfTextFormatter.WRAP, \"3-4\")]";
// Writes wrapped text at specified point on pages 3 and 4
document.writeText(s6,
point2,
PdfTextFormatter.WRAP,
"3, 4");
String s7 =
"7. [writeText(String str, PdfPoint pt, double "
+ "rotation, String pageRange) ~ writeText(s7, point3, "
+ "5.0, \"3-4\")]";
// Writes text, tilted by 5.0, at specified point on pages 3
// and 4
document.writeText(s7, point3, 5.0, "3, 4");
String s8 =
"8. [writeText(String str, PdfPoint pt, int alignment, "
+ "boolean wrap, String pageRange) ~ writeText(s8, "
+ "point4, PdfTextFormatter.RIGHT, "
+ "PdfTextFormatter.WRAP, \"3-4\")]";
// Writes wrapped and right-aligned text at specified point on
// pages 3 and 4
document.writeText(s8,
point4,
PdfTextFormatter.RIGHT,
PdfTextFormatter.WRAP, "3, 4");
// 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 using several overloaded methods
public void writeText_Example6() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_"
+ "example6.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates a Courier font
PdfFont fontCourier = PdfFont.create("Courier",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL, 8,
PdfEncodings.WINANSI);
String s1 =
"1. [writeText(String str, PdfFont f) ~ "
+ "writeText(s1, fontCourier)]";
// Creates a default page (1) and writes text with Courier
// font
document.writeText(s1, fontCourier);
String s2 =
"2. [writeText(String str, PdfFont f, boolean wrap) ~ "
+ "writeText(s2, fontCourier, PdfTextFormatter.WRAP)]";
// Creates page 2
PdfPage page = new PdfPage();
// Adds page 2 to document and makes it the document's current
// page
document.add(page, true);
// Writes wrapped text with Courier font on page 2
document.writeText(s2, fontCourier, PdfTextFormatter.WRAP);
String s3 =
"3. [writeText(String str, PdfFont f, int alignment) ~ "
+ "writeText(s3, fontCourier, PdfTextFormatter.RIGHT)]";
page = new PdfPage();
document.add(page, true);
// Writes right-aligned text with Courier font on page 3
document.writeText(s3, fontCourier, PdfTextFormatter.RIGHT);
PdfPoint point = new PdfPoint(250, 200);
String s4 =
"4. [writeText(String str, PdfFont f, int alignment, "
+ "PdfPoint pt) ~ writeText(s4, fontCourier, "
+ "PdfTextFormatter.LEFT, point)]";
// Writes left-aligned text with Courier font at a specified
// point on page 3
document.writeText(s4,
fontCourier,
PdfTextFormatter.LEFT,
point);
String s5 =
"5. [writeText(String str, PdfFont f, int alignment, "
+ "boolean wrap) ~ writeText(s5, fontCourier, "
+ "PdfTextFormatter.RIGHT, PdfTextFormatter.WRAP)]";
page = new PdfPage();
document.add(page, true);
// Writes wrapped and right-aligned text with Courier font on
// page 4
document.writeText(s5,
fontCourier,
PdfTextFormatter.RIGHT,
PdfTextFormatter.WRAP);
String s6 =
"6. [writeText(String str, PdfFont f, int alignment, "
+ "PdfPoint pt, boolean wrap) ~ writeText(s6, "
+ "fontCourier, PdfTextFormatter.JUSTIFIED, point, "
+ "PdfTextFormatter.WRAP)]";
// Writes wrapped and justified text with Courier font at
// specified point on page 5
document.writeText(s6,
fontCourier,
PdfTextFormatter.JUSTIFIED,
point,
PdfTextFormatter.WRAP);
// Creates and adds four pages
for (int i = 1; i <= 4; i++)
{
page = new PdfPage();
document.add(page, true);
}
String s7 =
"7. [writeText(String str, PdfFont f, String pageRange) "
+ "~ writeText(s7, fontCourier, \"6, 8\")]";
// Writes text with Courier font on pages 6 and 8
document.writeText(s7,
fontCourier,
"6, 8");
for (int i = 1; i <= 4; i++)
{
page = new PdfPage();
document.add(page, true);
}
String s8 =
"8. [writeText(String str, PdfFont f, boolean wrap, String "
+ "pageRange) ~ writeText(s8, fontCourier, "
+ "PdfTextFormatter.WRAP, \"10, 12\")]";
// Writes wrapped text with Courier font on pages 6 and 8
document.writeText(s8,
fontCourier,
PdfTextFormatter.WRAP,
"10, 12");
for (int i = 1; i <= 4; i++)
{
page = new PdfPage();
document.add(page, true);
}
String s9 =
"9. [writeText(String str, PdfFont f, int alignment, "
+ "String pageRange) ~ writeText(s9, fontCourier, "
+ "PdfTextFormatter.CENTER, \"14, 16\")]";
// Writes center-aligned text with Courier font on pages 14
// and 16
document.writeText(s9,
fontCourier,
PdfTextFormatter.CENTER,
"14, 16");
for (int i = 1; i <= 4; i++)
{
page = new PdfPage();
document.add(page, true);
}
String s10 =
"10. [writeText(String str, PdfFont f, int alignment, "
+ "boolean wrap, String pageRange) ~ writeText(s10, "
+ "fontCourier, PdfTextFormatter.LEFT, "
+ "PdfTextFormatter.WRAP, \"18-20\")]";
// Writes left-aligned and wrapped text with Courier font on
// pages from 18 to 20
document.writeText(s10,
fontCourier,
PdfTextFormatter.LEFT,
PdfTextFormatter.WRAP,
"18-20");
String s11 =
"11. [writeText(String str, PdfFont f, int alignment, "
+ "PdfPoint pt, String pageRange) ~ writeText(s11, "
+ "fontCourier, PdfTextFormatter.RIGHT, point, "
+ "\"12, 14, 16, 18\")]";
// Writes right-aligned text with Courier font at specified
// point on pages 12, 14, 16 and 18
document.writeText(s11,
fontCourier,
PdfTextFormatter.RIGHT,
point,
"12, 14, 16, 18");
String s12 =
"12. [writeText(String str, PdfFont f, int alignment, "
+ "PdfPoint pt, boolean wrap, String pageRange) ~ "
+ "writeText(s12, fontCourier, PdfTextFormatter.LEFT, "
+ "point, PdfTextFormatter.WRAP, \"11, 13, 15, 17\")]";
// Writes left-aligned and wrapped text with Courier font at
// specified point on pages 11, 13, 15, and 17
document.writeText(s12,
fontCourier,
PdfTextFormatter.LEFT,
point,
PdfTextFormatter.WRAP,
"11, 13, 15, 17");
// 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 using several overloaded methods
public void writeText_Example5() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_"
+ "example5.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates and draws rectangles
PdfRect rectangle1 = new PdfRect(100, 100, 400, 100);
document.drawRect(rectangle1);
PdfRect rectangle2 = new PdfRect(200, 300, 400, 100);
document.drawRect(rectangle2);
PdfRect rectangle3 = new PdfRect(100, 500, 500, 75);
document.drawRect(rectangle3);
PdfRect rectangle4 = new PdfRect(200, 600, 250, 300);
document.drawRect(rectangle4);
String s1 =
"1. [writeText(String str, PdfRect rectangle) ~ "
+ "writeText(s1, rectangle1)]";
// Creates a default page (1) and writes text inside a
// rectangle
document.writeText(s1, rectangle1);
String s2 =
"2. [writeText(String str, PdfRect rectangle, int "
+ "alignment) ~ writeText(s2, rectangle2, "
+ "PdfTextFormatter.RIGHT)]";
// Writes right-aligned text inside rectangle on page 1
document.writeText(s2,
rectangle2,
PdfTextFormatter.RIGHT);
String s3 =
"3. [writeText(String str, PdfRect rectangle, int "
+ "alignment, double rotation, double "
+ "firstLinePosition) ~ writeText(s3, rectangle3, "
+ "PdfTextFormatter.LEFT, 5.0, 250)]";
// Writes left-aligned text rotated by 5.0 degrees inside a
// rectangle on page 1. The first line begins at 250
// pixels.
document.writeText(s3,
rectangle3,
PdfTextFormatter.LEFT,
5.0,
250);
String s4 =
"4. [writeText(String str, PdfRect "
+ "rectangle, double rotation, double "
+ "firstLinePosition) ~ writetext(s4, "
+ "rectangle4, 5.0, 150)]";
// Writes text rotated by 5.0 degrees inside a rectangle on
// page 1. The first line begins at 150 pixels.
document.writeText(s4, rectangle4, 5.0, 150);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
String s5 =
"5. [writeText(String str, PdfRect rectangle, String "
+ "pageRange) ~ writeText(s5, rectangle1, \"3-4\")]";
document.drawRect(rectangle1, "3-4");
// Writes text inside a rectangle on pages in page
// range "3-4"
document.writeText(s5, rectangle1, "3-4");
String s6 =
"6. [writeText(String str, PdfRect rectangle, int "
+ "alignment, String pageRange) ~ writeText(s6, "
+ "rectangle2, PdfTextFormatter.RIGHT, \"3-4\")]";
document.drawRect(rectangle2, "3-4");
// Writes right-aligned text inside rectangle on pages in
// page range "3-4"
document.writeText(s6,
rectangle2,
PdfTextFormatter.RIGHT,
"3-4");
String s7 =
"7. [writeText(String str, PdfRect rectangle, int "
+ "alignment, double rotation, double "
+ "firstLinePosition, String pageRange) ~ "
+ "writeText(s7, rectangle3, PdfTextFormatter.LEFT, "
+ "5.0, 250, \"3-4\")]";
document.drawRect(rectangle3, "3-4");
// Writes left-aligned text rotated by 5.0 degrees inside a
// rectangle on pages in page range "3-4". The first line
// begins at 250 pixels.
document.writeText(s7,
rectangle3,
PdfTextFormatter.LEFT,
5.0,
250,
"3-4");
String s8 =
"8. [writeText(String str, PdfRect rectangle, double "
+ "rotation, double firstLinePosition, String pageRange)"
+ "~ writetext(s8, rectangle4, 5.0, 150, \"3-4\")]";
document.drawRect(rectangle4, "3-4");
// Writes text rotated by 5.0 degrees inside a rectangle on
// pages in page range "3-4". The first line begins at 150
// pixels.
document.writeText(s8,
rectangle4,
5.0,
150,
"3-4");
// 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 using several overloaded methods
public void writeText_Example4() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_"
+ "example4.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates a PdfFont object
PdfFont fontArial = PdfFont.create(
"Arial",
PdfFont.BOLD | PdfFont.UNDERLINE | PdfFont.ITALIC
| PdfFont.STROKE_AND_FILL,
8,
PdfEncodings.WINANSI);
PdfFont fontCourier = PdfFont.create(
"Courier",
PdfFont.ITALIC | PdfFont.STROKE_AND_FILL,
8,
PdfEncodings.WINANSI);
// Creates and draws rectangles
PdfRect rectangle1 = new PdfRect(100, 100, 400, 100);
document.drawRect(rectangle1);
PdfRect rectangle2 = new PdfRect(200, 300, 400, 100);
document.drawRect(rectangle2);
PdfRect rectangle3 = new PdfRect(100, 500, 500, 75);
document.drawRect(rectangle3);
PdfRect rectangle4 = new PdfRect(200, 600, 250, 300);
document.drawRect(rectangle4);
String s1 =
"1. [writeText(String str, PdfFont f, "
+ "PdfRect rectangle) ~ writeText(s1, fontArial, "
+ "rectangle1)]";
// Creates a default page (1) and writes text inside a
// rectangle using Arial font
document.writeText(s1, fontArial, rectangle1);
String s2 =
"2. [writeText(String str, PdfFont f, PdfRect "
+ "rectangle, int alignment) ~ writeText(s2, fontArial, "
+ "rectangle2, PdfTextFormatter.RIGHT)]";
// Writes right-aligned text inside rectangle on page 1 using
// Arial font
document.writeText(s2,
fontArial,
rectangle2,
PdfTextFormatter.RIGHT);
String s3 =
"3. [writeText(String str, PdfFont f, PdfRect "
+ "rectangle, int alignment, double rotation, double "
+ "firstLinePosition) ~ writeText(s3, fontArial, "
+ "rectangle3, PdfTextFormatter.LEFT, 5.0, 250)]";
// Writes left-aligned text rotated by 5.0 degrees inside a
// rectangle on page 1 with Arial font. The first line begins
// at 250 pixels.
document.writeText(s3,
fontArial,
rectangle3,
PdfTextFormatter.LEFT,
5.0,
250);
String s4 =
"4. [writeText(String str, PdfFont f, PdfRect "
+ "rectangle, double rotation, double "
+ "firstLinePosition) ~ writetext(s4, fontArial, "
+ "rectangle4, 5.0, 150)]";
// Writes text rotated by 5.0 degrees using Arial font inside
// a rectangle. The first line begins at 150 pixels.
document.writeText(s4, fontArial, rectangle4, 5.0, 150);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
String s5 =
"5. [writeText(String str, PdfFont f, "
+ "PdfRect rectangle, String pageRange) ~ writeText(s5, "
+ "fontCourier, rectangle1, \"3-4\")]";
document.drawRect(rectangle1, "3-4");
// Writes text using Courier font inside a rectangle on pages
// in page range "3-4"
document.writeText(s5, fontCourier, rectangle1, "3-4");
String s6 =
"6. [writeText(String str, PdfFont f, PdfRect "
+ "rectangle, int alignment, String pageRange) ~ "
+ "writeText(s6, fontCourier, rectangle2, "
+ "PdfTextFormatter.RIGHT, \"3-4\")]";
document.drawRect(rectangle2, "3-4");
// Writes right-aligned text inside rectangle on page 1 using
// Arial font
document.writeText(s6,
fontCourier,
rectangle2,
PdfTextFormatter.RIGHT,
"3-4");
String s7 =
"7. [writeText(String str, PdfFont f, PdfRect "
+ "rectangle, int alignment, double rotation, double "
+ "firstLinePosition, String pageRange) ~ writeText(s7, "
+ "fontCourier, rectangle3, PdfTextFormatter.LEFT, 5.0, "
+ "250, \"3-4\")]";
document.drawRect(rectangle3, "3-4");
// Writes left-aligned text rotated by 5.0 degrees inside a
// rectangle on pages in page range "3-4" with Courier font.
// The first line begins at 250 pixels.
document.writeText(s7,
fontCourier,
rectangle3,
PdfTextFormatter.LEFT,
5.0,
250,
"3-4");
String s8 =
"8. [writeText(String str, PdfFont f, PdfRect "
+ "rectangle, double rotation, double "
+ "firstLinePosition, String pageRange) ~ writetext(s8, "
+ "fontCourier, rectangle4, 5.0, 150, \"3-4\")]";
document.drawRect(rectangle4, "3-4");
// Writes text rotated by 5.0 degrees inside a rectangle on
// pages in page range "3-4" with Courier font. The first
// line begins at 150 pixels.
document.writeText(s8,
fontCourier,
rectangle4,
5.0,
150,
"3-4");
// 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 write text using several overloaded methods
public void writeText_Example3() throws IOException,
PdfException {
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_example3_.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a PdfFont object
PdfFont fontArial = PdfFont.create("Arial", PdfFont.BOLD
| PdfFont.UNDERLINE | PdfFont.ITALIC
| PdfFont.STROKE_AND_FILL, 8, PdfEncodings.WINANSI);
// Creates string identifying the text that is to be written
String s1 =
"1. [writeText(String str, PdfFont f, double x, "
+ "double y)"
+ " ~ writeText(s1, fontArial, 100, 100)] "
+ "THIS TEXT USES ARIAL FONT AND IS WRITTEN AT POSITION "
+ "(100, 100)";
document.writeText(s1, fontArial, 100, 100);
String s2 =
"2. [writeText(String str, PdfFont f, double x, "
+ "double y, boolean wrap)"
+ " ~ writeText(s2, fontArial, 100, 200, "
+ "PdfTextFormatter.WRAP)] "
+ "THIS TEXT USES ARIAL FONT. IT GETS WRITTEN AT POSITION "
+ "(100, 200). IT IS WRAPPED AT THE MARGINS.";
document.writeText(s2,
fontArial,
100, 200,
PdfTextFormatter.WRAP);
String s3 =
"3. [writeText(String str, PdfFont f, double x, "
+ "double y, double rotation)"
+ " ~ writeText(s3, fontArial, 100, 300, 5.0)] "
+ "THIS TEXT USES ARIAL FONT. IT GETS WRITTEN AT "
+ "POSITION (100, 300). IT IS ROTATED BY 5.0 DEGREES.";
document.writeText(s3, fontArial, 100.0, 300, 5);
String s4 =
"4. [writeText(String str, PdfFont f, int alignment "
+ "double x, double y)"
+ " ~ writeText(s3, fontArial, PdfTextFormatter.RIGHT, "
+ "100, 400)] "
+ "THIS TEXT USES ARIAL FONT. IT GETS WRITTEN AT POSITION "
+ "(100, 400). IT IS ALIGNED TO THE RIGHT MARGIN";
document.writeText(s4,
fontArial,
PdfTextFormatter.RIGHT,
100, 400);
String s5 =
"5. [writeText(String str, PdfFont f, int alignment, "
+ "double x, double y, boolean wrap)"
+ " ~ writeText(s5, fontArial, PdfTextFormatter.RIGHT, "
+ "100, 500, PdfTextFormatter.WRAP)] "
+ "THIS TEXT USES ARIAL FONT. IT GETS WRITTEN AT POSITION "
+ "(100, 500). IT IS ALIGNED TO THE RIGHT MARGIN. IT GETS "
+ "WRAPPED AT THE MARGINS.";
document.writeText(s5,
fontArial,
PdfTextFormatter.RIGHT,
100, 500,
PdfTextFormatter.WRAP);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
String s6 =
"6. [writeText(String str, PdfFont f, double x, "
+ "double y, String pageRange))"
+ " ~ writeText(s6, fontArial, 100, 100, \"3-4\")] "
+ "THIS TEXT USES ARIAL FONT AND IS WRITTEN AT POSITION "
+ "(100, 100) ON PAGE RANGE \"3-4\"";
document.writeText(s6, fontArial, 100, 100, "3-4");
String s7 =
"7. [writeText(String str, PdfFont f, double x, "
+ "double y, boolean wrap, String pageRange))"
+ " ~ writeText(s7, fontArial, 100, 200, "
+ "PdfTextFormatter.WRAP, \"3-4\")] "
+ "THIS TEXT USES ARIAL FONT. IT GETS WRITTEN AT "
+ "POSITION (100, 200) ON PAGE RANGE \"3-4\". IT IS "
+ "WRAPPED AT THE MARGINS.";
document.writeText(s7,
fontArial,
100, 200,
PdfTextFormatter.WRAP,
"3-4");
String s8 =
"8. [writeText(String str, PdfFont f, double x, "
+ "double y, double rotation, String pageRange))"
+ " ~ writeText(s8, fontArial, 100, 300, 5.0, \"3-4\")] "
+ "THIS TEXT USES ARIAL FONT. IT GETS WRITTEN AT "
+ "POSITION (100, 300) ON PAGE RANGE \"3-4\". IT IS "
+ "ROTATED BY 5.0 DEGREES.";
document.writeText(s8, fontArial, 100, 300, 5.0, "3-4");
String s9 =
"9. [writeText(String str, PdfFont f, int alignment, "
+ "double x, double y, String pageRange)"
+ " ~ writeText(s9, fontArial, PdfTextFormatter.RIGHT, "
+ "100, 400, \"3-4\")] "
+ "THIS TEXT USES ARIAL FONT. IT GETS WRITTEN AT "
+ "POSITION (100, 400) ON PAGE RANGE \"3-4\". IT IS "
+ "ALIGNED TO THE RIGHT MARGIN.";
document.writeText(s9,
fontArial,
PdfTextFormatter.RIGHT,
100, 400,
"3-4");
String s10 =
"10. [writeText(String str, PdfFont f, int alignment, "
+ "double x, double y, boolean wrap)"
+ " ~ writeText(s10, fontArial, PdfTextFormatter.RIGHT, "
+ "100, 500, PdfTextFormatter.WRAP, \"3-4\")] "
+ "THIS TEXT USES ARIAL FONT. IT GETS WRITTEN AT POSITION "
+ "(100, 500) ON PAGE RANGE \"3-4\". IT IS ALIGNED TO THE "
+ "RIGHT MARGIN. IT GETS WRAPPED AT THE MARGINS.";
document.writeText(s10,
fontArial,
PdfTextFormatter.RIGHT,
100, 500,
PdfTextFormatter.WRAP,
"3-4");
// 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 write text using several overloaded methods
public void writeText_Example2() throws IOException,
PdfException {
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_example2_.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates string identifying the text that is to be written
String s1 =
"1. [writeText(String str, double x, double y)"
+ " ~ writeText(s1, 100, 100)] "
+ "THIS TEXT GETS WRITTEN AT POSITION (100, 100)";
document.writeText(s1, 100, 100);
String s2 =
"2. [writeText(String str, double x, double y, boolean "
+ "wrap)"
+ " ~ writeText(s2, 100, 200, PdfTextFormatter.WRAP)] "
+ "THIS TEXT GETS WRITTEN AT POSITION (100, 200) AND IS "
+ "WRAPPED AT THE MARGINS";
document.writeText(s2, 100, 200, PdfTextFormatter.WRAP);
String s3 =
"3. [writeText(String str, double x, double y, double "
+ "rotation)"
+ " ~ writeText(s3, 100, 300, 5)] "
+ "THIS TEXT GETS ROTATED BY 5.0 DEGREES AND IS WRITTEN "
+ "AT POSITION (100, 300)";
document.writeText(s3, 100, 300, 5.0);
String s4 =
"4. [writeText(String str, double x, double y, int "
+ "alignment)"
+ " ~ writeText(s4, 100, 400, PdfTextFormatter.RIGHT)] "
+ "THIS TEXT GETS WRITTEN AT POSITION (100, 400) AND IS "
+ "ALIGNED TO THE RIGHT";
document.writeText(s4, 100, 400, PdfTextFormatter.RIGHT);
String s5 =
"5. [writeText(String str, double x, double y, int "
+ "alignment, boolean wrap)"
+ " ~ writeText(s5, 100, 500, PdfTextFormatter.CENTER, "
+ "PdfTextFormatter.WRAP)] "
+ "THIS TEXT GETS WRITTEN AT POSITION (100, 500). IT IS "
+ "ALIGNED TO CENTER AND GETS WRAPPED AT THE MARGINS";
document.writeText(s5,
100, 500,
PdfTextFormatter.CENTER,
PdfTextFormatter.WRAP);
// Creates pages 2, 3 and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
String s6 =
"6. [writeText(String str, double x, double y, String "
+ "pageRange)"
+ " ~ writeText(s6, 100, 100, \"3-4\")] "
+ "THIS TEXT GETS WRITTEN AT POSITION (100, 100) ON "
+ "PAGE RANGE \"3-4\"";
document.writeText(s6, 100, 100, "3-4");
String s7 =
"7. [writeText(String str, double x, double y, boolean "
+ "wrap, String pageRange)"
+ " ~ writeText(s2, 100, 200, PdfTextFormatter.WRAP, "
+ "\"3-4\")] "
+ "THIS TEXT GETS WRITTEN AT POSITION (100, 200) AND IS "
+ "WRAPPED AT THE MARGINS ON PAGE RANGE \"3-4\"";
document.writeText(s7,
100, 200,
PdfTextFormatter.WRAP,
"3-4");
String s8 =
"8. [writeText(String str, double x, double y, double "
+ "rotation, String pageRange))"
+ " ~ writeText(s3, 100, 300, 5, \"3-4\")] "
+ "THIS TEXT GETS ROTATED BY 5.0 DEGREES AND IS WRITTEN "
+ "AT POSITION (100, 300) ON PAGE RANGE \"3-4\"";
document.writeText(s8, 100, 300, 5.0, "3-4");
String s9 =
"9. [writeText(String str, double x, double y, int "
+ "alignment, String pageRange)"
+ " ~ writeText(s4, 100, 400, PdfTextFormatter.RIGHT, "
+ "\"3-4\")] "
+ "THIS TEXT GETS WRITTEN AT POSITION (100, 400) AND "
+ "IS ALIGNED TO THE RIGHT ON PAGE RANGE \"3-4\"";
document.writeText(s9,
100, 400,
PdfTextFormatter.RIGHT,
"3-4");
String s10 =
"10. [writeText(String str, double x, double y, int "
+ "alignment, boolean wrap, String pageRange)"
+ " ~ writeText(s5, 100, 500, PdfTextFormatter.CENTER, "
+ "PdfTextFormatter.WRAP, \"3-4\")] "
+ "THIS TEXT GETS WRITTEN AT POSITION (100, 500). IT IS "
+ "ALIGNED TO CENTER AND GETS WRAPPED AT THE MARGINS ON "
+ "PAGE RANGE \"3-4\"";
document.writeText(s10,
100, 500,
PdfTextFormatter.CENTER,
PdfTextFormatter.WRAP,
"3-4");
// 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 write text using several overloaded methods
public void writeText_Example1() throws IOException,
PdfException {
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_writeText_example1_.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a page
PdfPage page1 = new PdfPage();
// Adds it to the document and makes it the document's
// current page (1)
document.add(page1, true);
// Creates a string identifying the text that is to be
// written
String s1 = "1. [writeText(java.lang.String str)"
+ " ~ writeText(s1)] "
+ "THIS TEXT JUST GETS WRITTEN AT THE CURRENT "
+ "POSITION";
document.writeText(s1);
PdfPage page2 = new PdfPage();
document.add(page2, true);
String s2 =
" 2. [writeText(java.lang.String str, boolean wrap)"
+ " ~ writeText(s2, PdfTextFormatter.NO_WRAP)] "
+ "THIS TEXT DOES NOT GET WRAPPED AT THE MARGINS";
document.writeText(s2, PdfTextFormatter.NO_WRAP);
PdfPage page3 = new PdfPage();
document.add(page3, true);
String s3 =
" 3. [writeText(java.lang.String str, int alignment)"
+ " ~ writeText(s3, PdfTextFormatter.RIGHT)] "
+ "THIS TEXT GETS RIGHT-ALIGNED";
document.writeText(s3, PdfTextFormatter.RIGHT);
PdfPage page4 = new PdfPage();
document.add(page4, true);
String s4 =
" 4. [writeText(java.lang.String str, int alignment, "
+ "boolean wrap))"
+ " ~ writeText(s4, PdfTextFormatter.LEFT, "
+ "PdfTextFormatter.WRAP)] "
+ "THIS TEXT GETS LEFT-ALIGNED AND IS ALSO WRAPPED AT "
+ "THE MARGINS";
document.writeText(s4,
PdfTextFormatter.LEFT,
PdfTextFormatter.WRAP);
// Creates two pages
PdfPage page5 = new PdfPage();
PdfPage page6 = new PdfPage();
// Adds the two pages above to the document
document.add(page5);
document.add(page6);
String s5_6 =
"5-6. [writeText(java.lang.String str, java.lang.String "
+ "pageRange)"
+ " ~ writeText(s5_6, \"5-6\")] "
+ "THIS TEXT JUST GETS WRITTEN AT THE CURRENT POSITION "
+ "ON PAGE RANGE \"5-6\"";
document.writeText(s5_6, "5-6");
PdfPage page7 = new PdfPage();
PdfPage page8 = new PdfPage();
document.add(page7);
document.add(page8);
String s7_8 =
"7-8. [writeText(java.lang.String str, boolean wrap, "
+ "java.lang.String pageRange)"
+ " ~ writeText(s7_8, PdfTextFormatter.NO_WRAP, "
+ "\"7-\"8)] "
+ "THIS TEXT DOES NOT GET WRAPPED AT THE MARGINS ON "
+ "PAGE RANGE \"7-\"8";
document.writeText(s7_8, PdfTextFormatter.NO_WRAP, "7-8");
PdfPage page9 = new PdfPage();
PdfPage page10 = new PdfPage();
document.add(page9);
document.add(page10);
String s9_10 =
" 9-10. [writeText(java.lang.String str, int alignment, "
+ "java.lang.String pageRange)"
+ " ~ writeText(s9_10, PdfTextFormatter.RIGHT, "
+ "\"9-10\")] "
+ "THIS TEXT GETS RIGHT-ALIGNED ON PAGE RANGE \"9-10\"";
document.writeText(s9_10, PdfTextFormatter.RIGHT, "9-10");
PdfPage page11 = new PdfPage();
PdfPage page12 = new PdfPage();
document.add(page11);
document.add(page12);
String s11_12 =
" 11-12. [writeText(java.lang.String str, int alignment, "
+ "boolean wrap, java.lang.String pageRange))"
+ " ~ writeText(s11_12, PdfTextFormatter.LEFT, "
+ "PdfTextFormatter.WRAP, \"11-12\")] "
+ "THIS TEXT GETS LEFT-ALIGNED AND IS ALSO WRAPPED AT "
+ "THE MARGINS ON PAGE RANGE \"11-12\"";
document.writeText(s11_12,
PdfTextFormatter.LEFT,
PdfTextFormatter.WRAP,
"11-12");
// 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 a PDF file with 20 pages. It then
// extracts pages 4 to 6 and then places them in another file.
public void extractPagesTo_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_extractPagesTo_example_file1.pdf");
PdfDocument document = new PdfDocument(writer);
// Adds 20 new pages to the document and writes some text
// identifying each page
for (int i = 1; i <= 20; i++)
{
PdfPage page = new PdfPage();
document.add(page, true);
document.writeText("This is page #" + i);
}
// Extracts pages in page range 4 to 6 to another file
document.extractPagesTo(
"PdfDocument_extractPagesTo_example_file2.pdf", "4-6");
// Extracts pages in page range 4 to 6 and writes it in PDF
// version 1.4 to another file. The file is set to be opened
// immediately afterwards.
document.extractPagesTo(
"PdfDocument_extractPagesTo_example_file3.pdf",
"4-6", // pageRange
PdfDocument.VERSION_1_4, // extractAsVersion
true); // openAfterExtraction
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment draws squares using several overloaded
// methods
public void drawSquare_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawSquare_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Draws a square 100 pixels wide at position (100, 100)
document.drawSquare(100, 100, 100);
// Draws a square 200 pixels wide at position (300, 300)
document.drawSquare(300, 300, 200, true, true);
// Writes text identifying the above squares
document.writeText(
"drawSquare(double x, double y, double length)",
100, 60);
document.writeText("drawSquare(100, 100, 100)", 100, 80);
document.writeText(
"drawSquare(double x, double y, double length, "
+ "boolean isFill, boolean isStroke)",
100, 260);
document.writeText(
"drawSquare(300, 300, 200, true, true)",
100, 280);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws a square 100 pixels wide at position (100, 100)
// on pages in page range 3 to 4
document.drawSquare(100, 100, 100, "3-4");
// Draws a square 200 pixels wide at position (300, 300)
// on pages in page range 3 to 4
document.drawSquare(300, 300, 200, true, true, "3-4");
document.writeText(
"void drawSquare(double x, double y, double length, "
+ "java.lang.String pageRange)",
100, 60, "3-4");
document.writeText(
"drawSquare(100, 100, 100, \"3-4\")",
100, 80, "3-4");
document.writeText(
"void drawSquare(double x, double y, double length, "
+ "boolean isFill, boolean isStroke, "
+ "java.lang.String pageRange)",
100, 250, "3-4");
document.writeText(
"drawSquare(300, 300, 200, true, true, \"3-4\")",
100, 280, "3-4");
// 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 rectangles with rounded
// corners using overloaded methods
public void drawRoundRect_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawRoundRect_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Sets pen and brush colors
document.setPenColor(Color.RED);
document.setBrushColor(Color.GREEN);
// Creates a default page (1) and draws a rectangle with
// rounded corners
document.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
document.drawLine(100, 410, 150, 410);
document.drawLine(100, 407, 100, 413);
document.drawLine(150, 407, 150, 413);
document.writeText("arcWidth = 50", 80, 420);
document.drawLine(413, 100, 419, 100);
document.drawLine(413, 200, 419, 200);
document.drawLine(416, 100, 416, 200);
document.writeText("arcHeight = 100", 430, 140);
document.writeText(".", 100, 100);
document.writeText("(x,y) = (100, 100)", 70, 80);
document.writeText(
"drawRoundRect(double x, double y, double "
+ "width, double height, double arcWidth, "
+ "double arcHeight, boolean isFill, boolean "
+ "isStroke)", 100, 470);
document.writeText(
"drawRoundRect(100, 100, 300, 300, 50, 100, "
+ "true, true)",
100, 500);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws a rectangle with rounded corners on pages in page
// range 3 to 4
document.drawRoundRect(200, 200, // x and y
300, 300, // width and height
150, 75, // arcWidth and arcHeight
true, // isFill
true, // isStroke
"3-4"); // pageRange
document.writeText(
"drawRoundRect(double x, double y, double width,"
+ "double height, double arcWidth, double "
+ "arcHeight, boolean isFill, boolean isStroke, "
+ "java.lang.String pageRange)",
100, 150,
"3-4");
document.writeText(
"drawRoundRect(200, 200, 300, 300, 150, 75, true, "
+ "true, \"3-4\")",
100, 180,
"3-4");
// 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 rectangles using overloaded
// methods
public void drawRect_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawRect_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates a default page (1) and draws a rectangle specified
// with a PdfRect object
document.drawRect(new PdfRect(100, 100, 200, 200));
// Draws a rectangle specified with a java.awt.Rectangle
// object
document.drawRect(new Rectangle(400, 100, 200, 200));
// Draws a rectangle beginning at position (100, 400) with
// width 200 and height 200
document.drawRect(100, 400, 200, 200);
// Sets pen and brush colors
document.setPenColor(Color.RED);
document.setBrushColor(Color.GREEN);
// Draws a rectangle beginning at position (100, 700) with
// width 200 and height 200
document.drawRect(100, 700, 200, 200, true, true);
// Writes text identifying the above rectangles and settings
document.writeText("drawRect(PdfRect r)", 100, 60);
document.writeText(
"drawRect(new PdfRect(100, 100, 200, 200)",
100, 80);
document.writeText("drawRect(java.awt.Rectangle r)",
400, 60);
document.writeText(
"drawRect(new Rectangle(400, 100, 200, 200))",
400, 80);
document.writeText(
"drawRect(double x, double y, double width, "
+ "double height)",
100, 360);
document.writeText("drawRect(100, 400, 200, 200)", 100, 380);
document.writeText("setPenColor(Color.RED)", 400, 500);
document.writeText("setBrushColor(Color.GREEN)", 400, 520);
document.writeText(
"drawRect(double x, double y, double width, "
+ "double height, boolean isFill, boolean "
+ "isStroke)",
100, 660);
document.writeText("drawRect(400, 700, 200, 200, true, "
+ "true)",
100, 680);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws a rectangle specified with a PdfRect object on pages
// in page range 3 to 4
document.drawRect(new PdfRect(100, 100, 200, 200), "3-4");
// Draws a rectangle specified with a java.awt.Rectangle
// object on pages in page range 3 to 4
document.drawRect(new Rectangle(400, 100, 200, 200), "3-4");
// Draws a rectangle beginning at position (100, 400) with
// width 200 and height 200
document.drawRect(100, 400, 200, 200, "3-4");
// Changes pen and brush colors
document.setPenColor(Color.GREEN);
document.setBrushColor(Color.RED);
// Draws a rectangle specified with pen and brush settings on
// pages in page range 3 to 4
document.drawRect(100, 700, 200, 200, true, true, "3-4");
// Writes text identifying the above rectangles and settings
// on pages in page range 3 to 4
document.writeText(
"drawRect(PdfRect r, java.lang.String pageRange)", 100,
60, "3-4");
document.writeText(
"drawRect(new PdfRect(100, 100, 200, 200), \"3-4\")",
100, 80, "" + "3-4");
document.writeText(
"drawRect(java.awt.Rectangle r, java.lang.String "
+ "pageRange)",
400, 60, "3-4");
document.writeText(
"drawRect(new Rectangle(400, 100, 200, 200), \"3-4\")",
400, 80, "3-4");
document.writeText(
"drawRect(double x, double y, double width, double "
+ "height, java.lang.String pageRange)",
100, 360,
"3-4");
document.writeText("drawRect(100, 400, 200, 200, \"3-4\")",
100, 380,
"3-4");
document.writeText("setPenColor(Color.GREEN)",
400, 500,
"3-4");
document.writeText("setBrushColor(Color.RED)",
400, 520,
"3-4");
document.writeText(
"drawRect(double x, double y, double width, double "
+ "height, boolean isFill, boolean isStroke, "
+ "java.lang.String pageRange)",
100, 650,
"3-4");
document.writeText(
"drawRect(400, 700, 200, 200, true, false, \"3-4\")",
100, 680,
"3-4");
// 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 polylines using overloaded
// methods
public void drawPolyline_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawPolyline_example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Sets pen and brush colors
document.setPenColor(Color.RED);
document.setBrushColor(Color.GREEN);
// Creates arrays of x and y coordinates
double x[] = { 100, 200, 300, 400, 450 };
double y[] = { 100, 50, 100, 300, 150 };
// Creates page 1 and draws a 5-line polyline
document.drawPolyline(x, y, 5);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws a 4-line polyline on pages 3 and 4
document.drawPolyline(x, y, 4, "3-4");
// Writes text identifying the above polylines
document.writeText(". (100, 100)", 100, 100, "1,3,4");
document.writeText(". (200, 50)", 200, 50, "1,3,4");
document.writeText(". (300, 100)", 300, 100, "1,3,4");
document.writeText(". (400, 300)", 400, 300, "1,3,4");
document.writeText(". (450, 150)", 450, 150, "1,3,4");
// 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 polygons using several overloaded
// methods
public void drawPolygon_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawPolygon_example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Sets pen and brush colors
document.setPenColor(Color.RED);
document.setBrushColor(Color.GREEN);
// Creates two arrays of x and y coordinates
double x[] = { 100, 200, 300, 400, 450 };
double y[] = { 100, 50, 100, 300, 150 };
// Creates a default page (1) and draws a polygon based on
// the above arrays
document.drawPolygon(x, y, 5, true, true);
// Writes text identifying the above polygon
document.writeText(
"drawPolygon(double xPoints[], double yPoints[], "
+ "int nPoints, boolean isFill, boolean isStroke) ~ "
+ "drawPolygon(x, y, 5, true, true)", 200, 350);
// Creates pages 2, 3, 4, and 5
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
PdfPage page5 = new PdfPage();
// Adds pages 2, 3, 4 & 5 to document
document.add(page2);
document.add(page3);
document.add(page4);
document.add(page5);
// Draws polygons on pages in a page range
document.drawPolygon(x, y, 5, true, false, "2-3");
document.drawPolygon(x, y, 5, false, true, "4-5");
// Writes text identifying the above polygons
document.writeText(
"drawPolygon(double xPoints[], double yPoints[],"
+ "int nPoints, boolean isFill, boolean isStroke, "
+ "String pageRange)", 200, 350, "2-5");
document.writeText(
"drawPolygon(x, y, 5, true, false, \"2-3\")", 350, 70,
"2-3");
document.writeText(
"drawPolygon(x, y, 5, false, true, \"4-5\")", 350, 70,
"4-5");
document.writeText(". (100, 100)", 100, 100, "1-5");
document.writeText(". (200, 50)", 200, 50, "1-5");
document.writeText(". (300, 100)", 300, 100, "1-5");
document.writeText(". (400, 300)", 400, 300, "1-5");
document.writeText(". (450, 150)", 400, 150, "1-5");
// 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 pie segments using overloaded
// methods
public void drawPie_Example() throws IOException, PdfException {
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawPie_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Sets pen and brush colors
document.setPenColor(Color.RED);
document.setBrushColor(Color.GREEN);
// Draws pie segments with different fill and brush settings
document.drawPie(100, 200, // x and y
150, 150, // width and height
22.5, // startAngle
135, // arcAngle
true, // isFill
true); // isStroke
document.drawPie(300, 200,
150, 150,
90,
135,
true,
false);
document.drawPie(500, 200,
150, 150,
22.5,
225.5,
false,
true);
// Writes text identifying the above pie segments
document.writeText("drawPie(int x, int y, int width, int "
+ "height, double startAngle, double "
+ "arcAngle, boolean isFill, boolean "
+ "isStroke)",
100, 100);
document.writeText("isFill = true", 100, 160);
document.writeText("isStroke = true", 100, 180);
document.writeText("isFill = true", 300, 160);
document.writeText("isStroke = false", 300, 180);
document.writeText("isFill = false", 500, 160);
document.writeText("isStroke = true", 500, 180);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws a pie segment on pages in page range 3 to 4
document.drawPie(100, 200,
150, 150,
22.5,
135,
true,
true,
"3-4");
document.writeText("drawPie(int x, int y, int width, int "
+ "height, double startAngle, double "
+ "arcAngle, boolean isFill, boolean "
+ "isStroke, java.lang.String pageRange)",
100, 100,
"3-4");
document.writeText("drawPie(100, 100, 150, 150, 22.5, 135,"
+ " true, true, \"3-4\")",
100, 180,
"3-4");
// 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 several overloaded methods
public void drawLine_Example() throws IOException, PdfException {
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawLine_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Draws line from position (100, 100) to position (400, 400)
document.drawLine(100, 100, 400, 400);
// Writes text identifying the line
document.writeText(
"drawLine(100, 100, 400, 400)",
50, 60);
document.writeText(
"drawLine(double startx, double starty, double "
+ "endx, double endy)",
50, 80);
// Creates two PdfPoint objects
PdfPoint point1 = new PdfPoint(500, 200);
PdfPoint point2 = new PdfPoint(500, 500);
// Draws a line between the two PdfPoint objects
document.drawLine(point1, point2);
document.writeText(
"drawLine(point1, point2)",
450, 140);
document.writeText(
"drawLine(PdfPoint start, PdfPoint end)",
450, 160);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws line between position (100, 100) to position
// (400, 400) on pages in page range 3 to 4
document.drawLine(100, 100, 400, 400, "3-4");
document.writeText(". (100, 100)", 100, 100, "1, 3-4");
document.writeText(". (400, 400)", 400, 400, "1, 3-4");
document.writeText(". point1 (500, 200)",
500, 200,
"1, 3-4");
document.writeText(". point2 (500, 500)",
500, 500,
"1, 3-4");
document.writeText(
"drawLine(100, 100, 400, 400, \"3-4\")",
50, 60,
"3-4");
document.writeText(
"drawLine(double startx, double starty, "
+ "double endx, double endy, "
+ "String pageRange)",
50, 80,
"3-4");
// Draws line between the two PdfPoint objects on pages in
// page range 3 to 4
document.drawLine(point1, point2, "3-4");
document.writeText(
"drawLine(point1, point2, \"3-4\")",
450,
130,
"3-4");
document.writeText(
"drawLine(PdfPoint start, PdfPoint "
+ "end, String pageRange)",
450,
150,
"3-4");
// 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 images with overloaded methods
// that use String pathnames and PdfPoint objects.
public void drawImage_Example6() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawImage_"
+ "example6.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Assumes that a 468-by-60-pixel image exists
// inside "InputDocs" folder in current directory
String imagePathname = ".\\InputDocs\\banner_img_468_60.jpg";
PdfPoint point1 = new PdfPoint(100, 100);
PdfPoint point2 = new PdfPoint(100, 300);
PdfPoint point3 = new PdfPoint(100, 500);
PdfPoint point4 = new PdfPoint(100, 700);
// Creates a default page (page 1) and draws image at a point
document.drawImage(imagePathname, point1);
// Writes text identifying the above image
document.writeText(
"drawImage(java.lang.String Path, PdfPoint pt)",
100, 60);
document.writeText(
"document.drawImage(imagePathname, point1)",
100, 80);
// Draws image at a point with rotation angle at 5 degrees
document.drawImage(imagePathname, point2, 5);
document.writeText("drawImage(java.lang.String Path, "
+ "PdfPoint pt, double rotation)",
100, 260);
document.writeText("document.drawImage(imagePathname, "
+ "point2, 5)",
100, 280);
// Draws image at a point with width 460 and height 100
document.drawImage(imagePathname, point3, 460, 100);
document.writeText(
"drawImage(java.lang.String Path, PdfPoint pt,"
+ "double width, double height)",
100, 460);
document.writeText(
"document.drawImage(imagePathname, "
+ "point3, 460, 100)",
100, 480);
// Draws image at a point with width 460 and height 100.
// Rotation angle is set at 5 degrees
document.drawImage(imagePathname, point4, 460, 100, 5);
document.writeText("drawImage(java.lang.String Path, "
+ "PdfPoint pt, double width, double "
+ "height, double rotation)",
100, 660);
document.writeText("drawImage(imagePathname, point4, "
+ "460, 100, 5)",
100, 680);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws image at a point on pages in page range 3 to 4
document.drawImage(imagePathname, point1, "3-4");
document.writeText(
"drawImage(java.lang.String Path, PdfPoint pt, "
+ "java.lang.String pageRange)",
100, 60,
"3-4");
document.writeText(
"drawImage(imagePathname, point1, \"3-4\")",
100, 80,
"3-4");
// Draws image at a point on pages in page range 3 to 4.
// Rotation angle is set at 5 degrees
document.drawImage(imagePathname, point2, 5, "3-4");
document.writeText(
"drawImage(java.lang.String Path, PdfPoint pt, "
+ "double rotation, java.lang.String "
+ "pageRange)",
100, 260,
"3-4");
document.writeText(
"drawImage(imagePathname, point2, 5, \"3-4\")",
100, 280,
"3-4");
// Draws an image at a point on pages in page in page range 3
// to 4. Height is set at 460 and width is set at 100.
document.drawImage(imagePathname, point3, 460, 100, "3-4");
document.writeText(
"drawImage(java.lang.String Path, PdfPoint pt, "
+ "double width, double height, "
+ "java.lang.String pageRange)",
100, 450,
"3-4");
document.writeText(
"drawImage(imagePathname, point3, 460, 100, "
+ "\"3-4\")",
100, 480,
"3-4");
// Draws an image at a point on pages in page range 3 to 4.
// Rotation angle is set at 5 degrees. Height is set at 460
// and width is set at 100.
document.drawImage(imagePathname, point4, 460, 100, 5, "3-4");
document.writeText(
"drawImage(String path, PdfPoint pt, double "
+ "width, double height, double rotation, "
+ "String pageRange)",
100, 660,
"3-4");
document.writeText(
"drawImage(imagePathname, point4, 460, 100, 5, "
+ "\"3-4\")",
100, 680,
"3-4");
// 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 images with overloaded methods
// that use PdfImage and PdfPoint objects
public void drawImage_Example5() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawImage_"
+ "example5.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates a PdfImage object from a 468-by-60-pixel image
// inside "InputDocs" folder in current directory
PdfImage image = PdfImage.create(
".\\InputDocs\\banner_img_468_60.jpg");
PdfPoint point1 = new PdfPoint(100, 100);
PdfPoint point2 = new PdfPoint(100, 300);
PdfPoint point3 = new PdfPoint(100, 500);
PdfPoint point4 = new PdfPoint(100, 700);
// Creates a default page (page 1) and draws image at a point
document.drawImage(image, point1);
// Writes text identifying the above image
document.writeText("drawImage(PdfImage img, PdfPoint pt)",
100, 60);
document.writeText("document.drawImage(image, point1)",
100, 80);
// Draws image at a point with rotation angle at 5 degrees
document.drawImage(image, point2, 5);
document.writeText("drawImage(PdfImage img, PdfPoint pt, "
+ "double rotation)",
100, 260);
document.writeText("document.drawImage(image, point2, 5)",
100, 280);
// Draws image at a point with width 460 and height 100
document.drawImage(image, point3, 460, 100);
document.writeText("drawImage(PdfImage img, PdfPoint pt, "
+ "double width, double height)",
100, 460);
document.writeText(
"document.drawImage(image, point3, 460, 100)",
100, 480);
// Draws image at a point with width 460 and height 100.
// Rotation angle is set at 5 degrees
document.drawImage(image, point4, 460, 100, 5);
document.writeText("drawImage(PdfImage img, PdfPoint pt, "
+ "double width, double height, double "
+ "rotation)",
100, 660);
document.writeText("drawImage(image, point4, 460, 100, 5)",
100, 680);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws image at a point on pages in page range 3 to 4
document.drawImage(image, point1, "3-4");
document.writeText("drawImage(PdfImage img, PdfPoint pt, "
+ "java.lang.String pageRange)",
100, 60,
"3-4");
document.writeText("drawImage(image, point1, \"3-4\")",
100, 80,
"3-4");
// Draws image at a point on pages in page range 3 to 4.
// Rotation angle is set at 5 degrees.
document.drawImage(image, point2, 5, "3-4");
document.writeText("drawImage(PdfImage img, PdfPoint pt, "
+ "double rotation, java.lang.String "
+ "pageRange)",
100, 260,
"3-4");
document.writeText("drawImage(image, point2, 5, \"3-4\")",
100, 280,
"3-4");
// Draws an image at a point on pages in page range 3 to 4.
// Height is set at 460 and width is set at 100.
document.drawImage(image, point3, 460, 100, "3-4");
document.writeText("drawImage(PdfImage img, PdfPoint pt, "
+ "double width, double height, "
+ "java.lang.String pageRange)",
100, 460,
"3-4");
document.writeText("drawImage(image, point3, 460, 100, "
+ "\"3-4\")",
100, 480,
"3-4");
// Draws an image at a point on pages in page range 3 to 4.
// Rotation angle is set at 5 degrees. Height is set at 460
// and width is set at 100.
document.drawImage(image, point4, 460, 100, 5, "3-4");
document.writeText("drawImage(PdfImage img, PdfPoint pt, "
+ "double width, double height, double "
+ "rotation, java.lang.String pageRange)",
100, 650,
"3-4");
document.writeText("drawImage(image, point4, 460, 100, 5, "
+ "\"3-4\")",
100, 680,
"3-4");
// 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 images with overloaded methods
// that use String pathnames and PdfRect objects
public void drawImage_Example4() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawImage_"
+ "example4.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Assumes that this image exists in inside "InputDocs"
// folder of the local directory exists
String imagePathname = ".\\InputDocs\\banner_img_468_60.jpg";
// Creates a default page (1).
// Draws image inside rectangle on the page
document.drawImage(imagePathname,
new PdfRect(100, 100, 468, 60));
document.writeText("drawImage(imagePathname, new "
+ "PdfRect(100, 100, 468, 60))",
100, 60);
document.writeText("drawImage(java.lang.String path, "
+ "PdfRect rect)",
100, 80);
// Draws image inside rectangle on page 1 with a rotation
// of 5 degrees
document.drawImage(imagePathname,
new PdfRect(100, 300, 468, 60),
5);
document.writeText("drawImage(imagePathname, new "
+ "PdfRect(100, 300, 468, 60), 5)",
100, 260);
document.writeText("drawImage(java.lang.String path, "
+ "PdfRect rect, double rotation)",
100, 280);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws image inside rectangle on pages in page range 3 to 4
document.drawImage(imagePathname,
new PdfRect(100, 500, 468, 60),
"3-4");
document.writeText("drawImage(imagePathname, new PdfRect( "
+ "100, 500, 468, 60), \"3-4\")",
100, 460,
"3-4");
document.writeText("drawImage(java.lang.String path, "
+ "PdfRect rect, java.lang.String "
+ "pageRange)",
100, 480,
"3-4");
// Draws image inside rectangle on pages in page range 3 to 4
// with a rotation of 5 degrees
document.drawImage(imagePathname,
new PdfRect(100, 700, 468, 60),
5,
"3-4");
document.writeText("drawImage(imagePathname, new "
+ " PdfRect(100, 700, 468, 60), 5,"
+ "\"3-4\")",
100, 660, "3-4");
document.writeText("drawImage(java.lang.String path, "
+ "PdfRect rect, double rotation, "
+ "java.lang.String pageRange)",
100, 680, "3-4");
// 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 images with overloaded methods
// that use String pathnames.
public void drawImage_Example3() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawImage_"
+ "example3.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Assumes that this image exists in inside "InputDocs"
// folder of the local directory exists
String imagePathname = ".\\InputDocs\\banner_img_468_60.jpg";
// Creates a default page (1) and draws the image
// at (100, 100)
document.drawImage(imagePathname, 100, 100);
// Writes text identifying the above image
document.writeText("drawImage(java.lang.String path, "
+ "double x, double y)",
100, 60);
document.writeText("drawImage(imagePathname, 100, 100)",
100, 80);
// Draws image at (100, 300) with a 5-degree tilt
document.drawImage(imagePathname, 100, 300, 5);
document.writeText("drawImage(java.lang.String path, "
+ "double x, double y, double rotation)",
100, 260);
document.writeText("drawImage(imagePathname, 100, 300, 5)",
100, 280);
// Draws an image at (100, 500) with width 468 and
// height 60
document.drawImage(imagePathname, 100, 500, 468, 60);
document.writeText("drawImage(java.lang.String path, "
+ "double x, double y, double width,"
+ "double height)",
100, 460);
document.writeText("drawImage(imagePathname, 100, 500, "
+ "468, 60)",
100, 480);
// Draws an image at (100, 700) with width 468 and
// height 60. Tilt is set at 5 degrees.
document.drawImage(imagePathname, 100, 700, 468, 60, 5);
document.writeText("drawImage(java.lang.String path, double "
+ "x, double y, double width, double "
+ "height, double rotation)",
100, 660);
document.writeText("drawImage(imagePathname, 100, 700, "
+ "468, 60, 5)",
100, 680);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws image at (100, 100) on pages in page range 3 to 4
document.drawImage(imagePathname, 100, 100, "3-4");
document.writeText("drawImage(java.lang.String path, "
+ "double x, double y, "
+ "java.lang.String pageRange)",
100, 60, "3-4");
document.writeText("drawImage(imagePathname, 100, 100, "
+ "\"3-4\")",
100, 80, "3-4");
// Draws image at (100, 300) with a 5-degree tilt
// on pages in page range 3 to 4
document.drawImage(imagePathname, 100, 300, 5, "3-4");
document.writeText("drawImage(java.lang.String path, "
+ "double x, double y, double rotation,"
+ "java.lang.String pageRange)",
100, 260, "3-4");
document.writeText("drawImage(imagePathname, 100, 300, 5, "
+ "\"3-4\")",
100, 280, "3-4");
// Draws image at (100, 500) with width 468
// and height 60 on pages in page range 3 to 4
document.drawImage(imagePathname, 100, 500, 468, 60, "3-4");
document.writeText("void drawImage(java.lang.String path, "
+ "double x, double y, double width, "
+ "double height, java.lang.String "
+ "pageRange)",
100, 450,
"3-4");
document.writeText("drawImage(imagePathname, 100, 500, 468, "
+ "60, \"3-4\")",
100, 480,
"3-4");
// Draws image at (100, 700) with width 468 and height 60 on
// pages in page range 3 to 4. Tilt is set at 5 degrees.
document.drawImage(imagePathname,
100, 700,
468, 60,
5,
"3-4");
document.writeText("drawImage(java.lang.String path, "
+ "double x, double y, double width, "
+ "double height, double rotation, "
+ "java.lang.String pageRange)",
100, 650,
"3-4");
document.writeText("drawImage(imagePathname, 100, 700, 468, "
+ "60, 5, \"3-4\")",
100, 680,
"3-4");
// 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 images with overloaded methods
// that use PdfImage and PdfRect objects.
public void drawImage_Example2() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawImage_"
+ "example2.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates a PdfImage object
PdfImage image = PdfImage.create(
".\\InputDocs\\banner_img_468_60.jpg");
// Creates a default page (1) and draws an image inside a
// rectangle
document.drawImage(image, new PdfRect(100, 100, 468, 60));
// Writes text identifying the above image
document.writeText("drawImage(PdfImage img, PdfRect rect)",
100,
60);
document.writeText("drawImage(image, new "
+ "PdfRect(100, 100, 468, 60))",
100,
80);
// Draws an image rotated by 5 degrees inside a rectangle
document.drawImage(image,
new PdfRect(100, 300, 468, 60),
5);
document.writeText("drawImage(PdfImage img, PdfRect rect, "
+ "double rotation)",
100,
260);
document.writeText("drawImage(image, new "
+ "PdfRect(100, 300, 468, 60), 5)",
100,
280);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws image inside a rectangle on pages in page
// range 3 to 4
document.drawImage(image,
new PdfRect(100, 100, 468, 60),
"3-4");
document.writeText("drawImage(PdfImage img, PdfRect rect, "
+ "java.lang.String pageRange)",
100,
60,
"3-4");
document.writeText("drawImage(image, new PdfRect(100, 100, "
+ "468, 60), \"3-4\")",
100,
80,
"3-4");
// Draws image rotated by 5 degrees inside a rectangle on
// pages in page range 3 to 4
document.drawImage(image,
new PdfRect(100, 300, 468, 60),
5,
"3-4");
document.writeText("drawImage(PdfImage img, PdfRect rect, "
+ "double rotation, java.lang.String "
+ "pageRange)",
100,
260,
"3-4");
document.writeText("drawImage(image, new "
+ "PdfRect(100, 300, 468, 60), 5, "
+ "\"3-4\")",
100,
280,
"3-4");
// 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 images with overloaded methods
// that use PdfImage objects.
public void drawImage_Example1() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawImage_"
+ "example1.pdf");
PdfDocument document = new PdfDocument(writer);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
// Creates a PdfImage object
PdfImage image = PdfImage.create(
".\\InputDocs\\banner_img_468_60.jpg");
// Creates a default page (1) and draws image at
// position (100, 100)
document.drawImage(image, 100, 100);
// Writes text identifying the above image
document.writeText(
"drawImage(PdfImage img, double x, double y)",
100, 60);
document.writeText("drawImage(image, 100, 100)", 100, 80);
// Draws image at position (100, 300) with image rotated to
// 5 degrees
document.drawImage(image, 100, 300, 5);
document.writeText(
"drawImage(PdfImage img, double x, "
+ "double y, double rotation)",
100, 260);
document.writeText("drawImage(image, 100, 300, 5)",
100, 280);
// Draws image at position (100, 500) with width 400 and
// height 60
document.drawImage(image, 100, 500, 400, 60);
document.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "double width, double height)",
100, 460);
document.writeText(
"drawImage(image, 100, 500, 400, 60)",
100, 480);
// Draws image at position (100, 700) with width 460 and
// height 60. The image is rotated by 5 degree.
document.drawImage(image, 100, 700, 460, 60, 5);
document.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "double width, double height, double "
+ "rotation)",
100, 660);
document.writeText(
"drawImage(image, 100, 700, 460, 60, 5)",
100, 680);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws image on pages in page range 3 to 4 at position
// (100, 100)
document.drawImage(image, 100, 100, "3-4");
document.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "java.lang.String pageRange)",
100, 60,
"3-4");
document.writeText(
"drawImage(image, 100, 100, \"3-4\")",
100, 80,
"3-4");
// Draws image rotated by 5 degrees at position (100, 300)
// on pages in page range 3 to 4
document.drawImage(image, 100, 300, 5, "3-4");
document.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "double rotation, java.lang.String pageRange)",
100, 260,
"3-4");
document.writeText(
"document.drawImage(image, 100, 300, 5, "
+ "\"3-4\")",
100, 280,
"3-4");
// Draws image at (100, 500) with width 460 and height 60
// on pages in page range 3 to 4
document.drawImage(image, 100, 500, 460, 60, "3-4");
document.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "double width, double height, "
+ "java.lang.String pageRange)",
100, 460,
"3-4");
document.writeText(
"drawImage(image, 100, 500, 460, 60, \"3-4\")",
100, 480,
"3-4");
// Draws image rotated at 5 degrees on position (100, 700)
// with width 460 and height 60 on pages in page range 3 to 4
document.drawImage(image, 100, 700, 460, 60, 5, "3-4");
document.writeText(
"drawImage(PdfImage img, double x, double y, "
+ "double width, double height, "
+ "java.lang.String pageRange)",
100, 650,
"3-4");
document.writeText(
"drawImage(image, 100, 700, 460, 60, 5, "
+ "\"3-4\")",
100, 680,
"3-4");
// Checks and sets compression level of the document
if (document.getCompressionLevel()
!= PdfFlateFilter.BEST_COMPRESSION)
{
document.setCompressionLevel(
PdfFlateFilter.BEST_COMPRESSION);
}
// 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 ellipses using overloaded
// methods
public void drawEllipse_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawEllipse_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Sets pen and brush colors
document.setPenColor(Color.RED);
document.setBrushColor(Color.GREEN);
// Creates page 1 by default and draws ellipses with various
// settings on page 1
document.drawEllipse(100, 50, 200, 100, true, true);
document.drawEllipse(250, 50, 350, 100, true, false);
document.drawEllipse(400, 50, 500, 100, false, true);
// Writes text identifying the above ellipses on page 1
document.writeText("isFill = true", 100, 20);
document.writeText("isStroke = true", 100, 30);
document.writeText("isFill = true", 250, 20);
document.writeText("isStroke = false", 250, 30);
document.writeText("isFill = false", 400, 20);
document.writeText("isStroke = true", 400, 30);
// Creates page 2 and 3
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws a ellipse on pages in page range 3 to 4
document.drawEllipse(100, 50, 200, 100, true, true, "3-4");
// Writes text identifying the above ellipse on page range
// 3 to 4
document.writeText("isFill = true", 100, 10, "3-4");
document.writeText("isStroke = true", 100, 20, "3-4");
document.writeText("pageRange = \"3-4\"", 100, 30, "3-4");
// 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 circles using overloaded
// methods
public void drawCircle_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawCircle_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Sets pen and brush colors
document.setPenColor(Color.RED);
document.setBrushColor(Color.GREEN);
// Creates page 1 by default and draws circles with various
// settings on page 1
document.drawCircle(150, 100, 50, true, true);
document.drawCircle(300, 100, 50, true, false);
document.drawCircle(450, 100, 50, false, true);
// Writes text identifying the above circles on page 1
document.writeText("isFill = true", 100, 20);
document.writeText("isStroke = true", 100, 30);
document.writeText("isFill = true", 250, 20);
document.writeText("isStroke = false", 250, 30);
document.writeText("isFill = false", 400, 20);
document.writeText("isStroke = true", 400, 30);
// Creates pages 2, 3, and 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds pages 2, 3 & 4 to document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws a circle on pages in page range 3 to 4
document.drawCircle(150, 100, 50, true, true, "3-4");
// Writes text identifying the above circle on page range
// 3 to 4
document.writeText("isFill = true", 100, 10, "3-4");
document.writeText("isStroke = true", 100, 20, "3-4");
document.writeText("pageRange = \"3-4\"", 100, 30, "3-4");
// 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 arcs with overloaded methods
public void drawArc_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawArc_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a rectangle
PdfRect rectangle = new PdfRect(150, 200, 300, 300);
// Creates page 1 and draws an arc
document.drawArc(rectangle, 22.5, 235);
// Writes text identifying the above arc
document.writeText(
"drawArc(PdfRect rect, double startAngle, double "
+ "arcAngle) ~ drawArc(rectangle, 22.5, 235)",
rectangle);
// Creates pages 2 to 4
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Adds the pages to the document
document.add(page2);
document.add(page3);
document.add(page4);
// Draws an arc on pages 3 and 4
document.drawArc(rectangle, 22.5, 235, "3-4");
// Writes text identifying the starting point
// of the arc's bounding box
document.drawRect(rectangle, "1, 3, 4");
// Writes text idenifying the arcs on pages 3 & 4
document.writeText(
"drawArc(PdfRect rect, double startAngle, double "
+ "arcAngle, String pageRange) ~ drawArc(rectangle, "
+ "22.5, 235, \"3-4\")",
rectangle,
"3-4");
// 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 a document with 4 A4-size pages.
// It then disables the margins on some of the pages and
// and enables margins on others. Text is written to all the
// pages demonstrate the effect of the above settings.
public void AllMargins_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_AllMargins_"
+ "_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page4 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Adds pages to document
document.add(page1);
document.add(page2);
document.add(page3);
document.add(page4);
// Disables margins in pages in page range 1 and 4
document.disableAllMargins("1, 4");
// Writes a few lines of text to demonstrate the effect
document.writeText(
"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.",
"1, 4");
document.writeText("disableAllMargins(\"1, 4\")", 100, 100, "1, 4");
// Enables margins on pages in page range 2 to 3
document.enableAllMargins("2-3");
document.writeText(
"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.",
"2-3");
document.writeText("enableAllMargins(\"2-3\")", 100, 100, "2-3");
// 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 a document with 20 pages and then
// deletes pages 3 to 4 from it
public void deletePages_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_deletePages_Example_input.pdf");
PdfDocument document1 = new PdfDocument(writer);
// Adds 20 new pages to the document and writes some text
// identifying each page
for (int i = 1; i <= 20; i++)
{
PdfPage page = new PdfPage();
document1.add(page, true);
document1.writeText("This is page #" + i);
}
// Writes text identifying the pages
document1.writeText(
" of PdfDocument_deletePages_Example_input.pdf", "1-20");
// Sets the file to be opened after it is written to
document1.setOpenAfterSave(true);
// Writes the document object to file
document1.write();
// Closes all I/O streams associated with this reader object.
writer.dispose();
PdfReader reader = PdfReader.fileReader(
"PdfDocument_deletePages_Example_input.pdf");
PdfDocument document2 = new PdfDocument(reader);
// Sets pathname of output file
reader.setOutFilePath(
"PdfDocument_deletePages_example_output.pdf");
// Deletes pages from 3 to 5
document2.deletePages("3-5");
document2.writeText(
" on PdfDocument_deletePages_example_output.pdf",
0, 30,
"1-17");
document2.setOpenAfterSave(true);
document2.write();
reader.dispose();
}
// This code segment extracts a few pages from another
// document and then appends them to a newly created
// document.
public void appendPagesFrom_String_String_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_appendPagesFrom_"
+ "String_String_Example_file1.pdf");
PdfDocument document1 = new PdfDocument(writer);
// Adds 20 new pages to the document and writes some text
// identifying each page
for (int i = 1; i <= 20; i++)
{
PdfPage page = new PdfPage();
document1.add(page, true);
document1.writeText("This is page #" + i);
}
// Writes text identifying the pages
document1.writeText(
" of PdfDocument_appendPagesFrom_String_"
+ "String_Example_file1.pdf",
"1-20");
// Sets the file to be opened after it is written to
document1.setOpenAfterSave(true);
// Writes the document object to file
document1.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
writer = PdfWriter.fileWriter(
"PdfDocument_appendPagesFrom_"
+ "String_String_Example_file2.pdf");
PdfDocument document2 = new PdfDocument(writer);
// Extracts pages 4 and 5 from the previous file and appends
// them to this document
document2.appendPagesFrom(
"PdfDocument_appendPagesFrom_"
+ "String_String_Example_file1.pdf",
"4-5");
document2.writeText(
" on PdfDocument_appendPagesFrom_String_"
+ "String_Example_file2.pdf",
0, 50,
"2-3");
document2.setOpenAfterSave(true);
document2.write();
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, a PdfImage object is added as
// watermark to the document in various ways.
public void addWatermarkImage_String_int_boolean_double_boolean_String_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addWatermarkImage_String_int_"
+ "boolean_double_boolean_String_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Adds image specified by its pathname as watermark.
// The watermark is center-aligned vertically and
// middle-aligned horizontally. This position is made
// with reference to page margins.
document.addWatermarkImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
true, // applyPageMargins set to true
45,
true,
"1");
// Adds image specified by the pathname as watermark.
// The watermark is center-aligned vertically and
// middle-aligned horizontally. This position is made
// with reference to page boundaries.
document.addWatermarkImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
false, // applyPageMargins set to false
45,
true,
"2");
// Same as above statement (with applyPageMargins set to
// false) but without explicitly specifying that
// the watermark's position is made with reference to page
// boundaries rather than the page margins.
document.addWatermarkImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
45,
true,
"3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, an image specified by its pathname
// is added as a watermark to the document in various ways.
public void addWatermarkImage_PdfImage_int_boolean_double_boolean_String_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addWatermarkImage_PdfImage_int_"
+ "boolean_double_boolean_String_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates a PdfImage object from an image file inside
// folder "InputDocs" located in current directory
PdfImage image = PdfImage.create(
".\\InputDocs\\banner_img_468_60.jpg");
// Adds image as watermark. Image is center-aligned
// vertically and middle-aligned horizontally. This position
// is made with reference to page margins.
document.addWatermarkImage(
image,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
true, // applyPageMargins set to true
45,
true,
"1");
// Adds image as watermark. Image is center-aligned
// vertically and middle-aligned horizontally. This position
// is made with reference to page boundaries.
document.addWatermarkImage(
image,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
false, // applyPageMargins set to false
45,
true,
"2");
// Same as above statement (with applyPageMargins set to
// false) but without explicitly specifying that
// the watermark's position is made with reference to page
// boundaries rather than the page margins.
document.addWatermarkImage(
image,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
45,
true,
"3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, some text is added as watermak to the
// document in various ways.
public void addToFiltersList_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(new File(
"PdfDocument_addToFiltersList_"
+ "example.pdf"));
PdfDocument document = new PdfDocument(writer);
// Writes a line to the new PdfDocument object
document.writeText("Four score and seven years ago...");
// Adds filter to encode in ASCII base-85 representation
document.addToFiltersList(PdfFilter.ASCII85);
// Adds filter to encode in byte-oriented run-length encoding
// algorithm
document.addToFiltersList(PdfFilter.RUNLENGTH);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment demonstrates the use of an overloaded
// addBookmark method.
public void addBookmark_String_PdfBookmark_String_int_boolean_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addBookmark_String_PdfBookmark_String_"
+ "int_boolean_example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
document.writeText("This is PdfDocument_addBookmark_"
+ "String_PdfBookmark_String_"
+ "int_boolean_example.pdf",
100,
200,
"1-3");
// Create a PDF file with 13 pages
CreateSamplePDF("test.pdf", 13);
// Creates a new PdfBookmark object and assigns it to the
// root of the document outline
PdfBookmark bmRoot = document.getBookmarkRoot();
// Creates a bookmark that opens page 5 of test.pdf in a new
// window
document.addBookmark("Open \"test.pdf\" in new window",
bmRoot,
"test.pdf",
5,
true);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment demonstrates the use of an overloaded
// addBookmark() method
public void addBookmark_String_PdfBookmark_String_int_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addBookmark_String_PdfBookmark_String_"
+ "int_example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates a new PdfBookmark object and assigns it to the
// root of the document outline
PdfBookmark bmRoot = document.getBookmarkRoot();
// Creates a bookmark that resolves the Gnostice website URL
document.addBookmark("Go to Gnostice website",
bmRoot,
"http://www.gnostice.com",
PdfAction.URI);
// Creates a bookmark that executes a Javascript script
document.addBookmark(
"Display Javascript \"Hello, World! \" message",
bmRoot,
"app.alert(\"Hello, World!\")",
PdfAction.JAVASCRIPT);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, bookmarks are added in various ways.
public void addBookmark_String_PdfBookmark_int_double_int_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addBookmark_String_"
+ "PdfBookmark_int_double_int_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the second page. ");
page3.writeText("This is the third page. ");
page4.writeText("This is the fourth page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
document.add(page4);
// Assigns document's bookmark root to new PdfBookmark object
PdfBookmark bm = document.getBookmarkRoot();
// Adds a bookmark that leads to page 1. The page is to be
// zoomed to tightly fit its entire width inside a window.
// The vertical coordinate (100) of the destination is
// positioned on the top edge of the window.
document.addBookmark("Link to page 1 with FITH top 100",
bm,
1,
100,
PdfBookmark.FITH);
// Sets page 2 to be zoomed to tightly fit entire width of
// its bounding box inside a window. The vertical
// coordinate (100) of the destination is positioned on the
// top edge of the window.
document.addBookmark("Link to page 2 with FITBH top 100",
bm,
2,
100,
PdfBookmark.FITBH);
// Sets page 3 to be zoomed to tightly fit entire height of
// its bounding box inside a window. The horizontal
// coordinate (100) of the destination is positioned on the
// left edge of the window.
document.addBookmark("Link to page 3 with FITBV left 100",
bm,
3,
100,
PdfBookmark.FITBV);
// Sets page 3 to be zoomed to tightly fit its entire height
// inside a window. The horizontal coordinate (100) of the
// destination is positioned on the left edge of the window.
document.addBookmark("Link to page 4 with FITV left 100",
bm,
3,
100,
PdfBookmark.FITV);
// Adds a bookmark that leads to page 4 and displays
// the page in such a way that the entire height and
// width of its bounding box is displayed tightly
// inside a window
document.addBookmark("Link to page 4 with FITB",
bm,
4,
PdfBookmark.FITB);
// Adds bookmark that leads to position 400,200 on page 3
// with page zoom factor set to 150%
document.addBookmark("Link to position (400,200) "
+ "on page 3 with zoom 150",
bm, // parent
3, // pageNo
400, 200, // left, top
150); // zoom
// Adds a bookmark that leads to a rectangle on page 1
document.addBookmark("Link to Pdf Rectangle",
bm,
1,
new PdfRect(100, 100, 200, 200));
// Adds a bookmark that leads to a rectangular area on page 3
document.addBookmark("Link to rectangular area",
bm,
2,
new Rectangle(100, 100, 100, 100));
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Some text is then added as a watermark
// added to the pages.
public void addWatermarkText_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addWatermarkText_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates an Arial font size 22
PdfFont fontArial = PdfFont.create("Arial", PdfFont.BOLD
| PdfFont.UNDERLINE | PdfFont.ITALIC
| PdfFont.STROKE_AND_FILL, 26, PdfEncodings.WINANSI);
// Adds text as watermark, which is vertically center-aligned
// and horizontally middle-aligned. This position is made
// with reference to page margins.
document.addWatermarkText(
"CONFIDENTIAL: For Eyes Only",
fontArial,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
true, // applyPageMargins set to true
45,
true,
"1");
// Adds text as watermark, which is vertically center-aligned
// and horizontally middle-aligned. This position is made
// with reference to page boundaries.
document.addWatermarkText(
"CONFIDENTIAL: For Eyes Only",
fontArial,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
false, // applyPageMargins set to true
45,
true,
"2");
// Same as above statement (with applyPageMargins set to
// false) but without explicitly specifying that
// the watermark's position is made with reference to page
// boundaries rather than the page margins.
document.addWatermarkText(
"CONFIDENTIAL: For Eyes Only",
fontArial,
PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE,
45,
true,
"3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, some text is added to header of the
// pages but with different settings for the underlay option.
public void addHeaderText_String_PdfFont_int_boolean_String_example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addHeaderText_String_"
+ "PdfFont_int_boolean_String_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Adds an image to the header of pages 1 to 3 with
// underlay set as on
document.addHeaderImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
true,
"1-3");
// Creates an Arial font size 22
PdfFont fontArial = PdfFont.create(
"Arial",
PdfFont.BOLD | PdfFont.UNDERLINE | PdfFont.ITALIC
| PdfFont.STROKE_AND_FILL,
22,
PdfEncodings.WINANSI);
// Adds header text to page 1 with underlay option set to
// false
document.addHeaderText(
"Gnostice Information Technologies Private "
+ "Limited",
fontArial,
PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
false,
"1");
// Adds header text to pages 2 and 3 with underlay option set
// to true
document.addHeaderText(
"Gnostice Information Technologies Private"
+ "Limited",
fontArial, PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
true,
"2-3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, some text is added to footer of the
// pages but with different settings for the underlay option.
public void addFooterText_String_PdfFont_int_boolean_String_example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addFooterText_String_"
+ "PdfFont_int_boolean_String_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Adds an image to the footer of pages 1 to 3 with underlay
// set as on
document.addFooterImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
true,
"1-3");
// Creates an Arial font size 22
PdfFont fontArial = PdfFont.create(
"Arial",
PdfFont.BOLD | PdfFont.UNDERLINE | PdfFont.ITALIC
| PdfFont.STROKE_AND_FILL,
22,
PdfEncodings.WINANSI);
// Adds footer text to page 1 with underlay option set to
// false
document.addFooterText(
"Gnostice Information Technologies Private "
+ "Limited",
fontArial,
PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
false,
"1");
// Adds footer text to pages 2 and 3 with underlay option set
// to true
document.addFooterText(
"Gnostice Information Technologies Private"
+ "Limited",
fontArial, PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
true,
"2-3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, two images are added using their
// pathnames to the header of the documented with underlay on and
// off.
public void addHeaderImage_String_int_boolean_String_example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addHeaderImage_String_"
+ "int_boolean_String_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Adds first image with its pathname to pages 1 to 3 with
// underlay set as off
document.addHeaderImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
false,
"1-3");
// Adds second image with its pathname to pages 1 to 3 with
// underlay set as on
document.addHeaderImage(
".\\InputDocs\\banner_img_160_50.jpg",
PdfPage.HP_LEFT | PdfPage.VP_TOP,
true,
"1-3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, two images are added using their
// pathnames to the footer of the documented with underlay on and
// off.
public void addFooterImage_String_int_boolean_String_example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addFooterImage_String_"
+ "int_boolean_String_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Adds first PdfImage object to pages 1 to 3 with underlay
// set to true
document.addFooterImage(
".\\InputDocs\\banner_img_468_60.jpg",
PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
true,
"1-3");
// Adds second PdfImage object to pages 1 to 3 with underlay
// set to false
document.addFooterImage(
".\\InputDocs\\banner_img_160_50.jpg",
PdfPage.HP_LEFT | PdfPage.VP_TOP,
false,
"1-3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, two PdfImage objects are added to
// the header of the documented with underlay on and off.
public void addHeaderImage_PdfImage_int_boolean_String_example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addHeaderImage_PdfImage_"
+ "int_boolean_String_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2, 1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates PdfImage objects from image files inside
// folder "InputDocs" located in current directory
PdfImage image1 = PdfImage.create(
".\\InputDocs\\banner_img_468_60.jpg");
PdfImage image2 = PdfImage.create(
".\\InputDocs\\banner_img_160_50.jpg");
// Adds first PdfImage object to pages 1 to 3 with underlay
// set to true
document.addHeaderImage(image1,
PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
true,
"1-3");
// Adds second PdfImage object to pages 1 to 3 with underlay
// set to false
document.addHeaderImage(image2,
PdfPage.HP_LEFT | PdfPage.VP_TOP,
false,
"1-3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds three PdfPage objects to a PdfDocument
// object. A line of text is written after every PdfPage is added
// to the PdfDocument. Next, two PdfImage objects are added to
// footer of the documented with underlay on and off in either
// case.
public void addFooterImage_PdfImage_int_boolean_String_example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addFooterImage_PdfImage_"
+ "int_boolean_String_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates A4-size Pdfpage objects
PdfPage page1 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2,
1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page2 = new PdfPage(8.2677,
11.6929,
1, 1,
1.2, 1.2,
1.2, 1.2,
PdfMeasurement.MU_INCHES);
PdfPage page3 = new PdfPage(8.2677, 11.6929,
1, 1,
1.2, 1.2,
1.2, 1.2,
PdfMeasurement.MU_INCHES);
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates PdfImage objects from image files inside
// folder "InputDocs" located in current directory
PdfImage image1 = PdfImage.create(
".\\InputDocs\\banner_img_468_60.jpg");
PdfImage image2 = PdfImage.create(
".\\InputDocs\\banner_img_160_50.jpg");
// Adds first PdfImage object to pages 1 to 3 with underlay
// set as on
document.addFooterImage(image1,
PdfPage.HP_LEFT | PdfPage.VP_CENTRE,
true,
"1-3");
// Adds second PdfImage object to pages 1 to 3 with underlay
// set as off
document.addFooterImage(image2,
PdfPage.HP_LEFT | PdfPage.VP_TOP,
false,
"1-3");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds actions to a document that are executed
// the moment the document is opened
public void addAction_int_String_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addAction_int_String_Example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates a default page and writes some text
document.writeText(
"This page has a Javascript action and a URI action");
// Sets the document to execute a Javascript
// message that displays a 'Hello world' message
document.addAction(PdfAction.JAVASCRIPT,
"app.alert('Hello, world!')");
// Sets the document to open a URL in a browser
document.addAction(PdfAction.URI,
"http://www.gnostice.com/");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment illustrates how to add a named
// action to a PDF document
public void addAction_int_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"pdfdocument_addaction_"
+ "int_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Add a named action to the document
document.addAction(PdfAction.NAMED_LASTPAGE);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment adds pages to a document using several
// overloaded methods. It then specifies various settings
// related to the document.
public void add_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"pdfdocument_add_"
+ "example.pdf");
PdfDocument document = new PdfDocument(writer);
// Creates the PdfPage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Writes lines of text to the PdfPage objects
page1.writeText("This is page #1. ");
page2.writeText("This is page #2. ");
page3.writeText("This is page #3. ");
page4.writeText("This is page #4. ");
// Adds a PdfPage object to a Pdfdocument
document.add(page1);
// Writes a line of text to the current page of the
// PdfDocument
document.writeText("This text was written to document"
+ " after adding page #1. ");
document.add(page2);
document.writeText("This text was written to document"
+ " after adding page #2. ");
// Adds PdfPage object to PdfDocument
// Also sets this PdfPage as the PdfDocument's current page
// (instead of the first PdfPage)
document.add(page3, true);
document.writeText("This text was written to document"
+ " after adding page #3. ");
document.add(page4);
document.writeText("This text was written to document"
+ "after adding page #4. ");
// Checks and sets page layout
if (document.getPageLayout() == PdfPageLayout.SINGLE_PAGE) {
document.setPageLayout(PdfPageLayout.SINGLE_PAGE);
}
// Checks and sets PDF version
if (document.getVersion().equals(PdfDocument.VERSION_1_6)
!= true )
{
document.setVersion(PdfDocument.VERSION_1_6);
}
// Sets the title entry in the document information
// dictionary
document.setTitle("PDFOne Method Demonstration");
// Sets the author entry in the document information
// dictionary
document.setAuthor("Gnostice Team");
// Sets the subject entry in the document information
// dictionary
document.setSubject(
"Method add(PdfPage) in PdfDocument.java");
// Sets the keywords entry in the document information
// dictionary
document.setKeywords(
"java, PDF, document, add, page, PDFOne, PdfPage, "
+ "PdfDocument");
// Sets the producer entry in the document information
// dictionary
document.setProducer("Gnostice PDFOne Java");
// Displays number of pages in the document
System.out.println(
"Number of pages in this document: "
+ document.getPageCount());
// Ensures the file is to be executed after it is
// written to
if (document.isOpenAfterSave() != true) {
document.setOpenAfterSave(true);
}
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment creates a PdfDocument object with a
// PdfReader object.
public void PdfDocument_PdfReader_Example() throws IOException,
PdfException
{
// Calls another example, which creates a file, namely,
// PdfDocument_PdfDocument_PdfWriter_Example.pdf.
// (used as input in the next statement)
PdfDocument_PdfWriter_Example();
// Creates a PdfReader object with specified input and
// output filenames
PdfReader reader = PdfReader.fileReader(
"PdfDocument_PdfDocument_PdfWriter_Example.pdf", // input
"PdfDocument_PdfDocument_PdfReader_Example.pdf"); // output
// Constructs a PdfDocument object with the PdfReader object
PdfDocument document = new PdfDocument(reader);
// Creates a default page in the document.
// Writes text at position (100, 100) on the page.
document.writeText(
"This file, PdfDocument_PdfDocument_PdfWriter_Example.pdf,"
+ "was created after reading from PdfDocument_PdfDocument_"
+ "PdfReader_Example.pdf",
100, 100);
// Sets the document to be opened after it is saved
document.setOpenAfterSave(true);
// Writes the document object to file
document.write();
// Closes all I/O streams associated with this reader object
reader.dispose();
}
// This code segment creates a new PdfDocument object with a
// PdfWriter object.
public void PdfDocument_PdfWriter_Example() throws IOException,
PdfException
{
// Creates a PdfWriter object with a filename
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_PdfDocument_"
+ "PdfWriter_Example.pdf");
// Constructs a PdfDocument object with the PdfWriter object
PdfDocument document = new PdfDocument(writer);
// Writes text on the new PdfDocument object
document.writeText("Four score and seven years ago...");
// Sets the document to be displayed after it is saved
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 with several Bézier curves using
// overloaded methods
public void drawBezierCurve_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_drawBezier_example.pdf");
PdfDocument document = new PdfDocument(writer);
// Sets pen and brush colors
document.setPenColor(Color.RED);
document.setBrushColor(Color.GREEN);
// Draws Bézier curves each with one control point
// on page 1
document.drawBezierCurve(150, 100, // starting point
200, 50, // control point
250, 150, // end point
false, // fill setting
true); // stroke setting
document.drawBezierCurve(250, 100,
300, 50,
350, 150,
true,
true);
document.drawBezierCurve(350, 100,
400, 50,
450, 150,
true,
false);
// Writes text on page 1 identifying settings above curves
document.writeText("isFill = false", 150, 30);
document.writeText("isStroke = true", 150, 40);
document.writeText("isFill = true", 250, 30);
document.writeText("isStroke = true", 250, 40);
document.writeText("isFill = true", 350, 30);
document.writeText("isStroke = false", 350, 40);
document.writeText(". (150, 100)", 150, 100);
document.writeText(". (200, 50)", 200, 50);
document.writeText(". (250, 150)", 250, 150);
// Draws a Bézier curve with two control points
// on page 1
document.drawBezierCurve(150, 400, // starting point
200, 250, // first control point
250, 330, // second control point
300, 550, // end point
false, // fill setting
true); // stroke setting
// Writes text on page 1 identifying the above curve
document.writeText(". (150, 400)", 150, 400);
document.writeText(". (200, 250)", 200, 250);
document.writeText(". (250, 330)",250, 330);
document.writeText(". (300, 550)", 300, 550);
document.writeText("isFill = false", 150, 280);
document.writeText("isStroke = true", 150, 290);
// Creates page 2 and 3
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Adds pages 2 and 3 to document
document.add(page2);
document.add(page3);
// Draws Bézier curves each with single control points
// on pages in page range 2 to 3
document.drawBezierCurve(150, 100, // starting point
200, 50, // control point
250, 150, // end point
true, // fill setting
false, // stroke setting
"2-3"); // page range
// Writes text identify the above curves
document.writeText(". (150, 100)", 150, 100, "2-3");
document.writeText(". (200, 50)", 200, 50, "2-3");
document.writeText(". (250, 150)", 250, 150, "2-3");
document.writeText("isFill = true", 150, 20, "2-3");
document.writeText("isStroke = false", 150, 30, "2-3");
document.writeText("pageRange = \"2-3\"", 150, 40, "2-3");
// Draws Bézier curves each with two control points on
// pages in page range 2 to 3
document.drawBezierCurve(150, 400, // starting point
200, 250, // first control point
250, 330, // second control point
300, 550, // end point
true, // fill setting
true, // stroke setting
"2-3"); // page range
// Writes text identifying above curves
document.writeText(". (150, 400)", 150, 400, "2-3");
document.writeText(". (200, 250)", 200, 250, "2-3");
document.writeText(". (250, 330)", 250, 330, "2-3");
document.writeText(". (250, 230)", 300, 550, "2-3");
document.writeText("isFill = true", 150, 280, "2-3");
document.writeText("isStroke = true", 150, 290, "2-3");
document.writeText("pageRange = \"2-3\"", 150, 300, "2-3");
// 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 the use of an overloaded
// addBookmark() method.
public void addBookmark_String_PdfBookmark_int_double_double_double_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"addBookmark_String_PdfBookmark_"
+ "int_double_double_double_Example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Writes identifying text to the PdfPage objects
page1.writeText("This is page 1. ");
page2.writeText("This is page 2. ");
page3.writeText("This is page 3. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates a new PdfBookmark object and assigns it to the
// root of the document outline
PdfBookmark bm = document.getBookmarkRoot();
// Adds a bookmark that displays position (100, 200) on
// page 2 with a page magnification of 150%
bm = document.addBookmark(
"Page 2, position (100, 200), zoom 150%", // title
bm, // title
2, // pageNo
100, 200, // left, top
150); // zoom
document.writeText(".(100, 200)", 100, 200, "2");
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment demonstrates the use of an overloaded
// addBookmark() method.
public void addBookmark_String_PdfBookmark_int_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addBookmark_String_"
+ "PdfBookmark_int_example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Writes identifying text to the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates a new PdfBookmark object and assigns it to the
// root of the document outline
PdfBookmark bm = document.getBookmarkRoot();
// Creates a branch of bookmarks, one under the other
bm = document.addBookmark("Page 1", bm, 1);
bm = document.addBookmark("Page 2", bm, 2);
bm = document.addBookmark("Page 3", bm, 3);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment demonstrates the use of an overloaded
// addBookmark() method.
public void addBookmark_int_String_PdfBookmark_Example()
throws PdfException, IOException
{
PdfWriter writer = PdfWriter.fileWriter(
"pdfdocument_addBookmark_int_"
+ "String_PdfBookmark_example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
// Writes text identifying the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the middle page. ");
page3.writeText("This is the last page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
// Creates a new PdfBookmark object and assigns it to the
// root of the document outline
PdfBookmark bm = document.getBookmarkRoot();
// Adds a new bookmark. When selected, this bookmark displays
// the last page.
bm = document.addBookmark(PdfAction.NAMED_LASTPAGE,
"Go to last page",
bm);
// Adds a second bookmark under the first bookmark. When
// selected, this bookmark displays the last page.
bm = document.addBookmark(PdfAction.NAMED_FIRSTPAGE,
"Go to first page",
bm);
// Adds a third bookmark under the second bookmark. When
// selected, this bookmark displays the text-search pane.
bm = document.addBookmark(PdfAction.NAMED_SEARCH,
"Display text-search pane",
bm);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes all I/O streams associated with this writer object
writer.dispose();
}
// This code segment demonstrates the use of an overloaded
// addBookmark() method.
public void addBookmark_String_PdfBookmark_int_int_Example()
throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
"PdfDocument_addBookmark_"
+ "String_PdfBookmark_int_int_Example.pdf");
PdfDocument document = new PdfDocument(writer);
document.setPageMode(PdfPageMode.USEOUTLINES);
// Creates Pdfpage objects
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
PdfPage page3 = new PdfPage();
PdfPage page4 = new PdfPage();
// Writes text identifying the PdfPage objects
page1.writeText("This is first page. ");
page2.writeText("This is the second page. ");
page3.writeText("This is the third page. ");
page4.writeText("This is the fourth page. ");
// Adds the PdfPage objects to the PdfDocument
document.add(page1);
document.add(page2);
document.add(page3);
document.add(page4);
// Creates a new PdfBookmark object and assigns it to the
// root of the document outline
PdfBookmark bm = document.getBookmarkRoot();
// Adds a bookmark that leads to page 4 and displays
// the page in such a way that the entire height and
// width of the bounding box of the page is displayed
// tightly inside the window of the viewer application
document.addBookmark("Link to page 4 with FITB",
bm,
4,
PdfBookmark.FITB);
// Sets the file to be opened after it is written to
document.setOpenAfterSave(true);
// Writes the PdfDocument to file
document.write();
// Closes I/O streams associated with this writer object
writer.dispose();
}
// This code segment creates a sample PDF file.
public void CreateSamplePDF(String sampleFilePathname,
int noOfPages) throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(sampleFilePathname));
PdfDocument document = new PdfDocument(writer);
PdfPage page;
// Adds pages and bookmarks to the PdfDocument object
// created above
for (int i = 1; i <= noOfPages; i++)
{
page = new PdfPage();
page.writeText("This is page number "
+ i
+ " of \""
+ sampleFilePathname
+ "\"");
document.add(page);
document.addBookmark("Page " + (i),
document.getBookmarkRoot(),
i);
}
document.setOpenAfterSave(false);
document.write();
writer.dispose();
}
}