Monday, 11 January 2021 07:13

Add Line Numbers to a PDF in C#/VB.NET

This article demonstrates how to add line numbers before chunks of text in a PDF page by using Spire.PDF for .NET.

Below is a screenshot of the input document.

Add Line Numbers to a PDF in C#, VB.NET

C#
using Spire.Pdf;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddLineNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument of instance
            PdfDocument doc = new PdfDocument();

            //Load a PDF document
            doc.LoadFromFile(@"G:\360MoveData\Users\Administrator\Desktop\sample.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Find the spcific text in the fisrt line
            PdfTextFind topLine = page.FindText("C# (pronounced See Sharp)", TextFindParameter.None).Finds[0];

            //Get the line height
            float lineHeight = topLine.Bounds.Height;

            //Get the Y coordinate of the selected text
            float y = topLine.Bounds.Y;

            //Find the spcific text in the second line
            PdfTextFind secondLine = page.FindText("language. C#", TextFindParameter.None).Finds[0];

            //Calculate the line spacing
            float lineSpacing = secondLine.Bounds.Top - topLine.Bounds.Bottom;

            //Find the specific text in the last line
            PdfTextFind bottomLine = page.FindText("allocation of objects", TextFindParameter.None).Finds[0];

            //Get the height of the chunks 
            float height = bottomLine.Bounds.Bottom;

            //Create a font
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 11f);

            int i = 1;
            while (y < height)
            {
                //Draw line number before a specific line of text
                page.Canvas.DrawString(i.ToString(), font, PdfBrushes.Black, new PointF(15, y));
                y += lineHeight + lineSpacing;
                i++;
            }

            //Save the document
            doc.SaveToFile("result.pdf");
        }
    }
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.General.Find
Imports Spire.Pdf.Graphics
Imports System.Drawing
 
Namespace AddLineNumber
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Create a PdfDocument of instance
            Dim doc As PdfDocument =  New PdfDocument() 
 
            'Load a PDF document
            doc.LoadFromFile("G:\360MoveData\Users\Administrator\Desktop\sample.pdf")
 
            'Get the first page
            Dim page As PdfPageBase =  doc.Pages(0) 
 
            'Find the spcific text in the fisrt line
            Dim topLine As PdfTextFind =  page.FindText("C# (pronounced See Sharp)",TextFindParameter.None).Finds(0) 
 
            'Get the line height
            Dim lineHeight As single =  topLine.Bounds.Height 
 
            'Get the Y coordinate of the selected text
            Dim y As single =  topLine.Bounds.Y 
 
            'Find the spcific text in the second line
            Dim secondLine As PdfTextFind =  page.FindText("language. C#",TextFindParameter.None).Finds(0) 
 
            'Calculate the line spacing
            Dim lineSpacing As single =  secondLine.Bounds.Top - topLine.Bounds.Bottom 
 
            'Find the specific text in the last line
            Dim bottomLine As PdfTextFind =  page.FindText("allocation of objects",TextFindParameter.None).Finds(0) 
 
            'Get the height of the chunks 
            Dim height As single =  bottomLine.Bounds.Bottom 
 
            'Create a font
            Dim font As PdfFont =  New PdfFont(PdfFontFamily.TimesRoman,11f) 
 
            Dim i As Integer =  1 
            While y < height
                'Draw line number before a specific line of text
                page.Canvas.DrawString(i.ToString(),font,PdfBrushes.Black,New PointF(15,y))
                y += lineHeight + lineSpacing
                i = i + 1
            End While
 
            'Save the document
            doc.SaveToFile("result.pdf")
        End Sub
    End Class
End Namespace

Output

Add Line Numbers to a PDF in C#, VB.NET

Published in Text
Wednesday, 20 March 2024 09:49

C#: Replace Text in a PDF Document

People often need to replace text in a PDF document for a variety of reasons. It could be to correct errors or typos, update outdated information, customize the content for a specific audience or purpose, or comply with legal or regulatory requirements. By replacing text in a PDF, individuals can ensure accuracy, maintain document integrity, and enhance the overall quality and relevance of the information presented.

In this article, you will learn how to replace text in a PDF document in C# by using the Spire.PDF for .NET library.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Replace Text in a Specific PDF Page in C#

Spire.PDF for .NET offers the PdfTextReplacer.ReplaceAllText() method, allowing users to replace all occurrences of target text in a page with new text. The following are the steps to replace text in a specific page using C#.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object, and specify the replace options using ReplaceType property of the object.
  • Create a PdfTextReplacer object, and apply the replace options using Options property of it.
  • Replace all occurrences of the target text in the page with new text using PdfTextReplacer.ReplaceAllText() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;

namespace ReplaceTextInPage
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Specify the options for text replacement
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextReplacer object based on the page
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);

            // Set the replace options
            textReplacer.Options = textReplaceOptions;

            // Replace all occurrences of target text with new text
            textReplacer.ReplaceAllText(".NET Framework", "New Content");

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceTextInPage.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Replace Text in a PDF Document

