In my java application, I’ve been trying to create PDF with bookmarks to carry the “InheritZoom” (using XYZExplicitDestination) for compliance reason, but trying many ways, the resulting PDF keep using the “FitH” instead. Please see following for things I’ve tried:
Creating XYZExplicitDestination with zoom level zero and creating action with the string value.
com.aspose.pdf.facades.Bookmark bookmark = new com.aspose.pdf.facades.Bookmark();
XYZExplicitDestination destination = new XYZExplicitDestination(pageNumber,0,0,0);
GoToAction action = new GoToAction(destination);
bookmark.setAction(String.valueOf(action));
None of the above is working as expected in Adobe-Reader.
When we inspect the PDF file with Text Editor (e.g. VSCode), I keep seeing the goto action is using the “FitH”:
To set the “InheritZoom” on bookmarks in your PDF using Aspose.PDF for Java, you need to ensure that you are correctly creating the XYZExplicitDestination and associating it with the bookmark action. The issue you’re facing, where the resulting PDF uses “FitH” instead of “XYZ”, may stem from how the action is being set.
Here’s a correct implementation to achieve the desired bookmark action with “XYZ”:
import com.aspose.pdf.Document;
import com.aspose.pdf.Page;
import com.aspose.pdf.XYZExplicitDestination;
import com.aspose.pdf.GoToAction;
import com.aspose.pdf.facades.Bookmark;
public class BookmarkExample {
public static void main(String[] args) {
// Create a new PDF document
Document pdfDocument = new Document();
Page page = pdfDocument.getPages().add();
// Create a destination with XYZ parameters
int pageNumber = 1; // Page number where the bookmark points
XYZExplicitDestination destination = new XYZExplicitDestination(pageNumber, -1, -1, null); // -1 for inherit zoom
// Create a GoToAction with the destination
GoToAction action = new GoToAction(destination);
// Create a bookmark and set the action
Bookmark bookmark = new Bookmark();
bookmark.setAction(action);
bookmark.setTitle("My Bookmark");
pdfDocument.getBookmarks().add(bookmark);
// Save the document
pdfDocument.save("output.pdf");
}
}
In this code:
The XYZExplicitDestination is created with -1 for the zoom level, which indicates that the zoom level should be inherited.
The GoToAction is then created with this destination and assigned to the bookmark.
Make sure to check the resulting PDF in Adobe Reader to confirm that the bookmark action is set correctly to use “XYZ” with inherit zoom.
If you continue to face issues, consider checking the version of Aspose.PDF you are using, as there may be updates or bug fixes related to bookmark actions.