Text

Text (20)

Monday, 23 September 2019 09:49

Find and replace text on PDF document in C#

Written by support iceblue

Spire.PDF supports to find and replace the text in all the pages of PDF document. This article is going to show you how to replace text in the first page of PDF document in C#.

Step 1: Load the sample document file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("SearchReplaceTemplate.pdf");

Step 2: Searched the text “Spire.PDF for .NET” from the first page of the sample document.

PdfPageBase page = doc.Pages[0];
PdfTextFindCollection collection = page.FindText("Spire.PDF for .NET", TextFindParameter.IgnoreCase);

Step 3: Use the new string “E-iceblue Spire.PDF” to replace the searched text and sent the font and color for the new string.

String newText = "E-iceblue Spire.PDF";
//Creates a brush
PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
//Defines a font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular));

RectangleF rec;
foreach (PdfTextFind find in collection.Finds)
{
    // Gets the bound of the found text in page
    rec = find.Bounds;
    page.Canvas.DrawRectangle(PdfBrushes.White, rec);
    page.Canvas.DrawString(newText, font, brush, rec);

}

Step 4: Save the document to file.

doc.SaveToFile("ReplaceAllSearchedText_out.pdf");

Sample PDF document:

Find and replace text on PDF document in C#

Effective screenshot after replaced the searched text on PDF.

Find and replace text on PDF document in C#

Wednesday, 18 September 2019 06:33

C# Add Tooltip to the Searched Text on PDF

Written by support iceblue

This article demonstrates how to add tooltip to the text on an existing PDF document in C#. Spire.PDF for .NET supports to create tooltips by adding invisible button over the searched text from the PDF file.

Step 1: Load the sample document file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Sample.pdf");

Step 2: Searched the text “Add tooltip to PDF” from the first page of the sample document and get the position of it.

PdfPageBase page = doc.Pages[0];
PdfTextFind[] result = page.FindText("Add tooltip to PDF").Finds;
RectangleF rec = result[0].Bounds;            

Step 3: Create invisible button on text position

PdfButtonField field1 = new PdfButtonField(page, "field1");
field1.Bounds = rec;

Step 4: Set the content and format for the tooltip field.

field1.ToolTip = "E-iceblue Co. Ltd., a vendor of .NET, Java and WPF development components";
field1.BorderWidth = 0;
field1.BackColor = Color.Transparent;
field1.ForeColor = Color.Transparent;
field1.LayoutMode = PdfButtonLayoutMode.IconOnly;
field1.IconLayout.IsFitBounds = true;

Step 5: Save the document to file.

doc.SaveToFile("Addtooltip.pdf", FileFormat.PDF);

Effective screenshot after adding the tooltip to PDF:

C# add tooltip to the searched text on PDF

using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.General.Find;
using System.Drawing;

namespace TooltipPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("Sample.pdf");

            PdfPageBase page = doc.Pages[0];
            PdfTextFind[] result = page.FindText("Add tooltip to PDF").Finds;
            RectangleF rec = result[0].Bounds;            

            PdfButtonField field1 = new PdfButtonField(page, "field1");
            field1.Bounds = rec;

            field1.ToolTip = "E-iceblue Co. Ltd., a vendor of .NET, Java and WPF development components";
            field1.BorderWidth = 0;
            field1.BackColor = Color.Transparent;
            field1.ForeColor = Color.Transparent;
            field1.LayoutMode = PdfButtonLayoutMode.IconOnly;
            field1.IconLayout.IsFitBounds = true;

            doc.SaveToFile("Addtooltip.pdf", FileFormat.PDF);
     
        }
    }
}

Some long text, such as a phrase or a sentence, is likely to span two or more lines. When we try to highlight it, it’s required to be able to detect the whole phrase or sentence even if it spans two lines. This article is going to introduce how to find and highlight long text spanning multiple lines by using Spire.PDF with C# and VB.NET.

Code Snippets

