Can I prevent ABCpdf from mashing words together (e.g. mashingwordstogether) when convertering PDF to Text?

前端 未结 2 2136
故里飘歌
故里飘歌 2021-02-10 01:50

I\'m using ABCpdf to extract the text content of some PDF files, in particular by calling Doc.GetText(\"Text\"). (You call it in a loop, once per page.) This usually works well,

2条回答
  •  再見小時候
    2021-02-10 02:14

    Short Answer: You can get individual tokens of text via Doc.GetText("SVG"), parsing the XML for TEXT and TSPAN elements, and determining if there is layout spacing that should be treated as actual spaces. The behavior you're seeing from PDFBox is probably their attempt to make that assumption. Also, even Adobe Acrobat can return spaced text via the clipboard as PDFBox does.

    Long Answer: This may cause more problems, as this may not be the original intent of the text in the PDF.

    ABCpdf is doing the correct thing here, as the PDF spec only describes where things should be placed in the output medium. One can construct a PDF file that ABCpdf interprets in both styles, even though the original sentence looks nearly the same.

    To demonstrate this, here is a snapshot of a document from Adobe InDesign that shows a text layout matching both cases for your sample sentence.

    Snapshot From Adobe InDesign of a Specially Constructed PDF with Layout Spaces versus Text Spaces

    Note that the first row was not constructed with actual spaces, instead, the words were placed by hand in individual text regions and lined up to look approximately like a properly spaced sentence. The second row has a single sentence that has actual text spaces between the words, in a single text region.

    When exported to PDF and then read in by ABCpdf, Doc.GetText("TEXT") will return the following:

    ThisSentenceDoesn'tHaveAnySpacesBetweenWords.  
    This Sentence Doesn't Have Any Spaces Between Words.
    

    Thus if you wish to detect layout spaces, you must use SVG output and step through the tokens of text manually. Doc.GetText("SVG") returns text and other drawing entities as ABCpdf sees them on the page, and you can decide how you want to handle the case of layout based spacing.

    You'll receive output similar to this:

    
    
    This
    Sentence
    Doesn’t
    Have
    Any
    Spaces
    Between
    Words.
    
    This Sentence Doesn’t Have Any Spaces Between Words.
    
    

    And note that the basic structure reveals the original intent that gave you problems. (xml:space and attributes removed, whitespace modifications for the sake of example)

    
    
      This
      Sentence
      Doesn’t
      Have
      Any
      Spaces
      Between
      Words.
      
        This Sentence Doesn’t Have 
        Any Spaces Between W
        ords.
      
    
    

提交回复
热议问题