import java.io.File; import java.io.IOException; import com.gnostice.pdfone.PDFOne; import com.gnostice.pdfone.PdfAction; import com.gnostice.pdfone.PdfBookmark; import com.gnostice.pdfone.PdfDocument; import com.gnostice.pdfone.PdfException; import com.gnostice.pdfone.PdfMeasurement; import com.gnostice.pdfone.PdfPage; import com.gnostice.pdfone.PdfPageMode; import com.gnostice.pdfone.PdfRect; import com.gnostice.pdfone.PdfWriter; public class PdfBookmark_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 { PdfBookmark_Examples obj = new PdfBookmark_Examples(); // To try other examples, add the obj.<example_method> // accordingly. For example, try: // obj.addNext_int_String_Example(); obj.addNext_String_int_double_int_Example(); } // This code segment demonstrates the use of an overloaded // addPrevious() method. // This code segment requires pathnames of an application and // and an ordinary file. // To use pathnames supplied as command-line arguments, use // a statement in main() similar to: // obj.addPrevious_String_String_boolean_Example(args[0], args[1]); public void addPrevious_String_String_boolean_Example(String appPathname, String filePathname) throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( "PdfBookmark_addPrevious_String_" + "String_boolean_example.pdf"); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.writeText("This is page number " + i); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a new bookmark immediately after the above bookmark. // When selected, this new bookmark launches the specified // application. bm = bm.addPrevious("Launch \"" + appPathname + "\"", appPathname, false); // Adds a new bookmark immediately after the above bookmark. // When selected, this new bookmark prints the specified // file. bm.addPrevious("Print \"" + filePathname + "\"", filePathname, true); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addPrevious() method. public void addPrevious_String_int_PdfRect_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( "PdfBookmark_addPrevious_String_" + "int_PdfRect_example.pdf"); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.writeText("This is page number " + i); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Creates a rectangle PdfRect rectangle = new PdfRect(100, 150, 200, 250); // Draws the rectangle on page 1 document.drawRect(rectangle, "1"); // Adds a new bookmark immediately before the above bookmark. // When selected, it leads to the above rectangle on page 1. bm.addPrevious("Link to rectangle on Page 1", 1, rectangle); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addPrevious() method. public void addPrevious_String_int_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( "PdfBookmark_addPrevious_String_" + "int_int_example.pdf"); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.writeText("This is page number " + i); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a new bookmark immediately before the first bookmark. // When selected, this new bookmark leads to page 15. Page 15 // is displayed with entire height and width of its bounding // box tightly fit inside the window. bm.addPrevious("Link to Page 15 with FITB", 15, PdfBookmark.FITB); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addPrevious() method. public void addPrevious_String_int_double_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addPrevious_String_" + "int_double_int_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Creates a new bookmark immediately before the above // bookmark. When selected, this new bookmark will lead to // page 14. The page will be zoomed to tightly fit its entire // width inside the window. The top right corner of the // window will be positioned at 100 points down the top // edge of the page. bm = bm.addPrevious("Link to page 14 with FITH top 100", 14, 100, PdfBookmark.FITH); // Creates a new bookmark immediately before the above // bookmark. When selected, this new bookmark will lead to // page 14. The page will be zoomed to tightly fit its // entire width of its bounding box inside the window. The // top right corner of the window will be positioned at 100 // points down the top edge of the page. bm = bm.addPrevious("Link to page 14 with FITBH top 100", 14, 100, PdfBookmark.FITBH); // Creates a new bookmark immediately before the above // bookmark. When selected, this new bookmark will lead to // page 14. The page will be zoomed to tightly fit entire // height of its bounding box inside the window. The left // edge of the window will be positioned 100 points off the // left edge of the page. bm = bm.addPrevious("Link to page 14 with FITBV left 100", 14, 100, PdfBookmark.FITBV); // Creates a new bookmark immediately before the above // bookmark. When selected, this new bookmark will lead to // page 14. The page will be zoomed to tightly fit entire // height of the page inside the window. The left edge of the // window will be positioned 100 points off the left edge of // the page. bm.addPrevious("Link to page 14 with FITV left 100", 14, 100, PdfBookmark.FITV); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addPrevious() method. public void addPrevious_String_int_double_double_double_double_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addPrevious_" + "String_int_double_double_" + "double_double_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a new bookmark immediately before the above bookmark. // When selected, this new bookmark leads to a rectangular // area on page 13. bm.addPrevious( "Jump to rectangular area on page 13", 13, // pageNo 100, 100, 400, 400); // left, bottom, right, top // Identifies the rectangular area on page 13 document.drawRect(100, 100, 400, 400, "13"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addPrevious() method. public void addPrevious_String_int_double_double_double_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addPrevious_String_int_" + "double_double_double_" + "example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a bookmark immediately before the above bookmark. // When selected, this new bookmark leads to // position (100, 200) on page 12 with the page zoom set // at 125%. bm.addPrevious( "Jump to position (100, 200) on page 12 with zoom 125%", 12, // pageNo 100, 200, // left, top 125); // zoom // Identifies position (100, 200) on page 12 document.writeText(". (100, 200)", 100, 200, "12"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addPrevious() method. public void addPrevious_String_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_" + "addPrevious_String_int_" + "example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a new bookmark immediately before the above first // bookmark. When selected, this new bookmark leads to // page 15. bm.addPrevious("Link to page 15", 15); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addPrevious() method. public void addPrevious_int_String_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addPrevious_int_" + "String_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a bookmark immediately before the first bookmark. When // selected, it performs the named action. bm.addPrevious(PdfAction.NAMED_NEXTPAGE, "Move to the next page"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addNext() method. // This code segment requires pathnames of an application and // and an ordinary file. // To use pathnames supplied as command-line arguments, use // a statement in main() similar to: // obj.addNext_String_String_boolean_Example(args[0], args[1]); public void addNext_String_String_boolean_Example(String appPathname, String filePathname) throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( "PdfBookmark_addNext_String_" + "String_boolean_example.pdf"); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.writeText("This is page number " + i); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a new bookmark immediately after the above bookmark. // When selected, this new bookmark launches the specified // application. bm = bm.addNext("Launch \"" + appPathname + "\"", appPathname, false); // Adds a new bookmark immediately after the above bookmark. // When selected, this new bookmark prints the specified // file. bm.addNext("Print \"" + filePathname + "\"", filePathname, true); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addNext() method. public void addNext_String_int_PdfRect_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( "PdfBookmark_addNext_String_" + "int_PdfRect_example.pdf"); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.writeText("This is page number " + i); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Creates a rectangle PdfRect rectangle = new PdfRect(100, 150, 200, 250); // Draws the rectangle on page 1 document.drawRect(rectangle, "1"); // Adds a new bookmark immediately after the above bookmark. // When selected, it leads to the above rectangle on page 1. bm.addNext("Link to rectangle on Page 1", 1, rectangle); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addNext() method. public void addNext_String_int_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( "PdfBookmark_addNext_String_" + "int_int_example.pdf"); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.writeText("This is page number " + i); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a new bookmark immediately after the first bookmark. // When selected, this new bookmark leads to page 14. Page 14 // is displayed with entire height and width of its bounding // box tightly fit inside the window. bm.addNext("Link to Page 14 with FITB", 14, PdfBookmark.FITB); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addNext() method. public void addNext_String_int_double_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addNext_String_" + "int_double_int_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Creates a new bookmark immediately after the above // bookmark. When selected, this new bookmark will lead to // page 14. The page will be zoomed to tightly fit its entire // width inside the window. The top right corner of the // window will be positioned at 100 points down the top // edge of the page. bm = bm.addNext("Link to page 14 with FITH top 100", 14, 100, PdfBookmark.FITH); // Creates a new bookmark immediately after the above // bookmark. When selected, this new bookmark will lead to // page 14. The page will be zoomed to tightly fit its // entire width of its bounding box inside the window. The // top right corner of the window will be positioned at 100 // points down the top edge of the page. bm = bm.addNext("Link to page 14 with FITBH top 100", 14, 100, PdfBookmark.FITBH); // Creates a new bookmark immediately after the above // bookmark. When selected, this new bookmark will lead to // page 14. The page will be zoomed to tightly fit entire // height of its bounding box inside the window. The left // edge of the window will be positioned 100 points off the // left edge of the page. bm = bm.addNext("Link to page 14 with FITBV left 100", 14, 100, PdfBookmark.FITBV); // Creates a new bookmark immediately after the above // bookmark. When selected, this new bookmark will lead to // page 14. The page will be zoomed to tightly fit entire // height of the page inside the window. The left edge of the // window will be positioned 100 points off the left edge of // the page. bm.addNext("Link to page 14 with FITV left 100", 14, 100, PdfBookmark.FITV); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addNext() method. public void addNext_String_int_double_double_double_double_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addNext_String_" + "int_double_double_double_" + "double_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Creates a new bookmark immediately after the above // bookmark. When selected, this new bookmark leads to a // rectangular area on page 13. bm.addNext( "Jump to rectangular area on page 13", 13, // pageNo 100, 100, 400, 400); // left, bottom, right, top // Identifies the rectangular area on page 13 document.drawRect(100, 100, 400, 400, "13"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addNext() method. public void addNext_String_int_double_double_double_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addNext_String_int_" + "double_double_double_" + "example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a new bookmark that leads to position (100, 200) on // page 12 with page zoom set at 125% bm.addNext( "Jump to position (100, 200) on page 12 with zoom 125%", 12, // pageNo 100, 200, // left, top 125); // zoom // Identifies position (100, 200) on page 12 document.writeText(". (100, 200)", 100, 200, "12"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addNext() method. public void addNext_String_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_" + "addNext_String_int_" + "example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds sample pages and bookmarks to the document for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a new bookmark after the above bookmark. When // selected, it leads to page 15. bm.addNext("Jump to page 15", 15); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment demonstrates the use of an overloaded // addNext() method. public void addNext_int_String_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addNext_int_" + "String_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Creates a new bookmark immediately after the above // bookmark. A named action is added to this new bookmark. bm.addNext(PdfAction.NAMED_LASTPAGE, "Jump to last page"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment creates a bookmark and adds a URI // action to it. public void addActionURI_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addActionURI_" + "example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a URI action to the first bookmark. Here, the action // can make the viewer to resolve the URI of the Gnostice // home page. bm.addActionURI("http://www.gnostice.com/"); bm.setTitle("Go to page 1 and display Gnostice home " + "page in a browser"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a remote go-to action to a bookmark. public void addActionRemoteGoTo_Example() throws IOException, PdfException { // Creates a sample PDF file with 20 pages CreateSamplePDF("test.pdf", 20); PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addActionRemote" + "GoTo_example.pdf")); PdfDocument document = new PdfDocument(writer); PdfPage page; Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a remote go-to action to the first bookmark. Here, // the action can make the viewer to open page 8 of the // sample PDF file created above in a new window. bm.addActionRemoteGoTo("test.pdf", PdfBookmark.getRemoteGoToInstance(8), true); bm.setTitle("Open page 8 of test.pdf in a new window"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a named action to a few bookmarks. public void addActionNamed_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File("pdfbookmark_addAction" + "Named_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getFirstBookmark(); // Adds named actions to the bookmarks bm.addActionNamed(PdfAction.NAMED_LASTPAGE); bm.setTitle("Go to last page"); bm = bm.getNext(); bm.addActionNamed(PdfAction.NAMED_FIRSTPAGE); bm.setTitle("Go to first page"); bm = bm.getNext(); bm.addActionNamed(PdfAction.NAMED_NEXTPAGE); bm.setTitle("Go to next page"); bm = bm.getNext(); bm.addActionNamed(PdfAction.NAMED_PREVPAGE); bm.setTitle("Go to previous page"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a launch action to a bookmark. public void addActionLaunch_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File("pdfbookmark_addActionLaunch_example.pdf") ); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a launch action to the first bookmark bm.addActionLaunch("Document.txt", false); bm.setTitle("Launch file"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a Javascript action to a bookmark. public void addActionJavaScript_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File("pdfbookmark_addactionJavaScript_example.pdf") ); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a Javascript action to this bookmark bm.addActionJavaScript("app.alert(\"Hello, world!\")"); bm.setTitle("Get a \"hello world\" message"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a go-to action to a bookmark using an // overloaded addAction() method. public void addActionGoTo_int_PdfRect_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addactiongoto_" + "int_PdfRect_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Creates a rectangle PdfRect rectangle = new PdfRect(100, 150, 200, 250); // Adds a go-to action to this bookmark. The action can // change the current view of the document to the area // occupied by the above rectangle on page 10. bm.addActionGoto(10, rectangle); bm.setTitle( "Jump to rectangle (100, 150, 200, 250) on page 10"); document.drawRect(rectangle, "10"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a go-to action to a bookmark using an // overloaded addAction() method. public void addActionGoTo_int_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addactiongoto_" + "int_int_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a go-to action to this bookmark. The action can // change the current view of the document to display entire // width and height of page 10's bounding box tightly fit // inside the window. bm.addActionGoto(10,PdfBookmark.FITB); bm.setTitle("Jump to page 10, FITB"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a go-to action to a bookmark using an // overloaded addAction() method. public void addActionGoTo_int_double_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addactiongoto_" + "int_double_int_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(600, 300); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a go-to action to the bookmark. The action can change // the current view to page 10, which will be zoomed to // tightly fit its entire width inside the window. The top // edge of the window will be positioned 100 points down the // top edge of the page. bm.addActionGoto(10, 100, PdfBookmark.FITH); bm.setTitle("Jump to page 10 with FITH top 100"); // Creates a reference to the bookmark next to the current bm = bm.getNext(); // Adds a go-to action to the bookmark. The action can change // the current view to page 10, which will be zoomed to // tightly fit the entire width of its bounding box inside // the window. The top edge of the window will be positioned // 100 points down the top edge of the page. bm.addActionGoto(10, 100, PdfBookmark.FITBH); bm.setTitle("Jump to page 10 with FITBH top 100"); bm = bm.getNext(); // Adds a go-to action to the bookmark. The action can change // the current view to page 10, which will be zoomed to // tightly fit the entire height of its bounding box inside // the window. The left edge of the window will be positioned // 100 points off the left edge of the page. bm.addActionGoto(10, 100, PdfBookmark.FITBV); bm.setTitle("Jump to page 10 with FITBV left 100"); bm = bm.getNext(); // Adds a go-to action to the bookmark. The action change // the current view to page 10, which will be zoomed to // tightly fit its entire height inside the window. The left // edge of the window will be positioned 100 points off the // left edge of the page. bm.addActionGoto(10, 100, PdfBookmark.FITV); bm.setTitle("Jump to page 10 with FITV left 100"); // Identifies the position (100, 100) on page 10 document.writeText(".(100, 100)", 100, 100, "10"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a go-to action to a bookmark using an // overloaded addAction() method. public void addActionGoTo_int_double_double_double_double_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File("pdfbookmark_addactiongoto_" + "int_double_double_double_" + "double_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.writeText("This is page number " + i); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } document.drawRect(100, 100, 300, 250, "1"); // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a go-to action to the first bookmark. The action can // change the current view to a rectangular area on page 1. // The top-left corner of the rectangular area is at // (100, 100). The rectangular area is 300 points wide and // 250 points long. bm.addActionGoto(1, 100, 100, 300, 250); bm.setTitle("Go to rectangular area on page 1"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a go-to action to a bookmark using an // overloaded addAction() method. public void addActionGoTo_int_double_double_double_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addactiongoto_" + "int_double_double_double_" + "example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a go-to action to the first bookmark. The action can // change the current view of the document to position // (200, 100) on page 10 with page zoom set at 215%. bm.addActionGoto(10, 200, 100, 215); bm.setTitle("Jump to page 10 position (200, 100) zoom 215%"); // Identifies the position (200, 100) document.writeText(". (200, 100)", 200, 100, "10"); document.setOpenAfterSave(true); document.write(); writer.dispose(); } // This code segment adds a go-to action to a bookmark using an // overloaded addAction() method. public void addActionGoTo_int_Example() throws IOException, PdfException { PdfWriter writer = PdfWriter.fileWriter( new File( "pdfbookmark_addactiongoto_" + "int_example.pdf")); PdfDocument document = new PdfDocument(writer); document.setPageMode(PdfPageMode.USEOUTLINES); PdfPage page; // Adds pages and bookmarks to the PdfDocument object // created above for (int i = 1; i <= 20; i++) { page = new PdfPage(); page.setMeasurementUnit(PdfMeasurement.MU_INCHES); page.writeText("This is page number " + i, 2, 2); document.add(page); document.addBookmark("Page " + (i), document.getBookmarkRoot(), i); } // Creates a reference to the first bookmark PdfBookmark bm = document.getBookmarkRoot().getFirstChild(); // Adds a go-to action to this bookmark. The action can // changes the current view to page 10. bm.addActionGoto(10); bm.setTitle("Jump to page 10"); document.setOpenAfterSave(true); document.write(); 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(); } }