[C#]
//load the sample PDF file
PdfDocument doc = new PdfDocument(@"C:\Users\Administrator\Desktop\Hello.pdf");

//find all matching strings from the first page, even if some of them span multiple lines
PdfTextFind[] findResults = doc.Pages[0].FindText("Besides, Spire.PDF for JAVA can be applied easily to convert PDF to XPS, XPS to PDF, PDF to SVG, and PDF to PDF/A in high quality.", TextFindParameter.CrossLine).Finds;

//highlight the first search result
findResults[0].ApplyHighLight(Color.Yellow);

//save to file
doc.SaveToFile("output.pdf", FileFormat.PDF);
[VB.NET]
'load the sample PDF file
Dim doc As PdfDocument =  New PdfDocument("C:\Users\Administrator\Desktop\Hello.pdf") 
 
'find all matching strings from the first page, even if some of them span multiple lines
Dim findResults() As PdfTextFind =  doc.Pages(0).FindText("Besides, Spire.PDF for JAVA can be applied easily to convert PDF to XPS, XPS to PDF, PDF to SVG, and PDF to PDF/A in high quality.",TextFindParameter.CrossLine).Finds 
 
'highlight the first search result
findResults(0).ApplyHighLight(Color.Yellow)
 
'save to file
doc.SaveToFile("output.pdf", FileFormat.PDF)

Output

Find and Highlight Text Spanning Multiple Lines in PDF in C#, VB.NET

Wednesday, 05 December 2018 08:56

Draw Superscript and Subscript Text in PDF in C#

Written by support iceblue

In this article, we're going to demonstrate how to draw superscript and subscript text in PDF using Spire.PDF.

Draw Superscript Text

//Instantiate a PdfDocument instance
PdfDocument pdf = new PdfDocument();

//Add a page
PdfPageBase page = pdf.Pages.Add();

//Set initial (x, y) coordinate
float x = 0;
float y = 50;

//Set font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);

//Draw string
string text = "Sample Text";
page.Canvas.DrawString(text, font, PdfBrushes.Black, new PointF(x,y));

//Measure the string
SizeF size = font.MeasureString(text);

//Set the x coordinate of the superscript text
x += size.Width;

//Instantiate a PdfStringFormat instance
PdfStringFormat format = new PdfStringFormat();
//Set format as superscript
format.SubSuperScript = PdfSubSuperScript.SuperScript;

//Draw superscript text with format
text = "Superscript";
page.Canvas.DrawString(text, font, PdfBrushes.Black, new PointF(x,y), format);

//Save the document
pdf.SaveToFile("SuperScript.pdf");

Draw Superscript and Subscript Text in PDF in C#

Draw Superscript Text

//Instantiate a PdfDocument instance
PdfDocument pdf = new PdfDocument();

//Add a page
PdfPageBase page = pdf.Pages.Add();

//Set initial (x, y) coordinate
float x = 0;
float y = 50;

//Set font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);

//Draw string
string text = "Sample Text";
page.Canvas.DrawString(text, font, PdfBrushes.Black, new PointF(x, y));

//Measure the string
SizeF size = font.MeasureString(text);

//Set the x coordinate of the subscript text
x += size.Width;

//Instantiate a PdfStringFormat instance
PdfStringFormat format = new PdfStringFormat();
//Set format as subscript
format.SubSuperScript = PdfSubSuperScript.SubScript;

//Draw subscript
text = "Subscript";
page.Canvas.DrawString(text, font, PdfBrushes.Black, new PointF(x, y), format);

//Save the document
pdf.SaveToFile("SubScript.pdf");

Draw Superscript and Subscript Text in PDF in C#

Friday, 10 March 2017 02:26

How to Create Two Column PDF in C#, VB.NET

Written by support iceblue

It's impossible to format an existing PDF document with columns due to its fixed layout. It is possible to create columns in a PDF document so that text appears as it does in a newspaper when creating PDFs from scratch.

This article presents how we can add columns of text to PDF documents by using Spire.PDF.

Step 1: Initialize an instance of PdfDocument class, add a page to it.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Declare and initialize two string variables.

string s1 = "Spire.PDF for .NET is a professional PDF component applied to creating, writing, "
            + "editing, handling and reading PDF files without any external dependencies within "
            + ".NET application. Using this .NET PDF library, you can implement rich capabilities "
            + "to create PDF files from scratch or process existing PDF documents entirely through "
            + "C#/VB.NET without installing Adobe Acrobat.";

string s2 = "Many rich features can be supported by the .NET PDF API, such as security setting "
            +"(including digital signature), PDF text/ attachment/ image extract, PDF merge/ split "
            +", metadata update, section, graph/ image drawing and inserting, table creation and "
            +"processing, and importing data etc.Besides, Spire.PDF for .NET can be applied to easily "
            +"converting Text, Image and HTML to PDF with C#/VB.NET in high quality.";

Step 3: Create a PdfFont instance.

PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f);

