需要一种在PDF格式中使用背景颜色突出显示特定文本的解决方案,并且最终结果应在新的pdf文件中。.我尝试了几种基于itextSharp的解决方案,但到目前为止还没有运气。您能帮我解决这个问题吗?
private static void PDFTextGetter(string pSearch, StringComparison sc, string sourceFile, string destinationFile)
{
PdfStamper stamper;
PdfContentByte cb;
var pReader = new PdfReader(sourceFile);
using (FileStream fs = new FileStream(destinationFile, FileMode.Create))
{
stamper = new PdfStamper(pReader, fs);
for (int page = 1; page <= pReader.NumberOfPages; page++)
{
var strategy = new myLocationTextExtractionStrategy();
cb = stamper.GetUnderContent(page);
strategy.UndercontentCharacterSpacing = cb.CharacterSpacing;
strategy.UndercontentHorizontalScaling = cb.HorizontalScaling;
string currentText = PdfTextExtractor.GetTextFromPage(pReader, page, strategy);
List<iTextSharp.text.Rectangle> matchesFound = strategy.GetTextLocations(pSearch, sc);
cb.SetColorFill(BaseColor.YELLOW);
foreach (iTextSharp.text.Rectangle rect in matchesFound)
cb.Rectangle(rect.Left, rect.Bottom, rect.Width, rect.Height);
cb.Fill();
}
stamper.Close();
pReader.Close();
}
}