Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6968137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:24:46+00:00 2026-05-27T16:24:46+00:00

I would like know whether we can highlight text (colors) of already created PDF

  • 0

I would like know whether we can highlight text (colors) of already created PDF using itextsharp?

I see examples like creating a new PDF, while doing so we can apply colors. I am looking for where I can get chunks of text from PDF and apply colors and save it.

Here is the thing I am trying to accomplish, read a PDF file, parse text and highlight text based on business rules.

Any third party dll suggestion also works, as a first step I am looking in to opensource iTextsharp library.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:24:47+00:00Added an answer on May 27, 2026 at 4:24 pm

    Yes you can highlight text but you will have to work for it unfortunately. What looks like a highlight is a PDF Text Markup Annotation as far as the spec is considered. That part is pretty easy. The hard part is figuring out the coordinates to apply the annotation to.

    Here’s the simple code for creating a highlight using an existing PdfStamper called stamper:

    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 on page 1 using:

    stamper.AddAnnotation(highlight,1);
    

    Technically the rect parameter doesn’t actually get used (as far as I can tell) and instead gets overridden by the quad parameter. The quad parameter is an array of x,y coords that essentially represent the corners of a rectangle (technically quadrilateral). The spec says they start in the bottom left and go counter-clockwise but in reality they appear to go bottom left to bottom right to top left to top right. Calculating the quad is a pain so instead its just easier to create a rectangle and create the quad from it:

    iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f);
    float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top };
    

    So how do you get the rectangle of existing text in the first place? For that you need to look at TextExtractionStrategy and PdfTextExtractor. There’s a lot to go into so I’m going to start by pointing you at this post which has some further posts linked.

    Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.1.2 that shows off the creation of a simple PDF and the highlighting of part of the text using hard-coded coordinates. If you need help calculating these coordinates start with the link above and then ask any questions!

    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();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know whether i can get the drive information using the
I would like to know whether I can use DL Query in Protege programatically.
I would like to know whether I can test some value against a condition
I would like to know whether I can get source code a method on
I would like to know whether something like this can be easily achieved, any
I would like to know whether there are software that a user can edit
I am new to JavaFx technology, I would like to know whether I can
I would like to know whether a Web Service can be used to monitor
I am using Install Shield 5.5. I would like to know whether is it
I would like to know whether strace can cause anomaly for the program it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.