Exemplo n.º 1
0
        /// <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();
        }