Replace Text in an Entire PDF Document in C#

In order to replace all occurrences of target text in the entire document with new text, you need to iterate through pages in the document and replace text on each page using the PdfTextReplacer.ReplaceAllText() method.

The following are the steps to replace text in an entire PDF document using C#.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Create a PdfTextReplaceOptions object, and specify the replace options using ReplaceType property of the object.
  • Iterate through the pages in the document.
    • Create a PdfTextReplacer object based on a specified page, and apply the replace options using Options property of it.
    • Replace all occurrences of the target text in the page with new text using PdfTextReplacer.ReplaceAllText() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;

namespace ReplaceInEntireDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Specify the options for text replacement
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;

            for (int i = 0; i < doc.Pages.Count; i++) {

                // Get a specific page
                PdfPageBase page = doc.Pages[i];

                // Create a PdfTextReplacer object based on the page
                PdfTextReplacer textReplacer = new PdfTextReplacer(page);

                // Set the replace options
                textReplacer.Options = textReplaceOptions;

                // Replace all occurrences of target text with new text
                textReplacer.ReplaceAllText(".NET Framework", "New Content");
            }
            

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceTextInDocument.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

Replace the First Occurrence of the Target Text in C#

Instead of replacing all text on a page, you can only replace the first occurrence of the target text by utilizing the ReplaceText() method of the PdfTextReplacer class.

The following are the steps to replace the first occurrence of the target text using C#.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object, and specify the replace options using ReplaceType property of the object.
  • Create a PdfTextReplacer object, and apply the replace options using Options property of it.
  • Replace the first occurrence of the target text in the page with new text using PdfTextReplacer.ReplaceText() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;

namespace ReplaceFirstOccurance
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Specify the options for text replacement
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.IgnoreCase;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;

            // Get a specific page
            PdfPageBase page = doc.Pages[1];

            // Create a PdfTextReplacer object based on the page
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);

            // Set the replace options
            textReplacer.Options = textReplaceOptions;

            // Replace the first occurrence of target text with new text
            textReplacer.ReplaceText(".NET Framework", "New Content");

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceFirstOccurance.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Replace Text in a PDF Document

Replace Text Based on a Regular Expression in C#

Regular expressions are powerful and versatile patterns used for matching and manipulating text. With Spire.PDF, you utilize regular expressions to search for specific text patterns in a PDF and replace them with new strings.

The steps to replace text in PDF based on a regular expression are as follows.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object.
  • Specify the replace type as Regex using PdfTextReplaceOptions.ReplaceType property.
  • Create a PdfTextReplacer object, and apply the replace options using Options property of it.
  • Find and replace the text that matches a specified regular expression using PdfTextReplacer.ReplaceAllText() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;

namespace ReplaceUsingRegularExpression
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

            // Create a PdfTextReplaceOptions object
            PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

            // Set the replace type as Regex
            textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.Regex;

            // Get a specific page
            PdfPageBase page = doc.Pages[1];

            // Create a PdfTextReplacer object based on the page
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);

            // Set the replace options
            textReplacer.Options = textReplaceOptions;

            // Specify the regular expression
            string regularExpression = @"\bC\w*?R\b";

            // Replace all occurrences that match the regular expression with new text
            textReplacer.ReplaceAllText(regularExpression, "NEW");

            // Save the document to a different PDF file
            doc.SaveToFile("ReplaceWithRegularExpression.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Replace Text in a PDF Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Text
Wednesday, 18 September 2019 06:33

Add Tooltip to the Searched Text on PDF in C#

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);
     
        }
    }
}
Published in Text
Wednesday, 05 December 2018 08:56

Draw Superscript and Subscript Text in PDF in C#

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

Draw Superscript Text

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace Superscript
{
    class Program
    {
        static void Main(string[] args)
        {
            //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

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace Subscript
{
    class Program
    {
        static void Main(string[] args)
        {
            //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#

Published in Text
Thursday, 05 January 2023 02:26

C#/VB.NET: Create a Multi-Column PDF

When designing magazines or newspapers, you may need to display content in multiple columns on a single page to improve readability. In this article, you will learn how to programmatically create a two-column PDF from scratch using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Create a Two-Column PDF from Scratch in C# and VB.NET

Spire.PDF for .NET allows you to create a two-column PDF by drawing text at two separate rectangle areas in a PDF page. Below are the detailed steps to achieve the task.

  • Create a PdfDocument instance.
  • Add a new page in the PDF using PdfDocument.Pages.Add() method.
  • Define paragraph text, then set the text font and text alignment.
  • Draw text at two separate rectangle areas in the PDF using PdfPageBase.Canvas.DrawString (String, PdfFontBase, PdfBrush, RectangleF, PdfStringFormat) method.
  • Save the result file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace CreateTwoColumnPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument doc = new PdfDocument();

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

            //Define paragraph text
            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.";

            //Get width and height of page
            float pageWidth = page.GetClientSize().Width;
            float pageHeight = page.GetClientSize().Height;

            //Create a PdfSolidBrush instance
            PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.Black));

            //Create a PdfFont instance
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14f);

            //Set the text alignment via PdfStringFormat class
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);

            //Draw text
            page.Canvas.DrawString(s1, font, brush, new RectangleF(0, 20, pageWidth / 2 - 8f, pageHeight),format);
            page.Canvas.DrawString(s2, font, brush, new RectangleF(pageWidth / 2 + 8f, 20, pageWidth / 2, pageHeight), format);

            //Save the result document
            doc.SaveToFile("CreateTwoColumnPDF.pdf.pdf");
        }
    }
}