Step 4: Draw text of s1 in the first column.

page.Canvas.DrawString(s1, font, PdfBrushes.Black, new RectangleF(0, 20, page.GetClientSize().Width / 2 - 2f, page.GetClientSize().Height));

Step 5: Draw text of s2 in the second column.

page.Canvas.DrawString(s2, font, PdfBrushes.Black, new RectangleF(page.GetClientSize().Width / 2 + 2f, 20, page.GetClientSize().Width / 2, page.GetClientSize().Height));

Step 6: Save the file.

doc.SaveToFile("Output.pdf");

Result:

How to Create Two Column PDF in C#, VB.NET

Full Code:

[C#]
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

string s1 = "Spire.PDF for .NET is a professional PDF component applied to creating, writing, "
            + "editing, handling and reading PDF files without any external dependencies within "
            + ".NET application. Using this .NET PDF library, you can implement rich capabilities "
            + "to create PDF files from scratch or process existing PDF documents entirely through "
            + "C#/VB.NET without installing Adobe Acrobat.";
string s2 = "Many rich features can be supported by the .NET PDF API, such as security setting "
            +"(including digital signature), PDF text/ attachment/ image extract, PDF merge/ split "
            +", metadata update, section, graph/ image drawing and inserting, table creation and "
            +"processing, and importing data etc.Besides, Spire.PDF for .NET can be applied to easily "
            +"converting Text, Image and HTML to PDF with C#/VB.NET in high quality.";

PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f);
page.Canvas.DrawString(s1, font, PdfBrushes.Black, new RectangleF(0, 20, page.GetClientSize().Width / 2 - 2f, page.GetClientSize().Height));
page.Canvas.DrawString(s2, font, PdfBrushes.Black, new RectangleF(page.GetClientSize().Width / 2 + 2f, 20, page.GetClientSize().Width / 2, page.GetClientSize().Height));

doc.SaveToFile("Output.pdf");
[VB.NET]
Dim doc As PdfDocument = New PdfDocument()
Dim page As PdfPageBase = doc.Pages.Add()

String s1 = "Spire.PDF for .NET is a professional PDF component applied to creating, writing, "
            + "editing, handling and reading PDF files without any external dependencies within "
            + ".NET application. Using this .NET PDF library, you can implement rich capabilities "
            + "to create PDF files from scratch or process existing PDF documents entirely through "
            Dim "C#/VB.NET without installing Adobe Acrobat." As +
String s2 = "Many rich features can be supported by the .NET PDF API, such as security setting "
            + "(including digital signature), PDF text/ attachment/ image extract, PDF merge/ split "
            + ", metadata update, section, graph/ image drawing and inserting, table creation and "
            + "processing, and importing data etc.Besides, Spire.PDF for .NET can be applied to easily "
            + "converting Text, Image and HTML to PDF with C#/VB.NET in high quality."

Dim font As PdfFont = New PdfFont(PdfFontFamily.TimesRoman,12f)
page.Canvas.DrawString(s1, font, PdfBrushes.Black, New RectangleF(0, 20, page.GetClientSize().Width / 2 - 2f, page.GetClientSize().Height))
page.Canvas.DrawString(s2, font, PdfBrushes.Black, New RectangleF(page.GetClientSize().Width / 2 + 2f, 20, page.GetClientSize().Width / 2, page.GetClientSize().Height))

