Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle bookmarks in body #408 #409

Merged
merged 1 commit into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<style>
@page {
size: A4;
margin: 0;
}
</style>
</head>
<body>
<bookmarks>
<bookmark name="Test bookmark" href="#secondpage"/>
</bookmarks>
<div style="width: 100%; height: 20px; background-color: transparent; page-break-after: always;"></div>

<div id="secondpage" style="background-color: red; height: 10px;"></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ public void testBookmarkHeadSimple() throws IOException {
remove("bookmark-head-simple", doc);
}

/**
* Tests that a simple body bookmark linking to top of the second page works.
*/
@Test
public void testBookmarkBodySimple() throws IOException {
PDDocument doc = run("bookmark-body-simple");
PDDocumentOutline outline = doc.getDocumentCatalog().getDocumentOutline();

PDOutlineItem bm = outline.getFirstChild();
assertThat(bm.getTitle(), equalTo("Test bookmark"));
assertThat(bm.getDestination(), instanceOf(PDPageXYZDestination.class));
PDPageXYZDestination dest = (PDPageXYZDestination) bm.getDestination();

// At top of second page.
assertEquals(dest.getPage(), doc.getPage(1));
assertEquals(doc.getPage(1).getMediaBox().getUpperRightY(), dest.getTop(), 1.0d);

remove("bookmark-body-simple", doc);
}

/**
* Tests that a head bookmark linking to transformed element (by way of transform) on third page works.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,14 @@ public static PDPageXYZDestination createBoxDestination(
public void loadBookmarks() {
Document doc = _xml;
Element head = DOMUtil.getChild(doc.getDocumentElement(), "head");
Element body = DOMUtil.getChild(doc.getDocumentElement(), "body");
handleBookmarksInParent(head);
handleBookmarksInParent(body);
}

if (head != null) {
Element bookmarks = DOMUtil.getChild(head, "bookmarks");
private void handleBookmarksInParent(Element element) {
if (element != null) {
Element bookmarks = DOMUtil.getChild(element, "bookmarks");
if (bookmarks != null) {
List<Element> l = DOMUtil.getChildren(bookmarks, "bookmark");
if (l != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,14 @@ private void writeBookmark(RenderingContext c, Box root, PDOutlineNode parent, B

private void loadBookmarks(Document doc) {
Element head = DOMUtil.getChild(doc.getDocumentElement(), "head");
if (head != null) {
Element bookmarks = DOMUtil.getChild(head, "bookmarks");
Element body = DOMUtil.getChild(doc.getDocumentElement(), "body");
handleBookmarksInParent(head);
handleBookmarksInParent(body);
}

private void handleBookmarksInParent(Element element) {
if (element != null) {
Element bookmarks = DOMUtil.getChild(element, "bookmarks");
if (bookmarks != null) {
List<Element> l = DOMUtil.getChildren(bookmarks, "bookmark");
if (l != null) {
Expand Down