使用itextsharp突出显示pdf中的关键字并将其呈现给浏览器

时间:2015-03-13 12:35:40

标签: pdf itextsharp highlighting memorystream

我有一个现有的pdf。我正在使用itextSharp打开文档并动态突出显示关键字,当我将其保存到文件中时,它工作正常,但当我将其写入内存Stream并尝试在浏览器上呈现它时亮点不存在。

这是代码

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

0 个答案:

没有答案