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