title: I need to extract text from a pdf file using itext7 or itextsharp and put html tag for bold around all the words using bold font
body: <p>I am using iText7 and I want to extract all the texts from a pdf and put html tag for bold ( &lt;b&gt;...&lt;/b&gt; ) around all the words that uses bold fonts and save it in text file. Any pointers? I am able to independently extract text and also extract all the bold words but not able to co-relate the two.
  Here is the code snippet I am using for extracting the text:</p>
  <pre><code>PdfDocument MyDocument = new PdfDocument(new PdfReader(&quot;C:\\MyTest.pdf&quot;));
  string MyText = PdfTextExtractor.GetTextFromPage(MyDocument.GetPage(1), new 
  SimpleTextExtractionStrategy());
  </code></pre>
  <p>Here is the code I am using for extracting all the words using the bold font:</p>
  <pre><code>MyRectangle = new Rectangle(0, 0, 50, 100);
  CustomFontFilter fontFilter = new CustomFontFilter(MyRectangle);
  FilteredEventListener listener = new FilteredEventListener();
  LocationTextExtractionStrategy extractionStrategy = 
  listener.AttachEventListener(new LocationTextExtractionStrategy(), fontFilter);
  PdfCanvasProcessor parser = new PdfCanvasProcessor(listener);
  parser.ProcessPageContent(MyDocument.GetPage(1));
  String MyBoldTextList = extractionStrategy.GetResultantText();
  //------
  class CustomFontFilter : TextRegionEventFilter
  {
      public CustomFontFilter(iText.Kernel.Geom.Rectangle filterRect) : base(filterRect){ }
      override public bool Accept(IEventData data, EventType type)
      {
          if (type == EventType.RENDER_TEXT){
              TextRenderInfo renderInfo = (TextRenderInfo)data;
              PdfFont font = renderInfo.GetFont();
              if (font!=null)
                  return font.GetFontProgram().GetFontNames().GetFontName().Contains(&quot;Bold&quot;);
          }
          return false;
      }
  }
  </code></pre>
  <p>The problem is that the pdf in question here is a multi-column document. SimpleTextExtractionStrategy brings the text in perfect order but if I use the LocationStrategy, it messes up texts by jumping from one column to next column in each line. I am not able to find any way to get the list of bold words using SimpleTextExtractionStrategy. In LocationStrategy, the list that I get is not in the right order so I am unable to co-relate it.</p>