2011-01-02 12 views
5

का उपयोग कर पीडीएफ फ़ाइल से टेक्स्ट और टेक्स्ट आयताकार निर्देशांक निकालें I पीडीएफ फ़ाइल से सभी शब्द और उनके स्थान निर्देशांक प्राप्त करने का प्रयास कर रहा हूं। मैं .NET पर एक्रोबैट एपीआई का उपयोग करने में सफल रहा हूं। अब, मैं एक मुफ्त एपीआई का उपयोग कर एक ही परिणाम प्राप्त करने की कोशिश कर रहा हूं, जैसे iTextSharp (.NET संस्करण)। मैं PRTokeniser के साथ टेक्स्ट (लाइन द्वारा लाइन) प्राप्त कर सकता हूं, लेकिन मुझे इस बात का कोई अंदाजा नहीं है कि लाइन के निर्देशांक कैसे प्राप्त करें, प्रत्येक शब्द के अकेले रहने दें।itextsharp

+3

नीली लपटें और iTextSharp मुक्त नहीं हैं। –

उत्तर

9

आप com.itextpdf.text.pdf.parser पैकेज वर्गों का उपयोग करना चाहेंगे। वे वर्तमान परिवर्तन, रंग, फ़ॉन्ट, और बहुत आगे ट्रैक करते हैं।

अफसोस की बात है कि, इन वर्गों को नई पुस्तक में शामिल नहीं किया गया था, इसलिए आपको JavaDoc के साथ छोड़ दिया गया है, और मानसिक रूप से इसे जावा से सी # में परिवर्तित कर दिया गया है, जो कि काफी अधिक नहीं है।

तो तुम एक PdfTextExtractor में एक LocationTextExtractionStrategy प्लग करने के लिए चाहता हूँ।

यह आपको स्ट्रिंग और स्थान देगा क्योंकि उन्हें पीडीएफ में प्रस्तुत किया गया है। यह समझने के लिए आप पर निर्भर होगा कि शब्दों के रूप में (और अनुच्छेद यदि आवश्यक हो, तो ouch)।

ध्यान रखें कि पीडीएफ टेक्स्ट लेआउट के बारे में कुछ भी नहीं जानता है। प्रत्येक चरित्र को व्यक्तिगत रूप से रखा जा सकता है। अगर कोई ऐसा इच्छुक था (और ऐसा करने के लिए उन्हें कॉम्बो प्लेटर से कम कुछ टैकोस होना चाहिए) तो वे सभी 'ए' को किसी दिए गए पेज पर खींच सकते हैं, फिर सभी 'बी' और बहुत आगे।

अधिक वास्तविकता से, कोई व्यक्ति उस पृष्ठ पर सभी टेक्स्ट खींच सकता है जो FontA का उपयोग करता है, फिर FontB के लिए सब कुछ, और इसी तरह। यह अधिक कुशल सामग्री धाराओं का उत्पादन कर सकता है। ध्यान रखें कि इटैलिक और बोल्ड (और बोल्ड इटैलिक) सभी अलग फोंट कर रहे हैं। अगर कोई बोल्ड (या जो भी) के रूप में किसी शब्द का केवल एक हिस्सा चिह्नित करता है, तो उस लॉजिकल वर्ड को कम से कम दो ड्राइंग कमांड में विभाजित करने की आवश्यकता होती है।

लेकिन बहुत से लोग लॉजिकल ऑर्डर में पीडीएफ में अपना टेक्स्ट लिखते हैं ... जो लोग इसे पार्स करने की कोशिश कर रहे लोगों के लिए बहुत ही सुंदर हैं, लेकिन आपको इसकी अपेक्षा नहीं करनी चाहिए। क्योंकि आप हमेशा कुछ विषम गेंदों में भाग लेंगे जो नहीं करता है।

11

मेरा खाता मार्क स्टोरर के उत्तर के लिए बहुत नया जवाब है।

