예제 #1
0
파일: Names.cs 프로젝트: evertop/PDFClown
        public T Get <T>(
            PdfString key
            ) where T : PdfObjectWrapper
        {
            PdfObjectWrapper names = Get(typeof(T));

            return((T)names.GetType().GetProperty("Item", BindingFlags.Public | BindingFlags.Instance).GetValue(names, new object[] { key }));
        }
예제 #2
0
        public PdfObjectWrapper Get(
            Type type,
            PdfString key
            )
        {
            PdfObjectWrapper namedObjects = Get(type);

            return((PdfObjectWrapper)namedObjects.GetType().GetMethod("get_Item", BindingFlags.Public | BindingFlags.Instance).Invoke(namedObjects, new object[] { key }));
        }
예제 #3
0
        /**
         * <summary>Registers a named object.</summary>
         * <param name="name">Object name.</param>
         * <param name="object">Named object.</param>
         * <returns>Registered named object.</returns>
         */
        public T Register <T>(
            PdfString name,
            T @object
            ) where T : PdfObjectWrapper
        {
            PdfObjectWrapper namedObjects = Names.Get(@object.GetType());

            namedObjects.GetType().GetMethod("set_Item", BindingFlags.Public | BindingFlags.Instance).Invoke(namedObjects, new object[] { name, @object });
            return(@object);
        }
        private void PrintBookmarks(
            Bookmarks bookmarks
            )
        {
            if (bookmarks == null)
            {
                return;
            }

            foreach (Bookmark bookmark in bookmarks)
            {
                // Show current bookmark!
                Console.WriteLine("Bookmark '" + bookmark.Title + "'");
                Console.Write("    Target: ");
                PdfObjectWrapper target = bookmark.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 + "]");
                }

                // Show child bookmarks!
                PrintBookmarks(bookmark.Bookmarks);
            }
        }
예제 #5
0
        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;
                    }
                }
            }
        }