Extract text and text rectangle coordinates from a Pdf file using itextsharp

后端 未结 2 1370
清歌不尽
清歌不尽 2020-12-05 16:33

I\'m trying to get all words and their location coordinates from a PDF file. I\'ve succeeded using the Acrobat API on .NET. Now, I\'m trying to get the same res

2条回答
  •  长情又很酷
    2020-12-05 17:17

    My account is too new reply to Mark Storer's answer.

    I wasn't able to directly use the LocationTextExtracationStrategy (I think I must be doing something wrong). When I used the LocationTextExtracationStrategy I was able to get the text but I couldn't figure out how to get the coords for each string (or line of strings).

    I ended up subclassing the LocationTextExtracationStrategy and exposing the data I wanted because it does have it internally.

    I also wanted it in .net... so here is a sloppy C# version of what I put together.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using iTextSharp.text.pdf.parser;
    
    namespace PdfHelper
    {
        /// 
        /// Taken from http://www.java-frameworks.com/java/itext/com/itextpdf/text/pdf/parser/LocationTextExtractionStrategy.java.html
        /// 
        class LocationTextExtractionStrategyEx : LocationTextExtractionStrategy
        {
            private List m_locationResult = new List();
            private List m_TextLocationInfo = new List();
            public List LocationResult 
            {
                get { return m_locationResult; }
            }
            public List TextLocationInfo
            {
                get { return m_TextLocationInfo; }
            }
    
            /// 
            /// Creates a new LocationTextExtracationStrategyEx
            /// 
            public LocationTextExtractionStrategyEx()
            {
            }
    
            /// 
            /// Returns the result so far
            /// 
            /// a String with the resulting text
            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();
            }
    
            /// 
            /// 
            /// 
            /// 
            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; }
                }
    
                /// 
                /// Represents a chunk of text, it's orientation, and location relative to the orientation vector
                /// 
                /// 
                /// 
                /// 
                /// 
                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);
                }
    
                /// 
                /// true if this location is on the the same line as the other text chunk
                /// 
                /// the location to compare to
                /// true if this location is on the the same line as the other
                public bool sameLine(TextChunk textChunkToCompare)
                {
                    if (m_orientationMagnitude != textChunkToCompare.m_orientationMagnitude) return false;
                    if (m_distPerpendicular != textChunkToCompare.m_distPerpendicular) return false;
                    return true;
                }
    
                /// 
                /// 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.
                /// 
                /// 
                /// the number of spaces between the end of 'other' and the beginning of this chunk
                public float distanceFromEndOf(TextChunk other)
                {
                    float distance = m_distParallelStart - other.m_distParallelEnd;
                    return distance;
                }
    
                /// 
                /// Compares based on orientation, perpendicular distance, then parallel distance
                /// 
                /// 
                /// 
                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; }
                }
    
                /// 
                /// Create a TextInfo.
                /// 
                /// 
                public TextInfo(TextChunk initialTextChunk)
                {
                    TopLeft = initialTextChunk.AscentLine.GetStartPoint();
                    BottomRight = initialTextChunk.DecentLine.GetEndPoint();
                    m_Text = initialTextChunk.Text;
                }
    
                /// 
                /// Add more text to this TextInfo.
                /// 
                /// 
                public void appendText(TextChunk additionalTextChunk)
                {
                    BottomRight = additionalTextChunk.DecentLine.GetEndPoint();
                    m_Text += additionalTextChunk.Text;
                }
    
                /// 
                /// 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.
                /// 
                public void addSpace()
                {
                    m_Text += ' ';
                }
    
    
            }
        }
    }
    

    I added a TextLocationInfo property which hands back a List of lines of text + the coords for those lines (upper left and lower right) which can be used to give you a bounding box.

    I also saw something odd with my initial playing around. It looked like I got the same coords if I pulled the startPoint & endPoint from the baseline (I think the right thing to do, and the thing I did, was to pull those points from ascentLine and DecentLine). My initial pass I just used the baseline. Odd that I didn't see a difference in the resulting coords. So word to the wary... I'm not sure if the coords I'm providing are right... I just think they are/should be.

提交回复
热议问题