Ejemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    doc.InitSecurityHandler();

                    pdftron.PDF.Page newPg = doc.PageCreate();
                    doc.PagePushBack(newPg);

                    Ink ink = Ink.Create(doc, new pdftron.PDF.Rect(110, 10, 300, 200));
                    pdftron.PDF.Point pt3 = new pdftron.PDF.Point(110, 10);
                    //pt3.x = 110; pt3.y = 10;
                    ink.SetPoint(0, 0, pt3);
                    pt3.x = 150; pt3.y = 50;
                    ink.SetPoint(0, 1, pt3);
                    pt3.x = 190; pt3.y = 60;
                    ink.SetPoint(0, 2, pt3);
                    pt3.x = 180; pt3.y = 90;
                    ink.SetPoint(1, 0, pt3);
                    pt3.x = 190; pt3.y = 95;
                    ink.SetPoint(1, 1, pt3);
                    pt3.x = 200; pt3.y = 100;
                    ink.SetPoint(1, 2, pt3);
                    pt3.x = 166; pt3.y = 86;
                    ink.SetPoint(2, 0, pt3);
                    pt3.x = 196; pt3.y = 96;
                    ink.SetPoint(2, 1, pt3);
                    pt3.x = 221; pt3.y = 121;
                    ink.SetPoint(2, 2, pt3);
                    pt3.x = 288; pt3.y = 188;
                    ink.SetPoint(2, 3, pt3);
                    ink.SetColor(new ColorPt(0, 1, 1), 3);
                    newPg.AnnotPushBack(ink);

                    // Save as a linearized file which is most popular
                    // and effective format for quick PDF Viewing.
                    doc.Save("linearized_output.pdf", SDFDoc.SaveOptions.e_linearized);

                    System.Console.WriteLine("Done. Results saved in linearized_output.pdf");

                    MessageBox.Show("Done. Results saved in linearized_output.pdf", "Document Creation");
                }
            }
            catch (PDFNetException ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }
        static void CreateHighlightAnnots(Annotation annotation, System.Drawing.Color highlightColor, Document document, List <Rect> coveredRectsBefore, out List <Rect> coveredRectsAfter)
        {
            List <Quad> quads = annotation.Quads.Where(q => q.IsContainer == false).ToList();
            List <int>  pages = quads.Select(q => q.PageIndex).ToList().Distinct().ToList();

            pages.Sort();

            KnowledgeItem quotation = (KnowledgeItem)annotation.EntityLinks.Where(e => e.Indication == EntityLink.PdfKnowledgeItemIndication).FirstOrDefault().Source;

            string quotationText = quotation.Text;

            if (string.IsNullOrEmpty(quotationText))
            {
                quotationText = quotation.CoreStatement;
            }
            bool TextAlreadyWrittenToAnAnnotation = false;

            coveredRectsAfter = coveredRectsBefore;

            double currentR = highlightColor.R;
            double currentG = highlightColor.G;
            double currentB = highlightColor.B;
            double currentA = highlightColor.A;

            ColorPt colorPt = new ColorPt(
                currentR / 255,
                currentG / 255,
                currentB / 255
                );

            double highlightOpacity = currentA / 255;

            foreach (int pageIndex in pages)
            {
                pdftron.PDF.Page page = document.GetPage(pageIndex);
                if (page == null)
                {
                    continue;
                }
                if (!page.IsValid())
                {
                    continue;
                }
                List <Quad> quadsOnThisPage = annotation.Quads.Where(q => q.PageIndex == pageIndex && q.IsContainer == false).Distinct().ToList();

                List <Rect> boxes = new List <Rect>();

                foreach (Quad quad in quadsOnThisPage)
                {
                    boxes.Add(new Rect(quad.MinX, quad.MinY, quad.MaxX, quad.MaxY));
                }

                Annot newAnnot = null;

                // If we want to make the later annotation invisible, we should uncomment the following if then else, and comment out the line after that

                if (boxes.Select(b => new { b.x1, b.y1, b.x2, b.y2 }).Intersect(coveredRectsAfter.Select(b => new { b.x1, b.y1, b.x2, b.y2 })).Count() == boxes.Count())
                {
                    newAnnot = CreateSingleHighlightAnnot(document, boxes, colorPt, 0);
                }
                else
                {
                    newAnnot = CreateSingleHighlightAnnot(document, boxes, colorPt, highlightOpacity);
                }

                // newAnnot = CreateSingleHighlightAnnot(document, boxes, colorPt, highlightOpacity);

                if (!TextAlreadyWrittenToAnAnnotation)
                {
                    newAnnot.SetContents(quotationText);
                    TextAlreadyWrittenToAnAnnotation = true;
                }

                page.AnnotPushBack(newAnnot);

                if (newAnnot.IsValid() == false)
                {
                    continue;
                }
                if (newAnnot.GetAppearance() == null)
                {
                    newAnnot.RefreshAppearance();
                }

                coveredRectsAfter.AddRange(boxes);
            }
        }