|
// The class to handle the before-page event while converting an XPS document.
|
|
// Learn more: https://docs.aspose.com/page/java/xps/modifying-xps-pages-on-events/
|
|
private static class NavigationInjector extends BeforePageSavingEventHandler {
|
|
// The font in which navigation hyperlinks and page numbers will be displayed.
|
|
private final XpsFont font;
|
|
// The page numbers to convert.
|
|
private final TreeMap<Integer, Integer> pageNumbers;
|
|
|
|
public NavigationInjector(XpsFont font, int[] pageNumbers) {
|
|
this.font = font;
|
|
if (pageNumbers == null) {
|
|
this.pageNumbers = null;
|
|
return;
|
|
}
|
|
|
|
// Turn the page number array into a sorted collection of unique values.
|
|
this.pageNumbers = new TreeMap<>();
|
|
for (int pn : pageNumbers) {
|
|
this.pageNumbers.put(pn, 0);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The action itself to be triggered on a before-page event.
|
|
*
|
|
* @param args The event arguments.
|
|
*/
|
|
@Override
|
|
public void handle(BeforeSavingEventArgs<PageAPI> args) {
|
|
PageAPI api = args.getElementAPI();
|
|
|
|
XpsGlyphs glyphs;
|
|
// For all pages in the output PDF except the first one...
|
|
if (args.getOutputPageNumber() > 1) {
|
|
// ...insert a hyperlink to the first page...
|
|
glyphs = api.createGlyphs(font, 15f, 5f, api.getHeight() - 10f, "[First]");
|
|
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
|
|
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(pageNumbers == null ? 1 : pageNumbers.firstKey()));
|
|
api.add(glyphs);
|
|
|
|
// ...and to the previous page.
|
|
glyphs = api.createGlyphs(font, 15f, 60f, api.getHeight() - 10f, "[Prev]");
|
|
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
|
|
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
|
|
pageNumbers == null ? args.getAbsolutePageNumber() - 1 :
|
|
pageNumbers.keySet().toArray(new Integer[0])[args.getOutputPageNumber() - 2]));
|
|
api.add(glyphs);
|
|
}
|
|
|
|
// For all pages in the output PDF except the last one...
|
|
if ((pageNumbers != null && args.getOutputPageNumber() < pageNumbers.size()) ||
|
|
(pageNumbers == null && args.getOutputPageNumber() < api.getTotalPageCount())) {
|
|
// ...insert a hyperlink to the next page...
|
|
glyphs = api.createGlyphs(font, 15f, 110f, api.getHeight() - 10f, "[Next]");
|
|
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
|
|
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
|
|
pageNumbers == null ? args.getAbsolutePageNumber() + 1 :
|
|
pageNumbers.keySet().toArray(new Integer[0])[args.getOutputPageNumber()]));
|
|
api.add(glyphs);
|
|
|
|
// ...and to the last page.
|
|
glyphs = api.createGlyphs(font, 15f, 160f, api.getHeight() - 10f, "[Last]");
|
|
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
|
|
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
|
|
pageNumbers == null ? api.getTotalPageCount() :
|
|
pageNumbers.lastKey()));
|
|
api.add(glyphs);
|
|
}
|
|
|
|
// Insert a page number in the bottom-right corner.
|
|
glyphs = api.createGlyphs(font, 15f, api.getWidth() - 20f, api.getHeight() - 10f,
|
|
String.valueOf(args.getOutputPageNumber()));
|
|
glyphs.setFill(api.createSolidColorBrush(Color.BLACK));
|
|
api.add(glyphs);
|
|
|
|
// Add an outline entry to display the links to the converted pages in the navigation pane of a PDF viewer.
|
|
api.addOutlineEntry(String.format("Page %d", args.getOutputPageNumber()), 1, args.getAbsolutePageNumber());
|
|
}
|
|
} |