Skip to content

Commit

Permalink
#79 - Accessible bookmarks implementation and testcase.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfickle committed Jan 28, 2019
1 parent 6cc4f40 commit abd36ee
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ public static void main(String... args) throws Exception {
run("running");
run("lists");
run("tables");
run("bookmarks");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<title>Simple PDF/UA Bookmarks sample</title>
<style>
@page {
size: A4;
margin: 0;
}
</style>
<bookmarks>
<bookmark name="Outer" href="#secondpage">
<bookmark name="Inner" href="#thirdpage"/>
</bookmark>
</bookmarks>
</head>
<body>
<div style="width: 100%; height: 20px; background-color: transparent; page-break-after: always;"></div>

<div id="secondpage" style="background-color: red; height: 10px;"></div>
<div style="width: 100%; height: 20px; background-color: transparent; page-break-after: always;"></div>
<div id="thirdpage" style="background-color: green; height: 10px;"></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,20 @@ public String toString() {
private static class FigureContentItem extends GenericContentItem {
}

/**
* Given a box, gets its structual element.
*/
public static PDStructureElement getStructualElementForBox(Box targetBox) {
if (targetBox != null &&
targetBox.getAccessibilityObject() != null &&
targetBox.getAccessibilityObject() instanceof AbstractStructualElement) {

return ((AbstractStructualElement) targetBox.getAccessibilityObject()).elem;
}

return null;
}

public void finishPdfUa() {
PDStructureTreeRoot root = _od.getWriter().getDocumentCatalog().getStructureTreeRoot();
if (root == null) {
Expand Down Expand Up @@ -524,8 +538,11 @@ private void finishTreeItem(AbstractTreeItem item, AbstractStructualElement pare
// which contains other structual elements or content items (text).
GenericStructualElement child = (GenericStructualElement) item;

if (child.children.isEmpty()) {
if (child.children.isEmpty() &&
(child.box.getElement() == null || !child.box.getElement().hasAttribute("id"))) {
// There is no point in outputting empty structual elements.
// Exception is elements with an id which may be there to
// use as a link or bookmark destination.
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.logging.Level;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitHeightDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
Expand Down Expand Up @@ -68,9 +69,10 @@ private void writeBookmarks(RenderingContext c, Box root, PDOutlineNode parent,
private void writeBookmark(RenderingContext c, Box root, PDOutlineNode parent, Bookmark bookmark) {
String href = bookmark.getHRef();
PDPageXYZDestination target = null;

Box box = null;

if (href.length() > 0 && href.charAt(0) == '#') {
Box box = _sharedContext.getBoxById(href.substring(1));
box = _sharedContext.getBoxById(href.substring(1));

if (box != null) {
target = createBoxDestination(c, _writer, _od, _dotsPerPoint, root, box);
Expand All @@ -84,6 +86,12 @@ private void writeBookmark(RenderingContext c, Box root, PDOutlineNode parent, B
PDOutlineItem outline = new PDOutlineItem();
outline.setDestination(target == null ? _defaultDestination : target);
outline.setTitle(bookmark.getName());

PDStructureElement se = PdfBoxAccessibilityHelper.getStructualElementForBox(box);
if (se != null) {
outline.setStructureElement(se);
}

parent.addLast(outline);
writeBookmarks(c, root, outline, bookmark.getChildren());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,14 +846,17 @@ public void start(Document doc) {
}

public void finish(RenderingContext c, Box root) {
_bmManager.loadBookmarks();
_bmManager.writeOutline(c, root);
processControls();
_linkManager.processLinks();

if (_pdfUa != null) {
_pdfUa.finishPdfUa();
}

// Bookmarks must come after PDF/UA structual tree creation
// because bookamrks link to structual elements in the tree.
_bmManager.loadBookmarks();
_bmManager.writeOutline(c, root);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ private void addPdfUaXMPSchema(PDDocument doc) {
{
PDDocumentCatalog catalog = doc.getDocumentCatalog();
String lang = _doc.getDocumentElement().getAttribute("lang");
catalog.setLanguage(lang != null ? lang : "EN-US");
catalog.setLanguage(!lang.isEmpty() ? lang : "EN-US");
catalog.setViewerPreferences(new PDViewerPreferences(new COSDictionary()));
catalog.getViewerPreferences().setDisplayDocTitle(true);

Expand Down

0 comments on commit abd36ee

Please sign in to comment.