C#/VB.NET: Create a Multi-Column PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Text
Tuesday, 28 February 2017 09:32

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

Spire.PDF provides support to render simple HTML string in a PDF document by using PdfHTMLTextElement class. (Only available on .NET, .Net Core/ .NET Standard doesn't offer PdfHTMLTextElement & PdfMetafileLayoutFormat 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/2),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#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace InsertHTMLStyledTexttoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //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 HTML styled text to PDF using "
                             + "Spire.PDF for .NET. ";

            //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 / 2), format);
            doc.SaveToFile("Output.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace InsertHTMLStyledTexttoPDF
	Class Program
		Private Shared Sub Main(args As String())
			'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 HTML styled text to PDF using " + "Spire.PDF for .NET. "

			'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 / 2), format)
			doc.SaveToFile("Output.pdf")
		End Sub
	End Class
End Namespace
Published in Text
Wednesday, 22 February 2017 09:12

Replace font(s) in PDF document

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
Published in Text
Tuesday, 28 November 2023 06:30

C#: Get Coordinates of Text or an Image in PDF

Getting the coordinates of text or an image in a PDF is a useful task that allows precise referencing and manipulation of specific elements within the document. By extracting the coordinates, one can accurately identify the position of text or images on each page. This information proves valuable for tasks like data extraction, text recognition, or highlighting specific areas. This article introduces how to get the coordinate information of text or an image in a PDF document in C# using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for .NET package as references in your .NET project. You can download Spire.PDF for .NET from our website or install it directly through NuGet.

PM> Install-Package Spire.PDF

Get Coordinates of Text in PDF in C#

The PdfTextFinder.Find() method provided by Spire.PDF can help us find all instances of the string to be searched in a searchable PDF document. The coordinate information of a specific instance can be obtained through the PdfTextFragment.Positions property. The following are the step to get the (X, Y) coordinates of the specified text in a PDF using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Loop through the pages in the document.
  • Create a PdfTextFinder object, and get all instances of the specified text from a page using PdfTextFinder.Find() method.
  • Loop through the find results and get the coordinate information of a specific result through PdfTextFragment.Positions property.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;
using System.Drawing;

namespace GetCoordinatesOfText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

            //Loop through all pages
            foreach (PdfPageBase page in doc.Pages)
            {
                //Create a PdfTextFinder object
                PdfTextFinder finder = new PdfTextFinder(page);

                //Set the find options
                PdfTextFindOptions options = new PdfTextFindOptions();
                options.Parameter = TextFindParameter.IgnoreCase;
                finder.Options = options;

                //Find all instances of a specific text
                List fragments = finder.Find("target audience");

                //Loop through the instances
                foreach (PdfTextFragment fragment in fragments)
                {
                    //Get the position of a specific instance
                    PointF found = fragment.Positions[0];
                    Console.WriteLine(found);
                }
            }
        }
    }
}

C#: Get Coordinates of Text or an Image in PDF

Get Coordinates of an Image in PDF in C#

Spire.PDF provides the PdfImageHelper.GetImagesInfo() method to help us get all image information on a specific page. The coordinate information of a specific image can be obtained through the PdfImageInfo.Bounds property. The following are the steps to get the coordinates of an image in a PDF document using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specific page through PdfDocument.Pages[index] property.
  • Create a PdfImageHelper object, and get all image information from the page using PdfImageHelper.GetImageInfo() method.
  • Get the coordinate information of a specific image through PdfImageInfo.Bounds property.
  • C#
using Spire.Pdf;
using Spire.Pdf.Utilities;

namespace GetCoordinatesOfImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

            //Get a specific page 
            PdfPageBase page = doc.Pages[0];

            //Create a PdfImageHelper object
            PdfImageHelper helper = new PdfImageHelper();

            //Get image information from the page
            PdfImageInfo[] images = helper.GetImagesInfo(page);

            //Get X,Y coordinates of a specific image
            float xPos = images[0].Bounds.X;
            float yPos = images[0].Bounds.Y;
            Console.WriteLine("The image is located at({0},{1})", xPos, yPos);
        }
    }
}

C#: Get Coordinates of Text or an Image in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Text
Tuesday, 01 November 2016 08:10

Get the font information of text in PDF

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
Published in Text

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:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace MultipleFontStyles
{
    class Program
    {
        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");
        }
    }
}
Published in Text
Page 1 of 2