मैं सीधे LocationTextExtracationStrategy (मुझे लगता है कि मैं कुछ गलत कर किया जाना चाहिए) उपयोग करने में सक्षम नहीं था। जब मैंने LocationTextExtracationStrategy का उपयोग किया तो मैं पाठ प्राप्त करने में सक्षम था लेकिन मुझे पता नहीं लगा कि प्रत्येक स्ट्रिंग (या स्ट्रिंग्स की रेखा) के लिए कॉर्ड कैसे प्राप्त करें।

मैं LocationTextExtracationStrategy उपवर्गीकरण और डेटा क्योंकि यह उसका आंतरिक रूप से है मैं चाहता था उजागर समाप्त हो गया।

मैं इसे भी .NET में चाहता था ... तो यहां मैंने जो कुछ रखा है उसका एक मैला सी # संस्करण है।

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using iTextSharp.text.pdf.parser; 

namespace PdfHelper 
{ 
    /// <summary> 
    /// Taken from http://www.java-frameworks.com/java/itext/com/itextpdf/text/pdf/parser/LocationTextExtractionStrategy.java.html 
    /// </summary> 
    class LocationTextExtractionStrategyEx : LocationTextExtractionStrategy 
    { 
     private List<TextChunk> m_locationResult = new List<TextChunk>(); 
     private List<TextInfo> m_TextLocationInfo = new List<TextInfo>(); 
     public List<TextChunk> LocationResult 
     { 
      get { return m_locationResult; } 
     } 
     public List<TextInfo> TextLocationInfo 
     { 
      get { return m_TextLocationInfo; } 
     } 

     /// <summary> 
     /// Creates a new LocationTextExtracationStrategyEx 
     /// </summary> 
     public LocationTextExtractionStrategyEx() 
     { 
     } 

     /// <summary> 
     /// Returns the result so far 
     /// </summary> 
     /// <returns>a String with the resulting text</returns> 
     public override String GetResultantText() 
     { 
      m_locationResult.Sort(); 

      StringBuilder sb = new StringBuilder(); 
      TextChunk lastChunk = null; 
      TextInfo lastTextInfo = null; 
      foreach (TextChunk chunk in m_locationResult) 
      { 
       if (lastChunk == null) 
       { 
        sb.Append(chunk.Text); 
        lastTextInfo = new TextInfo(chunk); 
        m_TextLocationInfo.Add(lastTextInfo); 
       } 
       else 
       { 
        if (chunk.sameLine(lastChunk)) 
        { 
         float dist = chunk.distanceFromEndOf(lastChunk); 

         if (dist < -chunk.CharSpaceWidth) 
         { 
          sb.Append(' '); 
          lastTextInfo.addSpace(); 
         } 
         //append a space if the trailing char of the prev string wasn't a space && the 1st char of the current string isn't a space 
         else if (dist > chunk.CharSpaceWidth/2.0f && chunk.Text[0] != ' ' && lastChunk.Text[lastChunk.Text.Length - 1] != ' ') 
         { 
          sb.Append(' '); 
          lastTextInfo.addSpace(); 
         } 
         sb.Append(chunk.Text); 
         lastTextInfo.appendText(chunk); 
        } 
        else 
        { 
         sb.Append('\n'); 
         sb.Append(chunk.Text); 
         lastTextInfo = new TextInfo(chunk); 
         m_TextLocationInfo.Add(lastTextInfo); 
        } 
       } 
       lastChunk = chunk; 
      } 
      return sb.ToString(); 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="renderInfo"></param> 
     public override void RenderText(TextRenderInfo renderInfo) 
     { 
      LineSegment segment = renderInfo.GetBaseline(); 
      TextChunk location = new TextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth(), renderInfo.GetAscentLine(), renderInfo.GetDescentLine()); 
      m_locationResult.Add(location); 
     } 

     public class TextChunk : IComparable, ICloneable 
     { 
      string m_text; 
      Vector m_startLocation; 
      Vector m_endLocation; 
      Vector m_orientationVector; 
      int m_orientationMagnitude; 
      int m_distPerpendicular; 
      float m_distParallelStart; 
      float m_distParallelEnd; 
      float m_charSpaceWidth; 

