Create Named Destination in Existing PDF Documents using Java

We at Aspose are pleased to announce a new version of Aspose.PDF for Java. As per the regular monthly release process, Aspose.PDF for Java 18.2 has been released and available for download on our Maven Repository. Aspose.PDF for Java 18.2 is a balancing release in which we have ported features from Aspose.PDF for .NET 18.2 API, in order to offer the same set of features for both .NET and Java platforms. In order to have a look at what is new in this release of the API, we recommend you to visit release notes page of Aspose.PDF for Java 18.2. However, the following are some features that we would like to highlight.

Create Named Destination in Existing PDF Documents using Java

With Aspose.PDF for Java 18.2, you can now create named destinations programmatically in your Java Application. We have implemented getNamedDestinations() method for com.aspose.pdf.Document Class, which allows you to manipulate Named Destinations in existing PDF documents. NamedDestinationCollection has the following properties:

  • public IAppointment get_Item(String name); // Gets destination by its name
  • public void set_Item(String name, IAppointment value); // Sets destination by its name
  • public int getCount(); // Returns count of the destinations.
  • public void remove(String name); // Removes destination by its name.
  • public void add(String name, IAppointment appointment); // Adds new named destination.

The following code snippet demonstrates the usage of getNamedDestinations() method:

Document pdf = new Document();
// Create document with 100 pages
for (int i = 1; i <= 100; i++)
{
 Page page = pdf.getPages().add();
 page.addStamp(new com.aspose.pdf.TextStamp("Page " + i));
 // Named Destinations for every page
 pdf.getNamedDestinations().add("Page" + i, <br>new com.aspose.pdf.XYZExplicitDestination(i, 0, 600, 0.5));
}
for (int i = 1; i <= 100; i++)
{<br> // Create outlines (two outlines for every page)
 com.aspose.pdf.OutlineItemCollection item1 = new <br>com.aspose.pdf.OutlineItemCollection(pdf.getOutlines());
 item1.setDestination (new <br>com.aspose.pdf.NamedDestination(pdf, "Page" + i));
 item1.setTitle ("Page  " + i + "(1)");
 pdf.getOutlines().add(item1);

 com.aspose.pdf.OutlineItemCollection item2 = <br>new com.aspose.pdf.OutlineItemCollection(pdf.getOutlines());
 item2.setDestination (new <br>com.aspose.pdf.NamedDestination(pdf, "Page" + i));
 item2.setTitle ("Page  " + i + "(2)");
 pdf.getOutlines().add(item2);
}
// Update on of the named destinations
pdf.getNamedDestinations().set_Item("Page50", <br>new com.aspose.pdf.XYZExplicitDestination(50, 0, 100, 2));
pdf.save(myDir+"result.pdf");

Miscellaneous Fixes

As it is always recommended to use the latest release of our API’s as they include the latest features/improvements and fixes related to issues reported in earlier released versions. Therefore, please download the latest release of Aspose.PDF for Java 18.2.