Skip to content

Commit

Permalink
#79 - Deal with a structual element with content items over two or mo…
Browse files Browse the repository at this point in the history
…re pages.

Previously failing layers test is now passing.
  • Loading branch information
danfickle committed Jan 20, 2019
1 parent 536212a commit a795565
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,15 @@ public void paint(RenderingContext c, DisplayListPageContainer pageOperations) {
} else if (op instanceof PaintLayerBackgroundAndBorder) {

PaintLayerBackgroundAndBorder dlo = (PaintLayerBackgroundAndBorder) op;

c.getOutputDevice().startStructure(StructureType.LAYER, dlo.getMaster());
c.getOutputDevice().startStructure(StructureType.BACKGROUND, dlo.getMaster());

dlo.getMaster().paintBackground(c);
dlo.getMaster().paintBorder(c);

c.getOutputDevice().endStructure(StructureType.BACKGROUND, dlo.getMaster());
c.getOutputDevice().endStructure(StructureType.LAYER, dlo.getMaster());

} else if (op instanceof PaintReplacedElement) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDNumberTreeNode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkedContentReference;
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement;
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot;
import org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDMarkedContent;
Expand Down Expand Up @@ -102,7 +103,7 @@ public void finishPdfUa() {
root.appendKid(rootElem);

_root.elem = rootElem;
finishStructure(_root, _root.elem);
finishStructure(_root, _root);

_od.getWriter().getDocumentCatalog().setStructureTreeRoot(root);
}
Expand Down Expand Up @@ -159,7 +160,7 @@ private String chooseTag(StructureItem item) {
return StandardStructureTypes.SPAN;
}

private void finishStructure(StructureItem item, PDStructureElement parent) {
private void finishStructure(StructureItem item, StructureItem parent) {
for (StructureItem child : item.children) {
if (child.mcid == -1) {
if (child.children.isEmpty()) {
Expand All @@ -168,25 +169,38 @@ private void finishStructure(StructureItem item, PDStructureElement parent) {

if (child.box instanceof LineBox &&
!child.box.hasNonTextContent(_ctx)) {
// We skip line boxes in the tree.
finishStructure(child, parent);
} else {
String pdfTag = chooseTag(child);

child.parentElem = parent;
child.elem = new PDStructureElement(pdfTag, parent);
child.elem.setParent(parent);
child.parentElem = parent.elem;
child.elem = new PDStructureElement(pdfTag, child.parentElem);
child.elem.setParent(child.parentElem);
child.elem.setPage(child.page);
System.out.println("ADDING$$: " + child + " :::: " + child.elem + "-----" + pdfTag);
parent.appendKid(child.elem);
child.parentElem.appendKid(child.elem);

finishStructure(child, child.elem);
finishStructure(child, child);
}
} else if (child.type == StructureType.TEXT) {
child.parentElem = parent;
parent.appendKid(new PDMarkedContent(COSName.getPDFName("Span"), child.dict));
} else if (child.type == StructureType.BACKGROUND) {
child.parentElem = parent;
parent.appendKid(new PDArtifactMarkedContent(child.dict));
if (child.page == parent.page) {
// If this is on the same page as its parent structual element
// we can just use the dict with mcid in it only.
System.out.println("ADDING MCID DIRECT: " + child.mcid + " , " + child.page);
child.parentElem = parent.elem;
child.parentElem.appendKid(new PDMarkedContent(COSName.getPDFName("Span"), child.dict));
} else {
// Otherwise we need a more complete dict with the page.
child.parentElem = parent.elem;
child.dict = new COSDictionary();
child.dict.setItem(COSName.TYPE, COSName.getPDFName("MCR"));
child.dict.setItem(COSName.PG, child.page);
child.dict.setInt(COSName.MCID, child.mcid);
System.out.println("ADDING MCR: " + child.mcid + " , " + child.page);
PDMarkedContentReference ref = new PDMarkedContentReference(child.dict);
child.parentElem.appendKid(ref);
}
}
}
}
Expand Down Expand Up @@ -262,6 +276,7 @@ private StructureItem createMarkedContentStructureItem(StructureType type, Box b

current.mcid = _nextMcid;
current.dict = createMarkedContentDictionary();
current.page = _page;

_pageContentItems.get(_pageContentItems.size() - 1).add(current);

Expand Down

0 comments on commit a795565

Please sign in to comment.