      public LineSegment AscentLine; 
      public LineSegment DecentLine; 

      public object Clone() 
      { 
       TextChunk copy = new TextChunk(m_text, m_startLocation, m_endLocation, m_charSpaceWidth, AscentLine, DecentLine); 
       return copy; 
      } 

      public string Text 
      { 
       get { return m_text; } 
       set { m_text = value; } 
      } 
      public float CharSpaceWidth 
      { 
       get { return m_charSpaceWidth; } 
       set { m_charSpaceWidth = value; } 
      } 
      public Vector StartLocation 
      { 
       get { return m_startLocation; } 
       set { m_startLocation = value; } 
      } 
      public Vector EndLocation 
      { 
       get { return m_endLocation; } 
       set { m_endLocation = value; } 
      } 

      /// <summary> 
      /// Represents a chunk of text, it's orientation, and location relative to the orientation vector 
      /// </summary> 
      /// <param name="txt"></param> 
      /// <param name="startLoc"></param> 
      /// <param name="endLoc"></param> 
      /// <param name="charSpaceWidth"></param> 
      public TextChunk(string txt, Vector startLoc, Vector endLoc, float charSpaceWidth, LineSegment ascentLine, LineSegment decentLine) 
      { 
       m_text = txt; 
       m_startLocation = startLoc; 
       m_endLocation = endLoc; 
       m_charSpaceWidth = charSpaceWidth; 
       AscentLine = ascentLine; 
       DecentLine = decentLine; 

       m_orientationVector = m_endLocation.Subtract(m_startLocation).Normalize(); 
       m_orientationMagnitude = (int)(Math.Atan2(m_orientationVector[Vector.I2], m_orientationVector[Vector.I1]) * 1000); 

       // see http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html 
       // the two vectors we are crossing are in the same plane, so the result will be purely 
       // in the z-axis (out of plane) direction, so we just take the I3 component of the result 
       Vector origin = new Vector(0, 0, 1); 
       m_distPerpendicular = (int)(m_startLocation.Subtract(origin)).Cross(m_orientationVector)[Vector.I3]; 

       m_distParallelStart = m_orientationVector.Dot(m_startLocation); 
       m_distParallelEnd = m_orientationVector.Dot(m_endLocation); 
      } 

      /// <summary> 
      /// true if this location is on the the same line as the other text chunk 
      /// </summary> 
      /// <param name="textChunkToCompare">the location to compare to</param> 
      /// <returns>true if this location is on the the same line as the other</returns> 
      public bool sameLine(TextChunk textChunkToCompare) 
      { 
       if (m_orientationMagnitude != textChunkToCompare.m_orientationMagnitude) return false; 
       if (m_distPerpendicular != textChunkToCompare.m_distPerpendicular) return false; 
       return true; 
      } 

      /// <summary> 
      /// Computes the distance between the end of 'other' and the beginning of this chunk 
      /// in the direction of this chunk's orientation vector. Note that it's a bad idea 
      /// to call this for chunks that aren't on the same line and orientation, but we don't 
      /// explicitly check for that condition for performance reasons. 
      /// </summary> 
      /// <param name="other"></param> 
      /// <returns>the number of spaces between the end of 'other' and the beginning of this chunk</returns> 
      public float distanceFromEndOf(TextChunk other) 
      { 
       float distance = m_distParallelStart - other.m_distParallelEnd; 
       return distance; 
      } 

      /// <summary> 
      /// Compares based on orientation, perpendicular distance, then parallel distance 
      /// </summary> 
      /// <param name="obj"></param> 
      /// <returns></returns> 
      public int CompareTo(object obj) 
      { 
       if (obj == null) throw new ArgumentException("Object is now a TextChunk"); 

       TextChunk rhs = obj as TextChunk; 
       if (rhs != null) 
       { 
        if (this == rhs) return 0; 

        int rslt; 
        rslt = m_orientationMagnitude - rhs.m_orientationMagnitude; 
        if (rslt != 0) return rslt; 

        rslt = m_distPerpendicular - rhs.m_distPerpendicular; 
        if (rslt != 0) return rslt; 

        // note: it's never safe to check floating point numbers for equality, and if two chunks 
        // are truly right on top of each other, which one comes first or second just doesn't matter 
        // so we arbitrarily choose this way. 
        rslt = m_distParallelStart < rhs.m_distParallelStart ? -1 : 1; 

        return rslt; 
       } 
       else 
       { 
        throw new ArgumentException("Object is now a TextChunk"); 
       } 
      } 
     } 