doc.SaveToFile("Output.pdf")

Spire.PDF provides support to render simple HTML string in a PDF document by using PdfHTMLTextElement class. This class supports a set of basic HTML tags including Font, B, I, U, Sub, Sup and BR. For complex HTML rendering with CSS, please check Convert HTML String to PDF.

Following code snippets demonstrates how we can insert HTML styled text to PDF.

Step 1: Create a new PDF document, add a page to it.

PdfDocument doc = new PdfDocument();
PdfNewPage page = doc.Pages.Add() as PdfNewPage;

Step 2: Define HTML string.

string htmlText= "This demo shows how we can insert <u><i>HTML styled text</i></u> to PDF using "
                 + "<font color='#FF4500'>Spire.PDF for .NET</font>. ";

Step 3: Render HTML text.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 5);
PdfBrush brush = PdfBrushes.Black;
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, brush);
richTextElement.TextAlign = TextAlign.Left;

Step 4: Format page layout to enable that the HTML text will break into multiple pages if the content exceeds one page.

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
format.Break = PdfLayoutBreakType.FitPage;

Step 5: Draw HTML string on page.

richTextElement.Draw(page, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format);

Step 6: Save the document.

doc.SaveToFile("Output.pdf");

Output:

How to Insert HTML Styled Text to PDF in C#, VB.NET

Full Code:

[C#]
//Create a Pdf document
PdfDocument doc = new PdfDocument();

//Add a new page
PdfNewPage page = doc.Pages.Add() as PdfNewPage;

//HTML string
string htmlText= "This demo shows how we can insert <u><i>HTML styled text</i></u> to PDF using "
                 + "<font color='#FF4500'>Spire.PDF for .NET</font>. ";

//Render HTML text
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 5);
PdfBrush brush = PdfBrushes.Black;
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, brush);
richTextElement.TextAlign = TextAlign.Left;

//Format Layout
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
format.Break = PdfLayoutBreakType.FitPage;

//Draw htmlString  
richTextElement.Draw(page, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format);
doc.SaveToFile("Output.pdf");
[VB.NET]
'Create a Pdf document
Dim doc As New PdfDocument()

'Add a new page
Dim page As PdfNewPage = TryCast(doc.Pages.Add(), PdfNewPage)

'HTML string
Dim htmlText As String = "This demo shows how we can insert <u><i>HTML styled text</i></u> to PDF using " 
    & "<font color='#FF4500'>Spire.PDF for .NET</font>. "

'Render HTML text
Dim font As New PdfFont(PdfFontFamily.Helvetica, 5)
Dim brush As PdfBrush = PdfBrushes.Black
Dim richTextElement As New PdfHTMLTextElement(htmlText, font, brush)
richTextElement.TextAlign = TextAlign.Left

'Format Layout
Dim format As New PdfMetafileLayoutFormat()
format.Layout = PdfLayoutType.Paginate
format.Break = PdfLayoutBreakType.FitPage

'Draw htmlString  
richTextElement.Draw(page, New RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format)
doc.SaveToFile("Output.pdf")
Wednesday, 22 February 2017 09:12

How to replace font(s) in PDF document

Written by support iceblue

Spire.PDF supports the functionality to replace font(s) used in PDF document. The following parts shows how we can use Spire.PDF to replace all the fonts used in an existing PDF document with another alternate font in C# and VB.NET.

Screenshot before replacing font:

How to replace font(s) in PDF document

Code snippets:

Step 1: Instantiate an object of PdfDocument class and load the PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"E:\Program Files\input.pdf");

Step 2: Use the UsedFonts attribute of PdfDocument class to get all the fonts used in the document.

PdfUsedFont[] fonts = doc.UsedFonts;

