当前位置:   article > 正文

C# 使用itextsharp 读取pdf中文字坐标_c# pdfreadercontentparser

c# pdfreadercontentparser
  1. using iTextSharp.text.pdf;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace TestIText
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. PdfReader readerTemp = new PdfReader(@"D:\.pdf");
  14. PdfHelper.LocationTextExtractionStrategyEx pz = new PdfHelper.LocationTextExtractionStrategyEx();
  15. iTextSharp.text.pdf.parser.PdfReaderContentParser p = new iTextSharp.text.pdf.parser.PdfReaderContentParser(readerTemp);
  16. p.ProcessContent<PdfHelper.LocationTextExtractionStrategyEx>(1, pz);
  17. Console.WriteLine(pz.GetResultantText());
  18. Console.ReadLine();
  19. }
  20. }
  21. }

PdfHelper

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using iTextSharp.text.pdf.parser;
  6. namespace PdfHelper
  7. {
  8. /// <summary>
  9. /// Taken from http://www.java-frameworks.com/java/itext/com/itextpdf/text/pdf/parser/LocationTextExtractionStrategy.java.html
  10. /// </summary>
  11. class LocationTextExtractionStrategyEx : LocationTextExtractionStrategy
  12. {
  13. private List<TextChunk> m_locationResult = new List<TextChunk>();
  14. private List<TextInfo> m_TextLocationInfo = new List<TextInfo>();
  15. public List<TextChunk> LocationResult
  16. {
  17. get { return m_locationResult; }
  18. }
  19. public List<TextInfo> TextLocationInfo
  20. {
  21. get { return m_TextLocationInfo; }
  22. }
  23. /// <summary>
  24. /// Creates a new LocationTextExtracationStrategyEx
  25. /// </summary>
  26. public LocationTextExtractionStrategyEx()
  27. {
  28. }
  29. /// <summary>
  30. /// Returns the result so far
  31. /// </summary>
  32. /// <returns>a String with the resulting text</returns>
  33. public override String GetResultantText()
  34. {
  35. m_locationResult.Sort();
  36. StringBuilder sb = new StringBuilder();
  37. TextChunk lastChunk = null;
  38. TextInfo lastTextInfo = null;
  39. foreach (TextChunk chunk in m_locationResult)
  40. {
  41. if (lastChunk == null)
  42. {
  43. sb.Append(chunk.Text);
  44. lastTextInfo = n
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/1018988
推荐阅读
  

闽ICP备14008679号