protected void AddTextDataToPdfPage(PDFDoc pdfDoc, pdftron.PDF.Page pdfPage, string text)
        {
            var elementWriter = new ElementWriter();

            try
            {
                elementWriter.Begin(pdfPage);

                var elementBuilder = new ElementBuilder();
                var textFont       = Font.Create(pdfDoc, Font.StandardType1Font.e_times_roman);
                var textFontSize   = 10.0;
                var textElement    = elementBuilder.CreateTextBegin(textFont, textFontSize);
                elementWriter.WriteElement(textElement);
                elementBuilder.CreateTextRun(text);
                elementWriter.WriteElement(textElement);
                var textEnd = elementBuilder.CreateTextEnd();
                elementWriter.WriteElement(textEnd);

                elementWriter.Flush();
                elementWriter.End();
            }
            finally
            {
                elementWriter.Dispose();
            }
        }
Пример #2
0
        // NOTE: Overlay two PDF files and give the visual color diff between them
        // Used to review two document revisions or drawing blueprints that are overlayed with updated data
        private async void ProcessVisualDiff()
        {
            var docA = mPDFViewCtrlA.GetDoc();
            var docB = mPDFViewCtrlB.GetDoc();

            DiffOptions diffOptions = new DiffOptions();

            diffOptions.SetColorA(new ColorPt(1, 0, 0));
            diffOptions.SetColorB(new ColorPt(0, 0, 0));
            diffOptions.SetBlendMode(GStateBlendMode.e_bl_normal);

            pdftron.PDF.Page docAPage = docA.GetPage(1);
            pdftron.PDF.Page docBPage = docB.GetPage(1);

            try
            {
                PDFDoc outputDocument = new PDFDoc();
                outputDocument.AppendVisualDiff(docAPage, docBPage, diffOptions);

                Controls.PDFResultDialog contentDialog = new Controls.PDFResultDialog();
                var pdfCompare = new PDFCompareResult(outputDocument);
                pdfCompare.CloseButtonClicked += delegate
                {
                    contentDialog.Hide();
                };

                contentDialog.Content = pdfCompare;
                await contentDialog.ShowAsync();
            }
            catch (Exception ex)
            { }
        }
        public static void DeleteExistingHighlights(Document document)
        {
            for (int i = 1; i <= document.GetPageCount(); i++)
            {
                pdftron.PDF.Page page = document.GetPage(i);
                int annotCount        = page.GetNumAnnots();

                List <Annot> annotsToDelete = new List <Annot>();

                for (int j = 0; j < page.GetNumAnnots(); j++)
                {
                    Annot annot = page.GetAnnot(j);
                    if (annot.GetSDFObj() != null &&
                        annot.GetType() == Annot.Type.e_Highlight)
                    {
                        annotsToDelete.Add(annot);
                    }
                    else
                    {
                    }
                }

                foreach (Annot annot in annotsToDelete)
                {
                    page.AnnotRemove(annot);
                }
            }
        }
        public static List <ColorPt> ExistingColors(this Document document)
        {
            List <ColorPt> existingColorPts = new List <ColorPt>();

            for (int i = 1; i <= document.GetPageCount(); i++)
            {
                pdftron.PDF.Page page = document.GetPage(i);
                if (page.IsValid())
                {
                    for (int j = 0; j < page.GetNumAnnots(); j++)
                    {
                        Annot annot = page.GetAnnot(j);

                        if (annot.GetSDFObj() != null && (annot.GetType() == Annot.Type.e_Highlight || annot.GetType() == Annot.Type.e_Unknown))
                        {
                            ColorPt existingColorPt = annot.GetColorAsRGB();
                            if (existingColorPts.Where(e =>
                                                       e.Get(0) == existingColorPt.Get(0) &&
                                                       e.Get(1) == existingColorPt.Get(1) &&
                                                       e.Get(2) == existingColorPt.Get(2) &&
                                                       e.Get(3) == existingColorPt.Get(3)
                                                       ).Count() == 0)
                            {
                                existingColorPts.Add(existingColorPt);
                            }
                        }
                    }
                }
            }
            return(existingColorPts);
        }
