Highlight text in the pdf.

Posted by Sudeesh.Govind under ASP.NET on 9/13/2012 | Points: 10 | Views : 6662 | Status : [Member] | Replies : 6
Hi all,

I have a requirement would you help me.

in the aspx form contain 1 text box and search button.
when enter some text in text box and click search button.
when we click the button then open a pdf file and hilight the text that are given in the text box with background colour

any one have idea.

please send code or post the code.
sudeesh.govind@gmail.com

learn and earn


Responses

Posted by: Hariinakoti on: 9/13/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ,
see this link.i think it is useful to u
http://stackoverflow.com/questions/8582706/highlighting-text-colors-of-existing-pdf-using-itextsharp-using-c-sharp

Thanks & Regards
Hari

Sudeesh.Govind, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Eva2012 on: 9/13/2012 [Member] Starter | Points: 25

Up
0
Down
You can use a PDF component (http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html) to realize your task.

Sudeesh.Govind, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudeesh.Govind on: 12/6/2012 [Member] Starter | Points: 25

Up
0
Down
Ya we can highlight the test using iTextSharp. I want to search a word/line from PDF and shows the searched result as highlight.
How can we search the PDF

learn and earn

Sudeesh.Govind, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudeesh.Govind on: 12/6/2012 [Member] Starter | Points: 25

Up
0
Down
We can highlight using these two method
1) using chunck

public void HilightPDF()
{
string FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test1.pdf");


string path = Server.MapPath("~/PDF/");

Rectangle r = new Rectangle(400, 300);

Document doc = new Document(r);



try
{

PdfWriter.GetInstance(doc, new FileStream(FilePath, FileMode.Create));

doc.Open();

string text = @"The result can be seen below, which shows the text

having been written to the document but it looks a

mess. Chunks have no concept of how to force a new

line when the length exceeds the available width in

the document. Really, all they should be used for is

to change or set the style of a word or phrase inline. ";

text = text.Replace(Environment.NewLine, String.Empty).Replace(" ", String.Empty);
Font georgia = FontFactory.GetFont("georgia", 10f);
georgia.Color = BaseColor.GRAY;
Chunk beginning = new Chunk("when", new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.ITALIC));
beginning.SetBackground(new CMYKColor(0xFF, 0x00, 0x00,0x00), 10, -50, 20, -10);

Phrase p1 = new Phrase(beginning);
Paragraph p = new Paragraph();
p.Add(p1);
doc.Add(p);

}

catch (DocumentException dex)
{

throw (dex);

}

catch (IOException ioex)
{

throw (ioex);

}

finally
{

doc.Close();

}

}

2) using pdf annotation

public string ParsePdfText()//(string sourcePDF,int fromPageNum,int toPageNum)
{

//Create a simple test file
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
Guid id = Guid.NewGuid();
string unique = id.ToString();
//Create a new file from our test file with highlighting
string fileName = "Highlighted" + unique;
string highLightFile = Path.Combine(Server.MapPath("~/PDF/"), fileName+".pdf");//Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

//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);


}

}
return fileName;

}

Both work. My requirement is to search a string in pdf file and highlight those string in the pdf.

Any body can help me

learn and earn

Sudeesh.Govind, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudeesh.Govind on: 12/6/2012 [Member] Starter | Points: 25

Up
0
Down
How to get xy cordinate collection of a particular search text dynamically. Anybody help me.

learn and earn

Sudeesh.Govind, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mattyclown on: 2/19/2014 [Member] Starter | Points: 25

Up
0
Down
You have to use annotating control to highlight pdf file, like
http://www.rasteredge.com/how-to/vb-net-imaging/pdf-annotating/


Sudeesh.Govind, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response