2011-01-02 43 views
5

Tôi đang cố gắng thu thập tất cả các từ và tọa độ vị trí của chúng từ một tệp PDF. Tôi đã thành công khi sử dụng Acrobat API trên .NET. Bây giờ, tôi đang cố gắng để có được kết quả tương tự bằng cách sử dụng một API miễn phí, chẳng hạn như iTextSharp (phiên bản .NET). Tôi có thể nhận được văn bản (từng dòng) với PRTokeniser, nhưng tôi không có ý tưởng làm thế nào để có được tọa độ của dòng, hãy để một mình của mỗi từ.Trích xuất văn bản và văn bản hình chữ nhật tọa độ từ tệp Pdf bằng itextsharp

+3

iText và iTextSharp không miễn phí nếu bạn đang sử dụng chúng trong một ứng dụng thương mại. –

Trả lời

9

Bạn sẽ muốn sử dụng các lớp gói com.itextpdf.text.pdf.parser. Họ theo dõi chuyển đổi, màu sắc, phông chữ, v.v. hiện tại.

Đáng buồn thay, các lớp này không được đề cập trong sách mới, vì vậy, bạn còn lại với JavaDoc và chuyển đổi tinh thần tất cả từ Java sang C#, không quá căng.

Vì vậy, bạn sẽ muốn cắm một số LocationTextExtractionStrategy vào PdfTextExtractor.

Điều này sẽ cung cấp cho bạn các chuỗi và vị trí khi chúng được trình bày trong pdf. Nó sẽ được vào bạn để giải thích rằng như là từ (và đoạn văn nếu cần thiết, ouch).

Hãy nhớ rằng PDF không biết gì về bố cục văn bản. Mỗi nhân vật có thể được đặt riêng lẻ. Nếu ai đó có khuynh hướng như vậy (và họ sẽ phải là một vài tacos ngắn của một đĩa kết hợp để làm như vậy) họ có thể vẽ tất cả các 'a trên một trang nhất định, sau đó tất cả các' b, và vv.

Thực tế hơn, ai đó có thể vẽ tất cả văn bản trên trang sử dụng FontA, sau đó mọi thứ cho FontB, v.v. Điều này có thể tạo ra các luồng nội dung hiệu quả hơn. Hãy ghi nhớ rằng nghiêngđậm (và đậm nghiêng) là tất cả các phông chữ riêng biệt. Nếu ai đó chỉ đánh dấu một phần của một từ là đậm (hoặc bất kỳ thứ gì), thì từ logic đó bắt buộc phải được chia thành ít nhất hai lệnh vẽ.

Nhưng nhiều người chỉ cần viết văn bản của họ thành PDF theo thứ tự logic ... rất tiện dụng cho những người đang cố gắng phân tích cú pháp, nhưng bạn không phải mong đợi nó. Bởi vì bạn sẽ luôn luôn chạy vào một số kỳ quặc mà không.

11

Tài khoản của tôi trả lời quá mới cho câu trả lời của Mark Storer.

Tôi không thể trực tiếp sử dụng LocationTextExtracationStrategy (Tôi nghĩ mình phải làm điều gì đó sai). Khi tôi sử dụng các LocationTextExtracationStrategy tôi đã có thể nhận được văn bản nhưng tôi không thể tìm ra cách để có được các coords cho mỗi chuỗi (hoặc dòng dây).

Tôi đã kết thúc phân lớp địa chỉ LocationTextExtracationStrategy và hiển thị dữ liệu tôi muốn vì nó có nội bộ trong đó.

Tôi cũng muốn nó trong .net ... vì vậy đây là một phiên bản C# cẩu thả của những gì tôi đặt lại với nhau.

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 += ' '; 
      } 


     } 
    } 
} 

tôi đã thêm một tài sản TextLocationInfo mà trao lại một danh sách các dòng văn bản + các coords cho những dòng (phía trên bên trái và bên phải phía dưới) mà có thể được sử dụng để cung cấp cho bạn một hộp bounding.

Tôi cũng thấy điều gì đó kỳ lạ với lần đầu tiên tôi chơi xung quanh.Có vẻ như tôi có cùng coords nếu tôi kéo điểm bắt đầu của điểm số startPoint & từ đường cơ sở (tôi nghĩ điều phải làm và điều tôi đã làm là kéo các điểm đó từ ascentLine và DecentLine). Vượt qua ban đầu của tôi, tôi chỉ sử dụng đường cơ sở. Odd rằng tôi đã không thấy một sự khác biệt trong các coords kết quả. Vì vậy, từ để cảnh giác ... Tôi không chắc chắn nếu các hợp âm tôi đang cung cấp là đúng ... Tôi chỉ nghĩ rằng họ đang/nên.

+0

Xin chào, Lớp học của bạn trông cực kỳ hữu ích. Làm thế nào tôi có thể sử dụng điều này để thêm các điểm đến được đặt tên vào một tệp PDF dựa trên vị trí văn bản được tìm thấy? – Jason

+0

Tôi không chắc chắn các điểm đến được đặt tên là gì. Tôi đoán bạn có thể sửa đổi GetResultantText để nó cũng tạo ra một 'tên đích' trước khi nó thêm vào m_TextLocationInfo. Hoặc bạn chỉ có thể sử dụng lớp như là phân tích văn bản & vị trí và sau đó lặp qua danh sách các vị trí văn bản và tạo một 'điểm đến được đặt tên' cho mỗi vị trí. – greenhat

+0

Điều này có thể dễ dàng thay đổi để đưa ra các vị trí cho mỗi nhân vật không? – d456

Các vấn đề liên quan