diff --git a/pdfbox/src/main/java/org/apache/pdfbox/text/PDFTextStripper.java b/pdfbox/src/main/java/org/apache/pdfbox/text/PDFTextStripper.java index 3fe1ce0ad72..40a9a554cd6 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/text/PDFTextStripper.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/text/PDFTextStripper.java @@ -162,6 +162,8 @@ public class PDFTextStripper extends LegacyPDFStreamEngine // to replace the unicode of the first TextPosition and empty the others private boolean firstActualTextPosition = false; private String actualText = null; + // the marked content that set actualText, so that only its end resets it + private PDMarkedContent actualTextMarkedContent = null; /** * The charactersByArticle is used to extract text by article divisions. For example a PDF that has two columns like @@ -861,11 +863,16 @@ public void beginMarkedContentSequence(COSName tag, COSDictionary properties) { PDMarkedContent markedContent = PDMarkedContent.create(tag, properties); currentMarkedContents.push(markedContent); - actualText = markedContent.getActualText(); - if (actualText != null) + // the outermost /ActualText replaces all nested content, so ignore any nested one + if (actualText == null) { - actualText = actualText.replace("\u00ad", ""); // remove soft hyphens - firstActualTextPosition = true; + String markedContentActualText = markedContent.getActualText(); + if (markedContentActualText != null) + { + actualText = markedContentActualText.replace("\u00ad", ""); // remove soft hyphens + actualTextMarkedContent = markedContent; + firstActualTextPosition = true; + } } super.beginMarkedContentSequence(tag, properties); } @@ -876,9 +883,10 @@ public void endMarkedContentSequence() PDMarkedContent markedContent = currentMarkedContents.peek(); if (markedContent != null) { - if (markedContent.getActualText() != null) + if (markedContent == actualTextMarkedContent) { actualText = null; + actualTextMarkedContent = null; } currentMarkedContents.pop(); } diff --git a/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java b/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java index cca959f1b23..a5b2c8a8366 100644 --- a/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java +++ b/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java @@ -53,10 +53,13 @@ import org.apache.logging.log4j.LogManager; import org.apache.fontbox.util.BoundingBox; import org.apache.pdfbox.Loader; +import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDPropertyList; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFontDescriptor; import org.apache.pdfbox.pdmodel.font.PDType1Font; @@ -781,4 +784,79 @@ void testArticlesOnSameLine() throws IOException assertEquals("LEFT RIGHT\n", text); } } + + /** + * Test that marked content nested in an /ActualText sequence doesn't end the replacement. + * + * @throws IOException + */ + @Test + void testActualTextWithNestedMarkedContent() throws IOException + { + try (PDDocument doc = new PDDocument()) + { + PDPage page = new PDPage(); + try (PDPageContentStream cs = new PDPageContentStream(doc, page)) + { + cs.beginText(); + cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12); + cs.newLineAtOffset(50, 700); + cs.beginMarkedContent(COSName.SPAN, createActualText("XYZ")); + cs.beginMarkedContent(COSName.getPDFName("Foo")); + cs.showText("abc"); + cs.endMarkedContent(); + cs.showText("def"); + cs.endMarkedContent(); + cs.endText(); + } + doc.addPage(page); + + PDFTextStripper localStripper = new PDFTextStripper(); + localStripper.setLineSeparator("\n"); + localStripper.setPageEnd("\n"); + assertEquals("XYZ\n", localStripper.getText(doc)); + } + } + + /** + * Test that an /ActualText sequence nested in another one is ignored, because the outer one + * replaces all of its content. + * + * @throws IOException + */ + @Test + void testNestedActualText() throws IOException + { + try (PDDocument doc = new PDDocument()) + { + PDPage page = new PDPage(); + try (PDPageContentStream cs = new PDPageContentStream(doc, page)) + { + cs.beginText(); + cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12); + cs.newLineAtOffset(50, 700); + cs.beginMarkedContent(COSName.SPAN, createActualText("X")); + cs.showText("a"); + cs.beginMarkedContent(COSName.SPAN, createActualText("Y")); + cs.showText("b"); + cs.endMarkedContent(); + cs.showText("tail"); + cs.endMarkedContent(); + cs.endText(); + } + doc.addPage(page); + + PDFTextStripper localStripper = new PDFTextStripper(); + localStripper.setLineSeparator("\n"); + localStripper.setPageEnd("\n"); + assertEquals("X\n", localStripper.getText(doc)); + } + } + + private static PDPropertyList createActualText(String actualText) + { + COSDictionary dict = new COSDictionary(); + dict.setString(COSName.ACTUAL_TEXT, actualText); + return PDPropertyList.create(dict); + } }