     public class TextInfo 
     { 
      public Vector TopLeft; 
      public Vector BottomRight; 
      private string m_Text; 

      public string Text 
      { 
       get { return m_Text; } 
      } 

      /// <summary> 
      /// Create a TextInfo. 
      /// </summary> 
      /// <param name="initialTextChunk"></param> 
      public TextInfo(TextChunk initialTextChunk) 
      { 
       TopLeft = initialTextChunk.AscentLine.GetStartPoint(); 
       BottomRight = initialTextChunk.DecentLine.GetEndPoint(); 
       m_Text = initialTextChunk.Text; 
      } 

      /// <summary> 
      /// Add more text to this TextInfo. 
      /// </summary> 
      /// <param name="additionalTextChunk"></param> 
      public void appendText(TextChunk additionalTextChunk) 
      { 
       BottomRight = additionalTextChunk.DecentLine.GetEndPoint(); 
       m_Text += additionalTextChunk.Text; 
      } 

      /// <summary> 
      /// Add a space to the TextInfo. This will leave the endpoint out of sync with the text. 
      /// The assumtion is that you will add more text after the space which will correct the endpoint. 
      /// </summary> 
      public void addSpace() 
      { 
       m_Text += ' '; 
      } 


     } 
    } 
} 

मैं एक TextLocationInfo संपत्ति जो वापस (दाएं ऊपरी बाएँ और निचले) उन पंक्तियों के लिए पाठ की तर्ज + coords की एक सूची है जो आप बाउंडिंग बॉक्स देने के लिए इस्तेमाल किया जा सकता हाथ जोड़ा।

मैंने अपने प्रारंभिक खेल के साथ कुछ अजीब भी देखा।यह देखा है कि अगर मैं आधारभूत से StartPoint & endpoint खींच लिया है कि मैं एक ही coords मिला (मुझे लगता है कि ऐसा करना सही है, और मैंने सोचा, ascentLine और DecentLine से उन बिंदुओं को खींचने के लिए किया गया था)। मेरा प्रारंभिक पास मैंने बस आधार रेखा का उपयोग किया। अजीब बात है कि मुझे परिणामी कॉर्ड में कोई फर्क नहीं पड़ता। तो सावधान करने के लिए शब्द ... मुझे यकीन है कि अगर coords मैं प्रदान कर रहा हूँ ठीक कह रहे हैं नहीं कर रहा हूँ ... मैं तो बस लगता है कि वे कर रहे हैं/होना चाहिए। अगर आप उन्हें एक वाणिज्यिक आवेदन में उपयोग कर रहे हैं

+0

हाय, आपकी कक्षा अविश्वसनीय रूप से सहायक लगती है। टेक्स्ट नाम के आधार पर पीडीएफ में नामित गंतव्यों को जोड़ने के लिए मैं इसका उपयोग कैसे कर सकता हूं? – Jason

+0

मुझे यकीन नहीं है कि नामित गंतव्य क्या हैं। मैं इतना है कि यह भी एक 'नामित गंतव्य' बनाता है इससे पहले कि यह m_TextLocationInfo को संलग्न कर देता है आप GetResultantText को संशोधित कर सकता है अनुमान लगा रहा हूँ। या आप बस वर्ग इस्तेमाल कर सकते हैं पाठ और स्थानों पार्स और फिर पाठ स्थानों की सूची पर पुनरावृति और प्रत्येक के लिए एक 'नामित गंतव्य' बनाने के लिए के रूप में है। – greenhat

+0

क्या यह आसानी से प्रत्येक चरित्र के लिए पद देने के लिए बदला जा सकता है? – d456