Share via


How to highlights text in pdf document in c# using itextsharp.dll

Question

Monday, June 19, 2017 9:21 AM

How to highlights text in pdf document in c# using itextsharp.dll for below scenario

1.pdf file is in byte[] format

2.pdf file is in folder

All replies (2)

Thursday, June 29, 2017 6:39 AM

Hi Ganesan IT,

Thank you for posting here.

If you want to highlight text in pdf document using an existing PdfStamper called stamper, please see simple code.

PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad);

Once you have the highlight you can set the color using:

highlight.Color = BaseColor.YELLOW;

And then add it to your stamper

stamper.AddAnnotation(highlight,1);

Here is a sample:

using System;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Create a simple test file
            string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");

            using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        doc.Open();
                        doc.Add(new Paragraph("This is a test"));
                        doc.Close();
                    }
                }
            }

            //Create a new file from our test file with highlighting
            string highLightFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Highlighted.pdf");

            //Bind a reader and stamper to our test PDF
            PdfReader reader = new PdfReader(outputFile);

            using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (PdfStamper stamper = new PdfStamper(reader, fs))
                {
                    //Create a rectangle for the highlight. NOTE: Technically this isn't used but it helps with the quadpoint calculation
                    iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f);
                    //Create an array of quad points based on that rectangle. NOTE: The order below doesn't appear to match the actual spec but is what Acrobat produces
                    float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top };

                    //Create our hightlight
                    PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad);

                    //Set the color
                    highlight.Color = BaseColor.YELLOW;

                    //Add the annotation
                    stamper.AddAnnotation(highlight,1);
                }
            }

            this.Close();
        }
    }
}

Best Regards,

Hart

Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Wednesday, July 5, 2017 2:00 AM

I think Spire.PDF is a good alternative for your requirements. It supports to load a Pdf file from byte array as well as from a folder in disk. And it allows to highlight the selected text with custom color or to add markup annotation to specific range of text.

Highlight selected text

PdfTextFind[] results = pdf.Pages[0].FindText("Hello World").Finds;
foreach (PdfTextFind result in results)
{
    result.ApplyHighLight(Color.Green);
}

Markup text

//Load a sample PDF document
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
PdfPageBase page = doc.Pages[0];

//Locate the text that you want to markup
PdfTextFind ptf =page.FindText("Highlight text").Finds[0];
//Create a Text Markup annotation
Font font = ptf.Font;
string markupText = "Higlight text";
PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Adminstrator", "Text Markup Annotation", markupText, ptf.Position, new PdfTrueTypeFont(font));
annotation.Border = new PdfAnnotationBorder(0.75f);
annotation.TextMarkupColor = Color.Yellow;   

//Add annotation to PDF page    
page.AnnotationsWidget.Add(annotation);
doc.SaveToFile("Annotation.pdf", FileFormat.PDF);