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
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ public void processPage(PDPage page) throws IOException
}
}
characterListMapping.clear();
// marked content can't span pages, so drop anything left unclosed on a previous page
currentMarkedContents.clear();
actualText = null;
actualTextMarkedContent = null;
firstActualTextPosition = false;
super.processPage(page);
writePage();
endPage(page);
Expand Down
49 changes: 49 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 @@ -853,6 +853,55 @@ void testNestedActualText() throws IOException
}
}

/**
* Test that an /ActualText sequence that isn't closed on a page doesn't replace the text of the
* following pages or of the next document processed by the same stripper.
*
* @throws IOException
*/
@Test
void testUnclosedActualText() throws IOException
{
PDFTextStripper localStripper = new PDFTextStripper();
localStripper.setLineSeparator("\n");
localStripper.setPageEnd("\n");
try (PDDocument doc = new PDDocument())
{
PDPage page1 = new PDPage();
try (PDPageContentStream cs = new PDPageContentStream(doc, page1))
{
cs.beginText();
cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
cs.newLineAtOffset(50, 700);
cs.beginMarkedContent(COSName.SPAN, createActualText("X"));
cs.showText("one");
cs.endText();
}
doc.addPage(page1);
doc.addPage(createPageWithText(doc, "two"));
assertEquals("X\ntwo\n", localStripper.getText(doc));
}
try (PDDocument doc = new PDDocument())
{
doc.addPage(createPageWithText(doc, "three"));
assertEquals("three\n", localStripper.getText(doc));
}
}

private static PDPage createPageWithText(PDDocument doc, String text) throws IOException
{
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.showText(text);
cs.endText();
}
return page;
}

private static PDPropertyList createActualText(String actualText)
{
COSDictionary dict = new COSDictionary();
Expand Down