public override void Run( ) { // 1. Opening the PDF file... string filePath = PromptFileChoice("Please select a PDF file"); using (files::File file = new files::File(filePath)) { Document document = file.Document; // 2. Link extraction from the document pages. TextExtractor extractor = new TextExtractor(); extractor.AreaTolerance = 2; // 2 pt tolerance on area boundary detection. bool linkFound = false; foreach (Page page in document.Pages) { if (!PromptNextPage(page, !linkFound)) { Quit(); break; } IDictionary <RectangleF?, IList <ITextString> > textStrings = null; linkFound = false; // Get the page annotations! PageAnnotations annotations = page.Annotations; if (!annotations.Exists()) { Console.WriteLine("No annotations here."); continue; } // Iterating through the page annotations looking for links... foreach (Annotation annotation in annotations) { if (annotation is Link) { linkFound = true; if (textStrings == null) { textStrings = extractor.Extract(page); } Link link = (Link)annotation; RectangleF linkBox = link.Box; // Text. /* * Extracting text superimposed by the link... * NOTE: As links have no strong relation to page text but a weak location correspondence, * we have to filter extracted text by link area. */ StringBuilder linkTextBuilder = new StringBuilder(); foreach (ITextString linkTextString in extractor.Filter(textStrings, linkBox)) { linkTextBuilder.Append(linkTextString.Text); } Console.WriteLine("Link '" + linkTextBuilder + "' "); // Position. Console.WriteLine( " Position: " + "x:" + Math.Round(linkBox.X) + "," + "y:" + Math.Round(linkBox.Y) + "," + "w:" + Math.Round(linkBox.Width) + "," + "h:" + Math.Round(linkBox.Height) ); // Target. Console.Write(" Target: "); PdfObjectWrapper target = link.Target; if (target is Destination) { PrintDestination((Destination)target); } else if (target is actions::Action) { PrintAction((actions::Action)target); } else if (target == null) { Console.WriteLine("[not available]"); } else { Console.WriteLine("[unknown type: " + target.GetType().Name + "]"); } } } if (!linkFound) { Console.WriteLine("No links here."); continue; } } } }
/** * <summary>Replaces the Acroform fields with their corresponding graphics representation.</summary> * <param name="document">Document to flatten.</param> */ public void Flatten(Document document) { Dictionary <Page, PageStamper> pageStampers = new Dictionary <Page, PageStamper>(); Form form = document.Form; Fields formFields = form.Fields; foreach (Field field in formFields.Values) { foreach (Widget widget in field.Widgets) { Page widgetPage = widget.Page; AnnotationFlagsEnum flags = widget.Flags; // Is the widget to be rendered? if (((flags & AnnotationFlagsEnum.Hidden) == 0 || hiddenRendered) && ((flags & AnnotationFlagsEnum.Print) > 0 || nonPrintableRendered)) { // Stamping the current state appearance of the widget... PdfName widgetCurrentState = (PdfName)widget.BaseDataObject[PdfName.AS]; FormXObject widgetCurrentAppearance = widget.Appearance.Normal[widgetCurrentState]; if (widgetCurrentAppearance != null) { PageStamper widgetStamper; if (!pageStampers.TryGetValue(widgetPage, out widgetStamper)) { pageStampers[widgetPage] = widgetStamper = new PageStamper(widgetPage); } SKRect widgetBox = widget.Box; widgetStamper.Foreground.ShowXObject(widgetCurrentAppearance, widgetBox.Location, widgetBox.Size); } } // Removing the widget from the page annotations... PageAnnotations widgetPageAnnotations = widgetPage.Annotations; widgetPageAnnotations.Remove(widget); if (widgetPageAnnotations.Count == 0) { widgetPage.Annotations = null; widgetPageAnnotations.Delete(); } // Removing the field references relating the widget... PdfDictionary fieldPartDictionary = widget.BaseDataObject; while (fieldPartDictionary != null) { PdfDictionary parentFieldPartDictionary = (PdfDictionary)fieldPartDictionary.Resolve(PdfName.Parent); PdfArray kidsArray; if (parentFieldPartDictionary != null) { kidsArray = (PdfArray)parentFieldPartDictionary.Resolve(PdfName.Kids); } else { kidsArray = formFields.BaseDataObject; } kidsArray.Remove(fieldPartDictionary.Reference); fieldPartDictionary.Delete(); if (kidsArray.Count > 0) { break; } fieldPartDictionary = parentFieldPartDictionary; } } } if (formFields.Count == 0) { // Removing the form root... document.Form = null; form.Delete(); } foreach (PageStamper pageStamper in pageStampers.Values) { pageStamper.Flush(); } }