Пример #5
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);
            }
        }
        public static void AnnotationsImport(QuotationType quotationType)
        {
            // Static Variables

            PreviewControl previewControl = PreviewMethods.GetPreviewControl();

            if (previewControl == null)
            {
                return;
            }

            PdfViewControl pdfViewControl = previewControl.GetPdfViewControl();

            if (pdfViewControl == null)
            {
                return;
            }

            Document document = pdfViewControl.Document;

            if (document == null)
            {
                return;
            }

            Location location = previewControl.ActiveLocation;

            if (location == null)
            {
                return;
            }

            List <Reference> references = Program.ActiveProjectShell.Project.References.Where(r => r.Locations.Contains(location)).ToList();

            if (references == null)
            {
                return;
            }
            if (references.Count != 1)
            {
                MessageBox.Show("The document is not linked with exactly one reference. Import aborted.");
            }

            Reference reference = references.FirstOrDefault();

            if (references == null)
            {
                return;
            }

            LinkedResource linkedResource = location.Address;

            string pathToFile = linkedResource.Resolve().LocalPath;

            PreviewBehaviour previewBehaviour = location.PreviewBehaviour;

            // Dynamic Variables

            string colorPickerCaption = string.Empty;

            List <ColorPt> selectedColorPts = new List <ColorPt>();

            int annotationsImportedCount = 0;

            bool ImportEmptyAnnotations = false;
            bool RedrawAnnotations      = true;

            // The Magic

            Form commentAnnotationsColorPicker = new AnnotationsImporterColorPicker(quotationType, document.ExistingColors(), out selectedColorPts);

            DialogResult dialogResult = commentAnnotationsColorPicker.ShowDialog();

            RedrawAnnotations      = AnnotationsImporterColorPicker.RedrawAnnotationsSelected;
            ImportEmptyAnnotations = AnnotationsImporterColorPicker.ImportEmptyAnnotationsSelected;

            if (dialogResult == DialogResult.Cancel)
            {
                MessageBox.Show("Import of external highlights cancelled.");
                return;
            }

            List <Annotation> temporaryAnnotations = new List <Annotation>();

            if (ImportEmptyAnnotations || quotationType == QuotationType.Comment)
            {
                for (int pageIndex = 1; pageIndex <= document.GetPageCount(); pageIndex++)
                {
                    pdftron.PDF.Page page = document.GetPage(pageIndex);
                    if (page.IsValid())
                    {
                        List <Annot> annotsToDelete = new List <Annot>();
                        for (int j = 0; j < page.GetNumAnnots(); j++)
                        {
                            Annot annot = page.GetAnnot(j);

                            if (annot.GetSDFObj() != null && (annot.GetType() == Annot.Type.e_Highlight || annot.GetType() == Annot.Type.e_Unknown))
                            {
                                Highlight highlight = new Highlight(annot);
                                if (highlight == null)
                                {
                                    continue;
                                }

                                ColorPt annotColorPt = annot.GetColorAsRGB();

                                if (selectedColorPts.Where(e =>
                                                           e.Get(0) == annotColorPt.Get(0) &&
                                                           e.Get(1) == annotColorPt.Get(1) &&
                                                           e.Get(2) == annotColorPt.Get(2) &&
                                                           e.Get(3) == annotColorPt.Get(3)
                                                           ).Count() == 0)
                                {
                                    continue;
                                }
                                Annotation temporaryAnnotation = highlight.TemporaryAnnotation();
                                location.Annotations.Add(temporaryAnnotation);
                                temporaryAnnotations.Add(temporaryAnnotation);
                            }
                        } // end for (int j = 1; j <= page.GetNumAnnots(); j++)
                    }     // end if (page.IsValid())
                }         // end for (int i = 1; i <= document.GetPageCount(); i++)

                previewControl.ShowNoPreview();
                location.PreviewBehaviour = PreviewBehaviour.SkipEntryPage;
                previewControl.ShowLocationPreview(location);
                document = new Document(pathToFile);
            }

            //Uncomment here to get an overview of all annotations
            //int x = 0;
            //foreach (Annotation a in location.Annotations)
            //{
            //    System.Diagnostics.Debug.WriteLine("Annotation " + x.ToString());
            //    int y = 0;
            //    foreach (Quad q in a.Quads)
            //    {
            //        System.Diagnostics.Debug.WriteLine("Quad " + y.ToString());
            //        System.Diagnostics.Debug.WriteLine("IsContainer: " + q.IsContainer.ToString());
            //        System.Diagnostics.Debug.WriteLine("MinX: " + q.MinX.ToString());
            //        System.Diagnostics.Debug.WriteLine("MinY: " + q.MinY.ToString());
            //        System.Diagnostics.Debug.WriteLine("MaxX: " + q.MaxX.ToString());
            //        System.Diagnostics.Debug.WriteLine("MaxY: " + q.MaxY.ToString());
            //        y = y + 1;
            //    }
            //    x = x + 1;
            //}

            int v = 0;

            for (int pageIndex = 1; pageIndex <= document.GetPageCount(); pageIndex++)
            {
                pdftron.PDF.Page page = document.GetPage(pageIndex);
                if (page.IsValid())
                {
                    List <Annot> annotsToDelete = new List <Annot>();
                    for (int j = 0; j < page.GetNumAnnots(); j++)
                    {
                        Annot annot = page.GetAnnot(j);

                        if (annot.GetSDFObj() != null && (annot.GetType() == Annot.Type.e_Highlight || annot.GetType() == Annot.Type.e_Unknown))
                        {
                            Highlight highlight = new Highlight(annot);
                            if (highlight == null)
                            {
                                continue;
                            }

                            ColorPt annotColorPt = annot.GetColorAsRGB();

                            if (selectedColorPts.Where(e =>
                                                       e.Get(0) == annotColorPt.Get(0) &&
                                                       e.Get(1) == annotColorPt.Get(1) &&
                                                       e.Get(2) == annotColorPt.Get(2) &&
                                                       e.Get(3) == annotColorPt.Get(3)
                                                       ).Count() == 0)
                            {
                                continue;
                            }

                            // Uncomment here to get an overview of all highlights
                            //System.Diagnostics.Debug.WriteLine("Highlight " + v.ToString());
                            //int w = 0;
                            //foreach (Quad q in highlight.AsAnnotationQuads())
                            //{
                            //    System.Diagnostics.Debug.WriteLine("Quad " + w.ToString());
                            //    System.Diagnostics.Debug.WriteLine("IsContainer: " + q.IsContainer.ToString());
                            //    System.Diagnostics.Debug.WriteLine("MinX: " + q.MinX.ToString());
                            //    System.Diagnostics.Debug.WriteLine("MinY: " + q.MinY.ToString());
                            //    System.Diagnostics.Debug.WriteLine("MaxX: " + q.MaxX.ToString());
                            //    System.Diagnostics.Debug.WriteLine("MaxY: " + q.MaxY.ToString());
                            //    w = w + 1;
                            //}
                            //v = v + 1;

                            if (highlight.UpdateExistingQuotation(quotationType, location))
                            {
                                annotationsImportedCount++;
                                continue;
                            }
                            if (highlight.CreateNewQuotationAndAnnotationFromHighlight(quotationType, ImportEmptyAnnotations, RedrawAnnotations, temporaryAnnotations))
                            {
                                annotationsImportedCount++;
                                continue;
                            }
                        }
                    } // end for (int j = 1; j <= page.GetNumAnnots(); j++)
                    foreach (Annot annot in annotsToDelete)
                    {
                        page.AnnotRemove(annot);
                    }
                } // end if (page.IsValid())
            }     // end for (int i = 1; i <= document.GetPageCount(); i++)

            foreach (Annotation annotation in temporaryAnnotations)
            {
                location.Annotations.Remove(annotation);
            }

            location.PreviewBehaviour = previewBehaviour;

            MessageBox.Show(annotationsImportedCount.ToString() + " highlights have been imported.");
        }