Highlight keywords in a pdf using itextsharp and render it to the browser

十年热恋 提交于 2020-01-06 19:32:28

问题


I have an existing pdf .I am using itextSharp to open the document and highlight keywords dynamically and when I save this into a file it works fine, but when I write it into a memory Stream and try to render it on the browser the highlights are not there.

Here is the code

public void SearchPDF()
    {


        //Create a new file from our test file with highlighting
        string highLightFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Highlighted.pdf");
        // Stream
        //Bind a reader and stamper to our test PDF


        var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "201400699428__1_00000.pdf");

        PdfReader reader = new PdfReader(testFile);

        var numberOfPages = reader.NumberOfPages;
        System.Globalization.CompareOptions cmp = System.Globalization.CompareOptions.None;
        //Create an instance of our strategy

        MemoryStream m = new MemoryStream();

        //using (var fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None))
        //{
            using (Document document = new Document(PageSize.A4))
            {
                PdfWriter.GetInstance(document, m);

                using (PdfStamper stamper = new PdfStamper(reader, m))
                {
                    //document.Open();
                    for (var currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        MyLocationTextExtractionStrategy strategyTest = new MyLocationTextExtractionStrategy("Ritual Bath", cmp);
                        var listener = new MyRenderListener();
                        ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

                        //Parse page 1 of the document above
                        using (var r = new PdfReader(testFile))
                        {
                            var ex = PdfTextExtractor.GetTextFromPage(r, currentPageIndex, strategyTest);
                        }

                        //Loop through each chunk found

                        foreach (var p in strategyTest.myPoints)
                        {

                            //Console.WriteLine(string.Format("Found text {0} at {1}x{2}", p.Text, p.Rect.Left, p.Rect.Bottom));
                            float[] quad = { p.Rect.Left, p.Rect.Bottom, p.Rect.Right, p.Rect.Bottom, p.Rect.Left, p.Rect.Top, p.Rect.Right, p.Rect.Top };

                            Rectangle rect = new Rectangle(p.Rect.Left,
                                                           p.Rect.Top,
                                                           p.Rect.Bottom,
                                                           p.Rect.Right);

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


             //Response.ContentType = "application/pdf";
             //Response.AddHeader("content-disposition", "attachment; filename=download_report.pdf");
             //Response.BinaryWrite(byteContent);
             //Response.End();
        HttpContext.Current.Response.Buffer = false;
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline;filename=download_report.pdf");
        HttpContext.Current.Response.AppendHeader("Content-Length", m.ToArray().Length.ToString());
        HttpContext.Current.Response.OutputStream.Write(m.ToArray(), 0, m.ToArray().Length);
        HttpContext.Current.Response.OutputStream.Flush();
        HttpContext.Current.Response.OutputStream.Close();
        HttpContext.Current.Response.End();
    }

来源:https://stackoverflow.com/questions/29032422/highlight-keywords-in-a-pdf-using-itextsharp-and-render-it-to-the-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!