Skip to content
Closed
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
18 changes: 13 additions & 5 deletions pdfbox/src/main/java/org/apache/pdfbox/text/PDFTextStripper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
Expand Down
78 changes: 78 additions & 0 deletions pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}