[
https://issues.apache.org/jira/browse/PDFBOX-5532?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17623945#comment-17623945
]
Michael Klink commented on PDFBOX-5532:
---------------------------------------
For a general solution you indeed need to know the encoding associated with the
font in question. I.e. you also have to keep track of the *Tf* commands and
lookup the associated font resource from the page resources. And you also have
to handle *q* and *Q* accordingly. Also don't forget that there are not only
page content streams, text can also be in form XObjects or Patterns referred to
from there
Also you may have to deal with fonts whose encoding does not really help, in
particular encodings with non-standard names for glyphs and *Identity-H* and
*Identity-V*.
So this really is no trivial problem to solve in general.
If you can be sure that in your use case the PDFs are generated by the same
software all over, consider analyzing the PDF contents and determining whether
the text drawing in them allows for some short cuts in your task...
> COSString field non-ascii characters
> ------------------------------------
>
> Key: PDFBOX-5532
> URL: https://issues.apache.org/jira/browse/PDFBOX-5532
> Project: PDFBox
> Issue Type: Bug
> Reporter: David
> Priority: Major
>
>
> Hello,
> I am reading a pdf document but in the COSString field non-ascii characters
> are being retrieved. What can be the motive? I am using version
> pdfbox-2.0.24.jar
> This would be an example of the pdf document parsed:
> COSInt\{50}
> COSInt\{0}
> PDFOperator\{Td}
> COSString\{åÅÕãÁâ@}
> PDFOperator\{Tj}
> COSFloat\{770.18}
> COSInt\{0}
> PDFOperator\{Td}
> COSString\{×–Ž–©@}
> PDFOperator\{Tj}
> COSFloat\{520.21}
> COSInt\{0}
> Function java:
> public static PDDocument replaceText(PDDocument document, String
> searchString, String replacement) throws IOException {
>
> PDPageTree pages = document.getDocumentCatalog().getPages();
> for (PDPage page : pages) {
>
> PDFStreamParser parser = new PDFStreamParser(page);
> parser.parse();
> List tokens = parser.getTokens();
> for (int j = 0; j < tokens.size(); j++) {
> Object next = tokens.get(j);
>
> if (next instanceof Operator) {
> Operator op = (Operator) next;
>
> if (op.getName().equals("Tj")) {
> COSString previous = (COSString)
> tokens.get(j - 1);
> String string = previous.getString();
> System.out.println("previous:=" + string);
>
>
> if (string.equals(searchString)){
> COSString sx = new
> COSString(replacement);
> previous.setValue(sx.getBytes());
>
> }
> }
> }
> }
> // now that the tokens are updated we will replace the
> page content stream.
> PDStream updatedStream = new PDStream(document);
> OutputStream out = updatedStream.createOutputStream();
> ContentStreamWriter tokenWriter = new
> ContentStreamWriter(out);
> tokenWriter.writeTokens(tokens);
> page.setContents(updatedStream);
> out.close();
>
>
> }
> return document;
> }
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]