Aspose PDF not able to set InheritZoom on bookmarks in Java

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:

  1. 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));

  1. bookmark.setAction(pageNumber + " XYZ 0 0 0");

  2. bookmark.setAction(“GoTo”);
    bookmark.setDestination(pageNumber + " XYZ 0 0 0");

  3. bookmark.setAction(“XYZ”);
    bookmark.setDestination(pageNumber + " 0 0 0");

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”:

  1. <</Dest[735 0 R/FitH 792]/F 0/Title(\376\377\0001\0000\0001\0000)/First 751 0 R/Last 752 0 R/Count 24/C[0 0 0]/Parent 749 0 R>>

  2. <</Dest[735 0 R/FitH 792]/F 0/Title(\376\377\0001\0000\0001\0000)/First 751 0 R/Last 752 0 R/Count 24/C[0 0 0]/Parent 749 0 R>>

  3. <</F 0/Title(\376\377\0001\0000\0001\0000)/First 751 0 R/Last 752 0 R/Count 24/A<</S/GoTo/D[735 0 R/FitH 792]>>/C[0 0 0]/Parent 749 0 R>>

  4. <</Dest[735 0 R/FitH 792]/F 0/Title(\376\377\0001\0000\0001\0000)/First 751 0 R/Last 752 0 R/Count 24/C[0 0 0]/Parent 749 0 R>>

What is the right implementation to get my PDF bookmark to result with “/Goto” and “/XYZ” like below?

/A <</S /GoTo /D [195 0 R /XYZ -4 795 null]>>

Thanks much ahead!!!

@yachan

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.