Skip to content

Use ActualText when getting the text for the text layer - #20014

Open
calixteman wants to merge 1 commit into
mozilla:masterfrom
calixteman:issue20007
Open

calixteman wants to merge 1 commit into
mozilla:masterfrom
calixteman:issue20007

Conversation

@calixteman

Copy link
Copy Markdown
Contributor

No description provided.

@calixteman calixteman linked an issue Jun 16, 2025 that may be closed by this pull request
@calixteman

Copy link
Copy Markdown
Contributor Author

/botio test

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

Comment thread src/core/evaluator.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the args[1]?.get("ActualText") be exposed in the getOperatorList result as well?

e.g. something like this

args = [
    args[0].name,
    args[1] instanceof Dict ? args[1].get("MCID") : null,
    args[1] instanceof Dict ? args[1].get("ActualText") : null  // <--- extra arg
];

in

pdf.js/src/core/evaluator.js

Lines 2300 to 2303 in d2a6638

args = [
args[0].name,
args[1] instanceof Dict ? args[1].get("MCID") : null,
];

Not sure whether its a breaking change, but it's crucial for reconstructing content (e.g. svg) from the results of getOperatorList() when not using getTextContent().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you file a bug and explain why it'd be useful to have such a feature ?
Could it help to fix an existing issue in the current viewer ?

@fnlctrl fnlctrl Jun 17, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calixteman Ok, I'll open a another ticket for it. I don't think it's related to the current issue with the viewer.

Actually I opened the original ticket because I got wrong text from getOperatorList(), and the viewer is also affected so I used it to open the ticket as it's easier to reproduce than a code snippet.

I was actually building a pdf -> svg conversion tool with getOperatorList(). I found getTextContent() to be not useful - it only extracts text, and the shape info can only be obtained from getOperatorList(), and there's no easy way to interweave the text+shape back into correct order from the results of both functions, so I ditched getTextContent() and only use getOperatorList() to also obtain text.

@timvandermeij

Copy link
Copy Markdown
Collaborator

/botio-linux preview

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

@timvandermeij timvandermeij left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, with the comments addressed and passing tests.

Comment thread test/unit/api_spec.js Resolved
Comment thread src/core/evaluator.js
flushTextContentItem();
if (args[0]?.name === "Span") {
textContentItem.span = stringToPDFString(
args[1]?.get("ActualText") || ""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this PR also fix #12237 perhaps?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a start at fixing that issue. This is the first step, getting this /ActualText into the text content. That issue is asking for src/display/text_layer.js:#processItems to draw spans containing this actual text in the right places. That will mean also accumulating the text drawing that would have been done (to know the bounds of the glyphs that will be drawn) so that the bounds of the span can be calculated.

Comment thread test/unit/api_spec.js
});

it("get the text a content stream containing some ActualText", async function () {
const loadingTask = getDocument(buildGetDocumentParams("issue20007.pdf"));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know why, but the unit test failure suggests that this can't be loaded:

TEST-UNEXPECTED-FAIL | get the text a content stream containing some ActualText | in firefox | ResponseException: Unexpected server response (404) while retrieving PDF "http://127.0.0.1:38175/test/pdfs/issue20007.pdf". in http://127.0.0.1:38175/src/shared/util.js (line 501)

Moreover, is the movement in the reference test expected?

Comment thread src/core/evaluator.js
Comment on lines +3463 to +3467
if (args[0]?.name === "Span") {
textContentItem.span = stringToPDFString(
args[1]?.get("ActualText") || ""
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this addition does. This is for a BMC which is just a tag so there never is an args[1]? beginMarkedContentProps (below) is for BDC which is a tag and dictionary.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Cannot obtain text info with custom fonts

5 participants