public void PutRectAnno(float lx, float ly, float rx, float ry) { using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfStamper stamper = new PdfStamper(reader, fs)) { iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(lx, ly, rx, ry); float[] quad = { rect.Left, rect.Top, rect.Right, rect.Top, rect.Left, rect.Bottom, rect.Right, rect.Bottom }; PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad); highlight.Color = BaseColor.YELLOW; if (rect.Width > 0 && rect.Height > 0) { stamper.AddAnnotation(highlight, 1); } stamper.Close(); } fs.Close(); } }
public void HighLightText(String[] searchText) { //Create a new file from our test file with highlighting string highLightFile = Path.Combine(m_DirPath, "Highlighted.pdf"); MyTextExtractionStrategy[,] arr_t = null; int Pages = 0; //Parse page 1 of the document above using (var r = new PdfReader(m_filename)) { Pages = r.NumberOfPages; //Create an array of our strategy arr_t = new MyTextExtractionStrategy[r.NumberOfPages, searchText.Length]; for (int i = 0; i < r.NumberOfPages; i++) { for (int j = 0; j < searchText.Length; j++) { arr_t[i, j] = new MyTextExtractionStrategy(searchText[j], 1056f); var ex = PdfTextExtractor.GetTextFromPage(r, i + 1, arr_t[i, j]); } } } //Bind a reader and stamper to our test PDF PdfReader reader = new PdfReader(m_filename); using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfStamper stamper = new PdfStamper(reader, fs)) { for (int i = 0; i < Pages; i++) { for (int j = 0; j < searchText.Length; j++) { var t = arr_t[i, j]; foreach (var p in t.m_SearchResultsList) { Rectangle rect = p.rect; //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 highlight 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, i + 1); } } } } } }
// private void highlightPDFAnnotation(string readerPath, string outputFile, int pageno, string[] highlightText) private void highlightPDFAnnotation(string readerPath, string outputFile, string[] highlightText) { PdfReader reader = new PdfReader(readerPath); PdfContentByte canvas; using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfStamper stamper = new PdfStamper(reader, fs)) { int pageCount = reader.NumberOfPages; for (int pageno = 1; pageno <= pageCount; pageno++) { var strategy = new HighLightTextLocation(); strategy.UndercontentHorizontalScaling = 100; string currentText = PdfTextExtractor.GetTextFromPage(reader, pageno, strategy); for (int i = 0; i < highlightText.Length; i++) { List <Rectangle> MatchesFound = strategy.GetTextLocations(highlightText[i].Trim(), StringComparison.CurrentCultureIgnoreCase); foreach (Rectangle rect in MatchesFound) { float[] quad = { rect.Left - 3.0f, rect.Bottom, rect.Right, rect.Bottom, rect.Left - 3.0f, rect.Top + 1.0f, rect.Right, rect.Top + 1.0f }; //Create our hightlight PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad); //Set the color highlight.Color = BaseColor.YELLOW; PdfAppearance appearance = PdfAppearance.CreateAppearance(stamper.Writer, rect.Width, rect.Height); PdfGState state = new PdfGState(); state.BlendMode = new PdfName("Multiply"); appearance.SetGState(state); appearance.Rectangle(0, 0, rect.Width, rect.Height); appearance.SetColorFill(BaseColor.YELLOW); appearance.Fill(); highlight.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance); //Add the annotation stamper.AddAnnotation(highlight, pageno); } } } } } reader.Close(); }
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(); }
//-------------------------------------------------------------------------------------------------- void AddHeader(PdfStamper stamper, PdfReader reader, string stComment) { PdfContentByte canvas = stamper.GetOverContent(1); canvas.BeginText(); canvas.SetFontAndSize(GetDefaultFont(), 12); Rectangle rect = reader.GetPageSizeWithRotation(1); canvas.ShowTextAligned(PdfContentByte.ALIGN_CENTER, stComment, (rect.Left + rect.Right) / 2, rect.Top - 12, 0); canvas.EndText(); rect.Bottom = rect.Top - 18; float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, stComment, PdfAnnotation.MARKUP_HIGHLIGHT, quad); highlight.Color = BaseColor.YELLOW; highlight.Title = "PDFCompare"; stamper.AddAnnotation(highlight, 1); }
/// <summary> /// Highlights the PDF with Yellow annotation /// </summary> /// <param name="inputFile">File to read from</param> /// <param name="highLightFile">File to write to</param> /// <param name="pageno">Which page no to read</param> /// <param name="textToAnnotate">Texts to annotate</param> private void HighlightPDFAnnotation(string inputFile, string highLightFile, int pageno, params string[] textToAnnotate) { PdfReader reader = new PdfReader(inputFile); using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfStamper stamper = new PdfStamper(reader, fs)) { MyLocationTextExtractionStrategy strategy = new MyLocationTextExtractionStrategy(); strategy.UndercontentHorizontalScaling = 100; string currentText = PdfTextExtractor.GetTextFromPage(reader, pageno, strategy); for (int i = 0; i < textToAnnotate.Length; i++) { if (string.IsNullOrWhiteSpace(textToAnnotate[i])) { continue; } var lstMatches = strategy.GetTextLocations(textToAnnotate[i].Trim(), StringComparison.CurrentCultureIgnoreCase); if (!this.chkAnnotation.Checked) { lstMatches = lstMatches.Take(1).ToList(); } foreach (iTextSharp.text.Rectangle rectangle in lstMatches) { float[] quadPoints = { rectangle.Left - 3.0f, rectangle.Bottom, rectangle.Right, rectangle.Bottom, rectangle.Left - 3.0f, rectangle.Top + 1.0f, rectangle.Right, rectangle.Top + 1.0f }; PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer , rectangle, null , PdfAnnotation.MARKUP_HIGHLIGHT, quadPoints); highlight.Color = BaseColor.YELLOW; PdfGState state = new PdfGState(); state.BlendMode = new PdfName("Multiply"); PdfAppearance appearance = PdfAppearance.CreateAppearance(stamper.Writer, rectangle.Width, rectangle.Height); appearance.SetGState(state); appearance.Rectangle(0, 0, rectangle.Width, rectangle.Height); appearance.SetColorFill(BaseColor.YELLOW); appearance.Fill(); highlight.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance); //Add the annotation stamper.AddAnnotation(highlight, pageno); } } } } reader.Close(); }
public void ProcessPdf(StringComparison sc, string sourceFile, string destinationFile, string searchTerm, int excelRowNumber, List <KeyValuePair <int, string> > searchValues) { var sArr = searchTerm.Split(','); myProgressBar.Maximum = searchValues.Count; bool found = false; Cursor = Cursors.WaitCursor; if (File.Exists(sourceFile)) { var pReader = new PdfReader(sourceFile); myProgressBar.Value = 0; PdfStamper stamper = null; foreach (var item in searchValues) { var newStrings = item.Value.Split(','); var foundText = string.Empty; foreach (var search in newStrings) { for (var page = 1; page <= pReader.NumberOfPages; page++) { var t = new MyLocationTextExtractionStrategy(search, CompareOptions.Ordinal); using (var r = new PdfReader(sourceFile)) { var ex = PdfTextExtractor.GetTextFromPage(r, 1, t); } var matchesFound = t.MyPoints; if (t.MyPoints.Count > 0) { found = true; if (!string.IsNullOrEmpty(search)) { foundText += "," + search; } if (!File.Exists(destinationFile)) { stamper = new PdfStamper(pReader, new FileStream(destinationFile, FileMode.Create)); } if (!_fileList.Contains(destinationFile)) { _fileList.Add(destinationFile); } var cb = stamper.GetUnderContent(page); cb.SetColorFill(BaseColor.BLACK); foreach (var rect in matchesFound) { if (rect.Text == search) { cb.Rectangle(rect.Rect.Left, rect.Rect.Bottom, rect.Rect.Width, rect.Rect.Height); float[] quad = { rect.Rect.Left, rect.Rect.Bottom, rect.Rect.Right, rect.Rect.Bottom, rect.Rect.Left, rect.Rect.Top, rect.Rect.Right, rect.Rect.Top }; var highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect.Rect, Constants.vbNull.ToString(), PdfAnnotation.MARKUP_HIGHLIGHT, quad); highlight.Color = BaseColor.YELLOW; stamper.AddAnnotation(highlight, page); } } } } } if (found && !string.IsNullOrEmpty(foundText)) { if (sourceFile == txtFirstPDF.Text) { _foundValuesInFirstPdf.Add(new KeyValuePair <int, string>(item.Key, foundText.Substring(1))); } else { _foundValuesInSecindPdf.Add(new KeyValuePair <int, string>(item.Key, foundText.Substring(1))); } } myProgressBar.Value = myProgressBar.Value + 1; } if (stamper != null) { stamper.Close(); } } this.Cursor = Cursors.Default; }
//-------------------------------------------------------------------------------------------------- void MarkText(List <TextItem> items, int iPosition, int iCount, PdfStamper stamper, BaseColor color, int iPageAdd = 0, string stComment = null) { const string stTittle = "PDFCompare"; if (iCount == 0) { float rWidth, rPosition; if (iPosition >= items.Count) { iPosition = items.Count - 1; rWidth = (items[iPosition].MaxY - items[iPosition].MinY) / 5; rPosition = items[iPosition].MaxX; } else { rWidth = (items[iPosition].MaxY - items[iPosition].MinY) / 5; rPosition = items[iPosition].MinX - rWidth; } Rectangle rect = new Rectangle(rPosition, items[iPosition].MaxY, rPosition + rWidth, items[iPosition].MinY); float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, stComment, PdfAnnotation.MARKUP_HIGHLIGHT, quad); highlight.Color = color; highlight.Title = stTittle; stamper.AddAnnotation(highlight, items[iPosition].Page + iPageAdd); } else { while (iCount > 0 && items[iPosition].IsImage) { TextItem item = items[iPosition]; Rectangle rect = new Rectangle(item.MinX, item.MaxY, item.MaxX, item.MinY); PdfArray vertices = new PdfArray(); float rHalfWidth = 4; vertices.Add(new PdfNumber(item.MinX - rHalfWidth)); vertices.Add(new PdfNumber(item.MinY - rHalfWidth)); vertices.Add(new PdfNumber(item.MaxX + rHalfWidth)); vertices.Add(new PdfNumber(item.MinY - rHalfWidth)); vertices.Add(new PdfNumber(item.MaxX + rHalfWidth)); vertices.Add(new PdfNumber(item.MaxY + rHalfWidth)); vertices.Add(new PdfNumber(item.MinX - rHalfWidth)); vertices.Add(new PdfNumber(item.MaxY + rHalfWidth)); vertices.Add(new PdfNumber(item.MinX - rHalfWidth)); vertices.Add(new PdfNumber(item.MinY - rHalfWidth)); PdfAnnotation highlight = PdfAnnotation.CreatePolygonPolyline(stamper.Writer, rect, stComment, true, vertices); highlight.Color = color; highlight.Title = stTittle; highlight.Border = new PdfBorderArray(0, 0, rHalfWidth * 2); stamper.AddAnnotation(highlight, item.Page + iPageAdd); iPosition++; iCount--; } if (iCount == 0) { return; } int i; for (i = iPosition; i < iPosition + iCount; i++) { if (items[i].IsImage) { MarkText(items, iPosition, i - iPosition, stamper, color, iPageAdd, stComment); MarkText(items, i, 1, stamper, color, iPageAdd, stComment); if (i + 1 >= iPosition + iCount) { return; } iCount -= (i - iPosition) + 1; iPosition = i + 1; } } if (!items[iPosition].IsNewLine) { i = iPosition; while (i < iPosition + iCount - 1 && !items[i + 1].IsNewLine) { i++; } Rectangle rect = new Rectangle(items[iPosition].MinX, items[iPosition].MaxY, items[i].MaxX, items[iPosition].MinY); float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, stComment, PdfAnnotation.MARKUP_HIGHLIGHT, quad); highlight.Color = color; highlight.Title = stTittle; stamper.AddAnnotation(highlight, items[iPosition].Page + iPageAdd); iCount -= (i - iPosition) + 1; iPosition = i + 1; } if (iCount == 0) { return; } if (items.Count > iPosition + iCount && !items[iPosition + iCount].IsNewLine) { i = iPosition + iCount - 1; while (!items[i].IsNewLine) { i--; } Rectangle rect = new Rectangle(items[i].MinX, items[i].MaxY, items[i = iPosition + iCount - 1].MaxX, items[i].MinY); float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, stComment, PdfAnnotation.MARKUP_HIGHLIGHT, quad); highlight.Color = color; highlight.Title = stTittle; stamper.AddAnnotation(highlight, items[iPosition].Page + iPageAdd); iCount -= iPosition + iCount - i; } if (iCount == 0) { return; } i = iPosition; float rMinX = items[iPosition].MinX; float rMaxX = items[iPosition].MaxX; while (i < iPosition + iCount) { i++; if (i == iPosition + iCount || items[iPosition].Page != items[i].Page) { Rectangle rect = new Rectangle(rMinX, items[iPosition].MaxY, rMaxX, items[i - 1].MinY); float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, stComment, PdfAnnotation.MARKUP_HIGHLIGHT, quad); highlight.Color = color; highlight.Title = stTittle; stamper.AddAnnotation(highlight, items[iPosition].Page + iPageAdd); iCount -= i - iPosition; iPosition = i; if (iCount == 0) { return; } rMinX = items[iPosition].MinX; rMaxX = items[iPosition].MaxX; } if (items[i].MinX < rMinX) { rMinX = items[i].MinX; } if (items[i].MaxX > rMaxX) { rMaxX = items[i].MaxX; } } /* * for(i = iPosition; i < iPosition + iCount; i++) * { * TextItem item = items[i]; * Rectangle rect = new Rectangle(item.MinX, item.MaxY, item.MaxX, item.MinY); * float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; * PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, stComment, PdfAnnotation.MARKUP_HIGHLIGHT, quad); * highlight.Color = color; * highlight.Title = stTittle; * stamper.AddAnnotation(highlight, item.Page + iPageAdd); * } */ } }
public Dictionary <int, string> ExecuteHighlight(BackgroundWorker backgroundWorker) { OutputMg.OutputContent(backgroundWorker, "Starting highlight file " + pdfFilePath); List <string> topicTerms = ReadTargetTopicTerms.ParseTopicTerms(this.topicTermPath, this.targetTopicName); AddUserSearchTerms(topicTerms); string origiFile = pdfFilePath; //Create a new file from our test file with highlighting string highLightFile = highlightedPDFPath; int pdfNum = 0; PdfReader reader = new PdfReader(origiFile); using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfStamper stamper = new PdfStamper(reader, fs)) { using (var r = new PdfReader(origiFile)) { pdfNum = r.NumberOfPages; string ex = ""; ITextExtractionStrategy strategy; for (int i = 1; i <= pdfNum; i++) { OutputMg.OutputContent(backgroundWorker, "Parsing page: " + i); Rectangle pageRect = r.GetPageSize(i); Document doc = new Document(pageRect); float leftMargin = doc.LeftMargin; float rightMargin = doc.RightMargin; float lineWidth = pageRect.Width; var textPos = new FutherLocationTextExtractionStrategy(topicTerms); //Create an instance of our strategy ex = PdfTextExtractor.GetTextFromPage(r, i, textPos); //store the text and the position info in textPos List <iTextSharp.text.Rectangle> quadList = new List <iTextSharp.text.Rectangle>(); foreach (var p in textPos.myPoints) { string p_text = p.Text; iTextSharp.text.Rectangle rect = p.Rect; quadList.Add(rect);//collect the coordination of keywords } List <string> pageContent = new List <string>(); if (quadList.Count > 0) { List <iTextSharp.text.Rectangle> orderedRect = orderRectByBottom(quadList); //merge and adjust the rectangle, highlight the adjusted rect List <iTextSharp.text.Rectangle> adjustedRect = adjustRect(orderedRect, lineWidth, leftMargin); foreach (Rectangle rect in adjustedRect) { //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 //the co-ordination of four points 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; stamper.AddAnnotation(highlight, i); // i is the page //get the text of highlighting RenderFilter[] filter = { new RegionTextRenderFilter(rect) }; strategy = new MyFilteredTextRenderListener(new LocationTextExtractionStrategy(), filter); string text = PdfTextExtractor.GetTextFromPage(reader, i, strategy).Trim(); if (!pageContent.Contains(text)) { pageContent.Add(text); } } StringBuilder sb = new StringBuilder(); foreach (string tmp in pageContent) { sb.AppendLine(tmp); } pageContents.Add(i, sb.ToString()); } } } } } return(pageContents); }