[ 
https://issues.apache.org/jira/browse/PDFBOX-4532?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16834060#comment-16834060
 ] 

Tilman Hausherr commented on PDFBOX-4532:
-----------------------------------------

Heh heh, everything of the PDF specification that I don't use has the potential 
to be called "exotic" :)

Here's some code to extract this text. It has a flaw that it replaces only the 
last element, i.e. won't work properly if "diff count" in the output is other 
than 1. It also requires a modification in PDFBox, a TextPosition.setUnicode() 
method. The correct way to go would probably be set all "deltas" to empty 
except the last one (or the first one), and to assign the ActualText to it. Or 
to create a new field ActualText.
{code:java}
public class PDFBox4532ExtractText extends PDFTextStripper
{

    public static void main(String[] args) throws IOException
    {
        PDDocument doc = PDDocument.load(new File("PDFBOX-4532.pdf"));
        PDFTextStripper stripper = new PDFBox4532ExtractText();
        System.out.println(stripper.getText(doc));
    }

    public PDFBox4532ExtractText() throws IOException
    {
        addOperator(new BeginMarkedContentSequenceWithProperties());
        addOperator(new BeginMarkedContentSequence());
        addOperator(new EndMarkedContentSequence());
    }

    Stack<Integer> stack = new Stack<>();
    String actualText = null;

    @Override
    public void beginMarkedContentSequence(COSName tag, COSDictionary 
properties)
    {
        if (properties != null && properties.containsKey(COSName.ACTUAL_TEXT))
        {
            actualText = properties.getString(COSName.ACTUAL_TEXT);
            
stack.push(charactersByArticle.get(charactersByArticle.size()-1).size());
        }
        else
        {
            stack.push(null);
        }
        super.beginMarkedContentSequence(tag, properties);
    }

    @Override
    public void endMarkedContentSequence()
    {
        Integer i = stack.pop();
        if (i != null)
        {
            List<TextPosition> lastArticle = 
charactersByArticle.get(charactersByArticle.size()-1);
            TextPosition textPosition = lastArticle.get(lastArticle.size()-1);
            textPosition.setUnicode(actualText);
            System.out.println("EMC: ActualText: " + actualText + ", diff 
count: " + (lastArticle.size()-i));
        }
        super.endMarkedContentSequence();
    }
}
{code}

> PDFTextStripper replacing the decimal with white space
> ------------------------------------------------------
>
>                 Key: PDFBOX-4532
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4532
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Text extraction
>    Affects Versions: 2.0.15
>            Reporter: Akash Gupta
>            Priority: Major
>              Labels: ActualText
>         Attachments: FSUSA00BDD.pdf, PDFBOX-4532-reduced.pdf, 
> code_textStripper.PNG, numbers_without_decimal.PNG
>
>
> I'm using the PDFTextStripperByArea to be specific and trying to extract a 
> particular area from the document. 
> In the output most the numbers (all but one) have their decimal point 
> replaced by a white space. When I copy and paste the text using Abobe 
> reader/chrome the decimal point are preserved.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to