Exemple #1
0
            public IUILayerNode Wrap(PdfDirectObject baseObject)
            {
                if (baseObject == null)
                {
                    return(null);
                }
                if (baseObject.Wrapper is IUILayerNode node)
                {
                    return(node);
                }
                PdfDataObject baseDataObject = baseObject.Resolve();

                if (baseDataObject is PdfDictionary)
                {
                    return(Wrap <Layer>(baseObject));
                }
                else if (baseDataObject is PdfArray)
                {
                    return(Wrap <LayerCollection>(baseObject));
                }
                else
                {
                    throw new ArgumentException(baseDataObject.GetType().Name + " is NOT a valid layer node.");
                }
            }
Exemple #2
0
    /**
      <summary>Gets the character map extracted from the given encoding object.</summary>
      <param name="encodingObject">Encoding object.</param>
    */
    public static IDictionary<ByteArray,int> Get(
      PdfDataObject encodingObject
      )
    {
      if(encodingObject == null)
        return null;

      if(encodingObject is PdfName) // Predefined CMap.
        return Get((PdfName)encodingObject);
      else if(encodingObject is PdfStream) // Embedded CMap file.
        return Get((PdfStream)encodingObject);
      else
        throw new NotSupportedException("Unknown encoding object type: " + encodingObject.GetType().Name);
    }
Exemple #3
0
        /**
         * <summary>Gets the character map extracted from the given encoding object.</summary>
         * <param name="encodingObject">Encoding object.</param>
         */
        public static CMap Get(PdfDataObject encodingObject)
        {
            if (encodingObject == null)
            {
                return(null);
            }

            if (encodingObject is PdfName pdfName) // Predefined CMap.
            {
                return(Get(pdfName));
            }
            else if (encodingObject is PdfStream pdfStream) // Embedded CMap file.
            {
                return(Get(pdfStream));
            }
            else
            {
                throw new NotSupportedException("Unknown encoding object type: " + encodingObject.GetType().Name);
            }
        }
Exemple #4
0
        private void FillObjects(
            PdfDataObject objectObject,
            ICollection <PdfObjectWrapper> objects
            )
        {
            PdfDataObject objectDataObject = PdfObject.Resolve(objectObject);

            if (objectDataObject is PdfArray) // Multiple objects.
            {
                foreach (PdfDirectObject itemObject in (PdfArray)objectDataObject)
                {
                    FillObjects(itemObject, objects);
                }
            }
            else // Single object.
            {
                if (objectDataObject is PdfDictionary) // Annotation.
                {
                    objects.Add(
                        Annotation.Wrap((PdfReference)objectObject)
                        );
                }
                else if (objectDataObject is PdfTextString) // Form field (associated to widget annotations).
                {
                    objects.Add(
                        Document.Form.Fields[
                            (string)((PdfTextString)objectDataObject).Value
                        ]
                        );
                }
                else // Invalid object type.
                {
                    throw new Exception(
                              "Invalid 'Hide' action target type (" + objectDataObject.GetType().Name + ").\n"
                              + "It should be either an annotation or a form field."
                              );
                }
            }
        }
        public static ILayerNode Wrap(
            PdfDirectObject baseObject
            )
        {
            if (baseObject == null)
            {
                return(null);
            }

            PdfDataObject baseDataObject = baseObject.Resolve();

            if (baseDataObject is PdfDictionary)
            {
                return(Layer.Wrap(baseObject));
            }
            else if (baseDataObject is PdfArray)
            {
                return(Layers.Wrap(baseObject));
            }
            else
            {
                throw new ArgumentException(baseDataObject.GetType().Name + " is NOT a valid layer node.");
            }
        }
        public override void Run(
            )
        {
            // 1. Opening the PDF file...
            string filePath = PromptFileChoice("Please select a PDF file");

            using (var file = new File(filePath))
            {
                Document document = file.Document;

                // 2. Parsing the document...
                // 2.1. Metadata.
                // 2.1.1. Basic metadata.
                Console.WriteLine("\nDocument information:");
                Information info = document.Information;
                if (info.Exists())
                {
                    foreach (KeyValuePair <PdfName, object> infoEntry in info)
                    {
                        Console.WriteLine(infoEntry.Key + ": " + infoEntry.Value);
                    }
                }
                else
                {
                    Console.WriteLine("No information available (Info dictionary doesn't exist).");
                }

                // 2.1.2. Advanced metadata.
                Console.WriteLine("\nDocument metadata (XMP):");
                Metadata metadata = document.Metadata;
                if (metadata.Exists())
                {
                    try
                    {
                        XmlDocument metadataContent = metadata.Content;
                        Console.WriteLine(ToString(metadataContent));
                    }
                    catch (Exception e)
                    { Console.WriteLine("Metadata extraction failed: " + e.Message); }
                }
                else
                {
                    Console.WriteLine("No metadata available (Metadata stream doesn't exist).");
                }

                Console.WriteLine("\nIterating through the indirect-object collection (please wait)...");

                // 2.2. Counting the indirect objects, grouping them by type...
                var objCounters = new SortedDictionary <string, int>();
                objCounters["xref free entry"] = 0;
                foreach (PdfIndirectObject obj in file.IndirectObjects)
                {
                    if (obj.IsInUse()) // In-use entry.
                    {
                        PdfDataObject dataObject = obj.DataObject;
                        string        typeName   = (dataObject != null ? dataObject.GetType().Name : "empty entry");
                        if (objCounters.ContainsKey(typeName))
                        {
                            objCounters[typeName]++;
                        }
                        else
                        {
                            objCounters[typeName] = 1;
                        }
                    }
                    else // Free entry.
                    {
                        objCounters["xref free entry"]++;
                    }
                }
                Console.WriteLine("\nIndirect objects partial counts (grouped by PDF object type):");
                foreach (KeyValuePair <string, int> keyValuePair in objCounters)
                {
                    Console.WriteLine(" " + keyValuePair.Key + ": " + keyValuePair.Value);
                }
                Console.WriteLine("Indirect objects total count: " + file.IndirectObjects.Count);

                // 2.3. Showing some page information...
                Pages pages     = document.Pages;
                int   pageCount = pages.Count;
                Console.WriteLine("\nPage count: " + pageCount);

                int pageIndex = (int)Math.Round(pageCount / 2d);
                Console.WriteLine("Mid page:");
                PrintPageInfo(pages[pageIndex], pageIndex);

                pageIndex++;
                if (pageIndex < pageCount)
                {
                    Console.WriteLine("Next page:");
                    PrintPageInfo(pages[pageIndex], pageIndex);
                }
            }
        }