Step 3: Create a new PDF font. Loop through the fonts and call PdfUsedFont.replace() method to replace them with the new font.

PdfFont newfont = new PdfFont(PdfFontFamily.TimesRoman, 11f, PdfFontStyle.Italic | PdfFontStyle.Bold);
foreach (PdfUsedFont font in fonts)
{
    font.Replace(newfont);
}

Step 4: Save the resultant document.

doc.SaveToFile("output.pdf");

Screenshot after replacing font:

How to replace font(s) in PDF document

Full code:

[C#]
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;

namespace Replace_font_in_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"E:\Program Files\input.pdf");
            PdfUsedFont[] fonts = doc.UsedFonts;
            PdfFont newfont = new PdfFont(PdfFontFamily.TimesRoman, 11f, PdfFontStyle.Italic | PdfFontStyle.Bold);
            foreach (PdfUsedFont font in fonts)
            {
                font.Replace(newfont);
            }

            doc.SaveToFile("output.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Graphics.Fonts

Namespace Replace_font_in_PDF
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("E:\Program Files\input.pdf")
			Dim fonts As PdfUsedFont() = doc.UsedFonts
			Dim newfont As New PdfFont(PdfFontFamily.TimesRoman, 11F, PdfFontStyle.Italic Or PdfFontStyle.Bold)
			For Each font As PdfUsedFont In fonts
				font.Replace(newfont)
			Next

			doc.SaveToFile("output.pdf")
		End Sub
	End Class
End Namespace

Spire.PDF provides an easy and straightforward method called FindText to find a particular text in a page of a PDF document. After the specified text is found, we’re able to acquire the coordinate information by the Position property. Follow sections presents how to get coordinates of desired text in PDF using Spire.PDF.

Code Snippets:

Step 1: Initialize an instance of PdfDocument class and load a sample PDF file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"sample.pdf");

Step 2: Declare a variable of an array whose array type is PdfTextFind class, which represents a result of searching text in a PDF page.

PdfTextFind[] results = null;

Step 3: Find all results of searched text “Spire.PDF” in the PDF document and store the results in PdfTextFind[]. Traversal the results to get the specified text and acquire its coordinate information by the Position property.

foreach (PdfPageBase page in doc.Pages)
{
    results = page.FindText("Spire.PDF").Finds;
    foreach (PdfTextFind text in results)
    {
        PointF p = text.Position;
        Console.WriteLine(p);
    }
}

Result:

How to Get Coordinates of Desired Text in PDF in C#, VB.NET

Full Code:

[C#]
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"sample.pdf");

PdfTextFind[] results = null;
foreach (PdfPageBase page in doc.Pages)
{
    results = page.FindText("C#").Finds;
    foreach (PdfTextFind text in results)
    {
        PointF p = text.Position;
        Console.WriteLine(p);
    }
}
[VB.NET]
Dim doc As PdfDocument = New PdfDocument()
doc.LoadFromFile("sample.pdf")
 
Dim results() As PdfTextFind = Nothing
Dim page As PdfPageBase
For Each page In doc.Pages
    results = page.FindText("C#").Finds
    Dim text As PdfTextFind
    For Each text In results
        Dim p As PointF =  text.Position
        Console.WriteLine(p)
    Next
Next
Tuesday, 01 November 2016 08:10

How to get the font information of text in PDF

Written by support iceblue

Spire.PDF provides a class named PdfDocument that represents a PDF file, this class contains a property named UsedFonts which allows us to access the fonts used in PDF file, and then we can get the font information such as name, size, type and style easily.

The following steps explain how to get the font information of text in PDF by using Spire.PDF.

Step 1: Instantiate an object of PdfDocument class and load the PDF file.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(@"E:\Program Files\Sample.pdf");

Step 2: Get the fonts that are used in the PDF file and put them into a PdfUsedFont array.

PdfUsedFont[] usedfont = pdf.UsedFonts;

Step 3: Loop through the array and print out the name, size, type and style of each font.

