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); }
static Obj CreateHighlightAppearance(List <Rect> boxes, ColorPt highlightColor, double highlightOpacity, Document document) { var elementBuilder = new ElementBuilder(); elementBuilder.PathBegin(); boxes.ForEach(box => elementBuilder.Rect(box.x1 - 2, box.y1, box.x2 - box.x1, box.y2 - box.y1)); Element element = elementBuilder.PathEnd(); element.SetPathFill(true); element.SetPathStroke(false); GState elementGraphicState = element.GetGState(); elementGraphicState.SetFillColorSpace(ColorSpace.CreateDeviceRGB()); elementGraphicState.SetFillColor(highlightColor); elementGraphicState.SetFillOpacity(highlightOpacity); elementGraphicState.SetBlendMode(GState.BlendMode.e_bl_multiply); var elementWriter = new ElementWriter(); elementWriter.Begin(document); elementWriter.WriteElement(element); Obj highlightAppearance = elementWriter.End(); elementBuilder.Dispose(); elementWriter.Dispose(); Rect boundingBox = RectangleUnion(boxes); highlightAppearance.PutRect("BBox", boundingBox.x1, boundingBox.y1, boundingBox.x2, boundingBox.y2); highlightAppearance.PutName("Subtype", "Form"); return(highlightAppearance); }
private void ColorCheckboxClick(object sender, System.EventArgs e, CheckBox checkBox, ColorPt selectedColorPt, List <ColorPt> selectedColorPts, out List <ColorPt> selectedColorPtsAfter) { selectedColorPtsAfter = selectedColorPts; if (checkBox.Checked) { selectedColorPtsAfter.Add(selectedColorPt); } else { if (selectedColorPtsAfter.Where(s => s == selectedColorPt).ToList().Count > 0) { selectedColorPtsAfter.Remove(selectedColorPt); } } }
static Annot CreateSingleHighlightAnnot(Document document, List <Rect> boxes, ColorPt colorPt, double highlightOpacity) { Annot annotation = Annot.Create(document, Annot.Type.e_Highlight, RectangleUnion(boxes)); annotation.SetColor(colorPt); annotation.SetBorderStyle(new Annot.BorderStyle (Annot.BorderStyle.Style.e_solid, 1)); Obj quads = annotation.GetSDFObj().PutArray("QuadPoints"); List <Rect> lineRectangles = boxes.GroupBy(box => box.y1).Select(line => RectangleUnion(line.ToList())).ToList(); lineRectangles.ForEach(lineRect => PushBackBox(quads, lineRect)); annotation.SetAppearance(CreateHighlightAppearance(lineRectangles, colorPt, highlightOpacity, document)); return(annotation); }
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); } }
static public void ProcessText(ElementReader page_reader) { // Begin text element Console.WriteLine("Begin Text Block:"); Element element; while ((element = page_reader.Next()) != null) { switch (element.GetType()) { case Element.Type.e_text_end: // Finish the text block Console.WriteLine("End Text Block."); return; case Element.Type.e_text: { GState gs = element.GetGState(); ColorSpace cs_fill = gs.GetFillColorSpace(); ColorPt fill = gs.GetFillColor(); ColorPt outc = new ColorPt(); cs_fill.Convert2RGB(fill, outc); ColorSpace cs_stroke = gs.GetStrokeColorSpace(); ColorPt stroke = gs.GetStrokeColor(); Font font = gs.GetFont(); Console.Write("Font Name: "); Console.WriteLine(font.GetName()); // font.IsFixedWidth(); // font.IsSerif(); // font.IsSymbolic(); // font.IsItalic(); // ... // double word_spacing = gs.GetWordSpacing(); // double char_spacing = gs.GetCharSpacing(); // Use element.GetCTM() if you are interested in the CTM // (current transformation matrix). if (font.GetType() == Font.Type.e_Type3) { //type 3 font, process its data for (CharIterator itr = element.GetCharIterator(); itr.HasNext(); itr.Next()) { page_reader.Type3FontBegin(itr.Current()); ProcessElements(page_reader); page_reader.End(); } } else { Matrix2D ctm = element.GetCTM(); Matrix2D text_mtx = element.GetTextMatrix(); /* * Matrix2D mtx = ctm * text_mtx; * double font_sz_scale_factor = System.Math.Sqrt(mtx.m_b * mtx.m_b + mtx.m_d * mtx.m_d); * double font_size = gs.GetFontSize(); * Console.Write(" Font Size: {0:f}", font_sz_scale_factor * font_size); * * ColorPt font_color = gs.GetFillColor(); * ColorSpace cs = gs.GetFillColorSpace(); * * ColorPt rgb = new ColorPt(); * cs.Convert2RGB(font_color, rgb); * Color font_color_rgb = Color.FromArgb(255, (byte)(rgb.get_c(0)*255), * (byte)(rgb.get_c(1)*255), (byte)(rgb.get_c(2)*255)); * * * Console.WriteLine(" Font Color(RGB): red={0:d} green={1:d} blue={2:d}", * (byte)(rgb.Get(0)*255), * (byte)(rgb.Get(1)*255), * (byte)(rgb.Get(2)*255)); */ double x, y; int char_code; for (CharIterator itr = element.GetCharIterator(); itr.HasNext(); itr.Next()) { Console.Write("Character code: "); char_code = itr.Current().char_code; if (char_code >= 32 || char_code <= 127) { // Print if in ASCII range... Console.Write((char)char_code); } x = itr.Current().x; // character positioning information y = itr.Current().y; // To get the exact character positioning information you need to // concatenate current text matrix with CTM and then multiply // relative positioning coordinates with the resulting matrix. // Matrix2D mtx2 = ctm * text_mtx; mtx2.Mult(ref x, ref y); // Console.WriteLine(" Position: x={0:f} y={1:f}", x, y); } } Console.WriteLine(); break; } } } }
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."); }