Exemplo n.º 1
0
        private static void AnnotationHighLevelAPI(PDFDoc doc)
        {
            // The following code snippet traverses all annotations in the document
            System.Console.WriteLine("Traversing all annotations in the document...");

            string uri;
            int    page_num = 1;

            for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
            {
                System.Console.WriteLine("Page " + page_num++ + ": ");

                Page page       = itr.Current();
                int  num_annots = page.GetNumAnnots();
                for (int i = 0; i < num_annots; ++i)
                {
                    Annot annot = page.GetAnnot(i);
                    if (!annot.IsValid())
                    {
                        continue;
                    }
                    System.Console.WriteLine("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName());

                    Rect bbox = annot.GetRect();
                    System.Console.WriteLine("  Position: " + bbox.x1
                                             + ", " + bbox.y1
                                             + ", " + bbox.x2
                                             + ", " + bbox.y2);

                    switch (annot.GetType())
                    {
                    case Annot.Type.e_Link:
                    {
                        Link   lnk    = new Link(annot);
                        Action action = lnk.GetAction();
                        if (!action.IsValid())
                        {
                            continue;
                        }
                        if (action.GetType() == Action.Type.e_GoTo)
                        {
                            Destination dest = action.GetDest();
                            if (!dest.IsValid())
                            {
                                System.Console.WriteLine("  Destination is not valid");
                            }
                            else
                            {
                                int pg_num = dest.GetPage().GetIndex();
                                System.Console.WriteLine("  Links to: page number " + pg_num + " in this document");
                            }
                        }
                        else if (action.GetType() == Action.Type.e_URI)
                        {
                            uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText();
                            System.Console.WriteLine("  Links to: " + uri);
                        }
                        // ...
                    }
                    break;

                    case Annot.Type.e_Widget:
                        break;

                    case Annot.Type.e_FileAttachment:
                        break;

                    // ...
                    default:
                        break;
                    }
                }
            }

            // Use the high-level API to create new annotations.
            Page first_page = doc.GetPage(1);

            // Create a hyperlink...
            Link hyperlink = Link.Create(doc, new Rect(85, 570, 503, 524), Action.CreateURI(doc, "http://www.pdftron.com"));

            first_page.AnnotPushBack(hyperlink);

            // Create an intra-document link...
            Action goto_page_3 = Action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0));
            Link   link        = Link.Create(doc, new Rect(85, 458, 503, 502), goto_page_3);

            // Set the annotation border width to 3 points...
            Annot.BorderStyle border_style = new Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 3, 0, 0);
            //link.SetBorderStyle(border_style);
            link.SetColor(new ColorPt(0, 0, 1));

            // Add the new annotation to the first page
            first_page.AnnotPushBack(link);

            // Create a stamp annotation ...
            RubberStamp stamp = RubberStamp.Create(doc, new Rect(30, 30, 300, 200));

            stamp.SetIcon("Draft");
            first_page.AnnotPushBack(stamp);

            // Create a file attachment annotation (embed the 'peppers.jpg').
            FileAttachment file_attach = FileAttachment.Create(doc, new Rect(80, 280, 200, 320), (input_path + "peppers.jpg"));

            first_page.AnnotPushBack(file_attach);


            Ink   ink = Ink.Create(doc, new Rect(110, 10, 300, 200));
            Point pt3 = new 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);
            first_page.AnnotPushBack(ink);
        }
        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);
            }
        }