foreach (PdfUsedFont font in usedfont)
{
    Console.WriteLine("{0}, {1}, {2}, {3}", font.Name, font.Size, font.Type, font.Style);
}
Console.ReadKey();

Output:

How to get the font information of text in PDF

Full code:

[C#]
using System;
using Spire.Pdf;
using Spire.Pdf.Graphics.Fonts;

namespace Get_the_font_information_of_text_in_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(@"E:\Program Files\Sample.pdf");
            PdfUsedFont[] usedfont = pdf.UsedFonts;
            foreach (PdfUsedFont font in usedfont)
            {
                Console.WriteLine("{0}, {1}, {2}, {3}", font.Name, font.Size, font.Type, font.Style);
            }
            Console.ReadKey();
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics.Fonts

Namespace Get_the_font_information_of_text_in_PDF
	Class Program
		Private Shared Sub Main(args As String())
			Dim pdf As New PdfDocument()
			pdf.LoadFromFile("E:\Program Files\Sample.pdf")
			Dim usedfont As PdfUsedFont() = pdf.UsedFonts
			For Each font As PdfUsedFont In usedfont
				Console.WriteLine("{0}, {1}, {2}, {3}", font.Name, font.Size, font.Type, font.Style)
			Next
			Console.ReadKey()
		End Sub
	End Class
End Namespace

With the help of Spire.PDF, we have already demonstrated how to add text with different styles to a PDF file. By using the method canvas.drawstring offered by Spire.PDF, we can set the position, font, brush and style for the adding texts. With the PdfFontStyle, we can set the style to underline, bold, italic, regular and strikeout. Sometimes we may need to set multiple font style for the same texts within one paragraph, such as bold and italic together.

Here comes to the code snippet of how to apply two kinds of font styles together for the text on PDF in C#.

Step 1: Create a new PDF document.

PdfDocument pdf = new PdfDocument();

Step 2: Add a new page to the PDF file.

PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins());

Step 3: Create the PdfFont as Helvetica with size 10f, apply two font styles for it.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica,10f,PdfFontStyle.Italic | PdfFontStyle.Underline);
PdfFont font2 = new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Bold | PdfFontStyle.Strikeout);
PdfFont font3 = new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Bold | PdfFontStyle.Underline);

Step 4: Create a brush and set its color.

PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
PdfSolidBrush brush2 = new PdfSolidBrush(Color.Gray);
PdfSolidBrush brush3 = new PdfSolidBrush(Color.Green);

Step 5: Draw the text string at the specified location with the specified Brush and Font objects.

page.Canvas.DrawString("This sentence is Italic and underline", font, brush, new PointF(10, 10));
page.Canvas.DrawString("This sentence is Bold and strikeout", font2,brush2, new PointF(10, 40));
page.Canvas.DrawString("This sentence is Bold and underline",font3,brush3,new PointF(10, 70));

Step 6: Save the document to file.

pdf.SaveToFile("reslut.pdf");

Please check the effective screenshot as below:

How to apply multiple font styles for the text on PDF in C#

Full codes:

static void Main(string[] args)
{
    PdfDocument pdf = new PdfDocument();
    PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins());

    PdfFont font = new PdfFont(PdfFontFamily.Helvetica,10f,PdfFontStyle.Italic | PdfFontStyle.Underline);
    PdfFont font2 = new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Bold | PdfFontStyle.Strikeout);
    PdfFont font3 = new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Bold | PdfFontStyle.Underline);

    PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
    PdfSolidBrush brush2 = new PdfSolidBrush(Color.Gray);
    PdfSolidBrush brush3 = new PdfSolidBrush(Color.Green);

    page.Canvas.DrawString("This sentence is Italic and underline", font,brush, new PointF(10, 10));
    page.Canvas.DrawString("This sentence is Bold and strikeout", font2, brush2, new PointF(10, 40));
    page.Canvas.DrawString("This sentence is Bold and underline",font3,brush3,new PointF(10, 70));

    pdf.SaveToFile("reslut.pdf");
}
Page 1 of 2