Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Jul 16, 2019 8:27 am

Hello,

In a PDF I can find and highlight selected text like so:

Code: Select all
foreach (PdfPageBase page in pdf.Pages)
{
    result = page.FindText("Youth").Finds;
    foreach (PdfTextFind find in result)
    {
        find.ApplyHighLight();
    }
}


But how can I find what font and fontsize is used in the selected text (in this example for text "Youth" ) ?
So something like "find.Font" which will give me "Arial 12" or simlar ?

Thank you

Heinz4711
 
Posts: 20
Joined: Thu Feb 07, 2019 5:22 pm

Tue Jul 16, 2019 9:46 am

Hello,

Thanks for your inquiry.
Sorry to tell your that Spire.PDF doesn't support getting the font information for selected text at present. If this can be achieved in the future, I will let you know.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Apr 24, 2024 5:42 am

Heinz4711 wrote:Hello,

In a PDF I can find and highlight selected text like so:

Code: Select all
foreach (PdfPageBase page in pdf.Pages)
{
    result = page.FindText("Youth").Finds;
    foreach (PdfTextFind find in result)
    {
        find.ApplyHighLight();
    }
}


But how can I find what font and fontsize is used in the selected text (in this example for text "Youth" ) ?
So something like "find.Font" which will give me "Arial 12" or simlar ?

Thank you





Hi,

Thank you for your patience.

I'm glad to inform you that our Spire.PDF has supported retrieving font formatting of found text. Note: Only supports retrieving bold, faux bold (font style set to fill and stroke), italic, and color; does not support retrieving underline.

Welcome to download and test [Spire.PDF Pack(Hot Fix) Version:10.4.7].
Our website link:https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
NuGet link: https://www.nuget.org/packages/Spire.PDF

Code: Select all
  PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(inputFile);
            PdfPageBase page = pdf.Pages[0];
            PdfTextFinder finds = new PdfTextFinder(page);
            finds.Options.Parameter = TextFindParameter.None;
            List<PdfTextFragment> result = finds.Find("hello");
            StringBuilder str = new StringBuilder();
            foreach (PdfTextFragment find in result)
            {
                string text = find.LineText;
                string FontName = find.TextStates[0].FontName;
                float FontSize = find.TextStates[0].FontSize;
                string FontFamily = find.TextStates[0].FontFamily;
                bool IsBold = find.TextStates[0].IsBold;
                bool IsSimulateBold = find.TextStates[0].IsSimulateBold;
                bool IsItalic = find.TextStates[0].IsItalic;
                Color color = find.TextStates[0].ForegroundColor;
                str.AppendLine(text);
                str.AppendLine("FontName: " + FontName);
                str.AppendLine("FontSize: " + FontSize);
                str.AppendLine("FontFamily: " + FontFamily);
                str.AppendLine("IsBold: " + IsBold);
                str.AppendLine("IsSimulateBold: " + IsSimulateBold);
                str.AppendLine("IsItalic: " + IsItalic);
                str.AppendLine("color: " + color);
                str.AppendLine(" ");
            }
            PdfTextReplacer ptr = new PdfTextReplacer(page);
            ptr.ReplaceAllText("hello", "New");
            File.WriteAllText(outputFile_T, str.ToString());
            pdf.SaveToFile(outputFile);
            pdf.Dispose();


If you have any questions, please let us know.

Sincerely,
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF

cron