Esempio n. 1
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. Extracting attachments...
            // 2.1. Embedded files (document level).
            foreach(KeyValuePair<PdfString,FileSpecification> entry in document.Names.EmbeddedFiles)
            {EvaluateDataFile(entry.Value);}

            // 2.2. File attachments (page level).
            foreach(Page page in document.Pages)
            {
              foreach(Annotation annotation in page.Annotations)
              {
            if(annotation is FileAttachment)
            {EvaluateDataFile(((FileAttachment)annotation).DataFile);}
              }
            }
              }
        }
Esempio n. 2
0
 /**
  * <summary>Serializes the given PDF Clown file object.</summary>
  * <param name="file">PDF file to serialize.</param>
  * <param name="fileName">Output file name.</param>
  * <param name="serializationMode">Serialization mode.</param>
  * <returns>Serialization path.</returns>
  */
 protected string Serialize(
     files::File file,
     string fileName,
     files::SerializationModeEnum?serializationMode
     )
 {
     return(Serialize(file, fileName, serializationMode, null, null, null));
 }
Esempio n. 3
0
 /**
  * <summary>Serializes the given PDF Clown file object.</summary>
  * <param name="file">PDF file to serialize.</param>
  * <param name="title">Document title.</param>
  * <param name="subject">Document subject.</param>
  * <param name="keywords">Document keywords.</param>
  * <returns>Serialization path.</returns>
  */
 protected string Serialize(
     files::File file,
     string title,
     string subject,
     string keywords
     )
 {
     return(Serialize(file, null, title, subject, keywords));
 }
Esempio n. 4
0
 /**
  * <summary>Serializes the given PDF Clown file object.</summary>
  * <param name="file">PDF file to serialize.</param>
  * <param name="serializationMode">Serialization mode.</param>
  * <param name="title">Document title.</param>
  * <param name="subject">Document subject.</param>
  * <param name="keywords">Document keywords.</param>
  * <returns>Serialization path.</returns>
  */
 protected string Serialize(
     files::File file,
     files::SerializationModeEnum?serializationMode,
     string title,
     string subject,
     string keywords
     )
 {
     return(Serialize(file, GetType().Name, serializationMode, title, subject, keywords));
 }
        public override void Run()
        {
            // 1. Creating the document...
            files::File file     = new files::File();
            Document    document = file.Document;

            // 2. Applying links...
            BuildLinks(document);

            // 3. Serialize the PDF file!
            Serialize(file, "Link annotations", "applying link annotations", "links, creation");
        }
Esempio n. 6
0
        public override void Run()
        {
            // 1. PDF file instantiation.
            files::File file     = new files::File();
            Document    document = file.Document;

            // 2. Content creation.
            Populate(document);

            // 3. Serialize the PDF file!
            Serialize(file, "Annotations", "inserting annotations", "annotations, creation, attachment, sticky notes, callout notes, rubber stamps, markup, highlighting");
        }
Esempio n. 7
0
        public override void Run()
        {
            // 1. Instantiate a new PDF file!
            files::File file     = new files::File();
            Document    document = file.Document;

            // 2. Insert the contents into the document!
            Populate(document);

            // 3. Serialize the PDF file!
            Serialize(file, "Unicode", "using Unicode fonts", "Unicode");
        }
Esempio n. 8
0
        public override void Run()
        {
            // 1. PDF file instantiation.
            files::File file     = new files::File();
            Document    document = file.Document;

            // 2. Content creation.
            Build(document);

            // 3. Serialize the PDF file!
            Serialize(file, "Typesetting", "demonstrating how to add style to contents", "typesetting");
        }
Esempio n. 9
0
        public override void Run()
        {
            // 1. PDF file instantiation.
            files::File file     = new files::File();
            Document    document = file.Document;

            // 2. Content creation.
            Populate(document);

            // 3. PDF file serialization.
            Serialize(file, "Layer", "inserting layers", "layers, optional content");
        }
Esempio n. 10
0
        public override void Run(
            )
        {
            // 1. PDF file instantiation.
            files::File file     = new files::File();
            Document    document = file.Document;

            // 2. Content creation.
            Populate(document);

            // 3. Serialize the PDF file!
            Serialize(file, "Annotations", "inserting annotations", "annotations, creation, attachment, note, callout");
        }
Esempio n. 11
0
        public override void Run(
            )
        {
            // 1. Instantiate a new PDF file!
              files::File file = new files::File();
              Document document = file.Document;

              // 2. Insert the contents into the document!
              Populate(document);

              // 3. Serialize the PDF file!
              Serialize(file, "Unicode", "using Unicode fonts", "Unicode");
        }
Esempio n. 12
0
        public override void Run(
            )
        {
            // 1. PDF file instantiation.
              files::File file = new files::File();
              Document document = file.Document;

              // 2. Content creation.
              Populate(document);

              // 3. Serialize the PDF file!
              Serialize(file, "Annotations", "inserting annotations", "annotations, creation, attachment, note, callout");
        }
Esempio n. 13
0
        public override void Run(
            )
        {
            // 1. Creating the document...
              files::File file = new files::File();
              Document document = file.Document;

              // 2. Applying links...
              BuildLinks(document);

              // 3. Serialize the PDF file!
              Serialize(file, "Link annotations", "applying link annotations", "links, creation");
        }
Esempio n. 14
0
        public override void Run(
            )
        {
            // 1. PDF file instantiation.
              files::File file = new files::File();
              Document document = file.Document;

              // 2. Content creation.
              Populate(document);

              // 3. Serialize the PDF file!
              Serialize(file, "Inline image", "embedding an image within a content stream", "inline image");
        }
Esempio n. 15
0
        public override void Run(
            )
        {
            // 1. PDF file instantiation.
            files::File file     = new files::File();
            Document    document = file.Document;

            // 2. Content creation.
            Populate(document);

            // 3. Serialize the PDF file!
            Serialize(file, "Inline image", "embedding an image within a content stream", "inline image");
        }
Esempio n. 16
0
        public override void Run(
            )
        {
            // 1. PDF file instantiation.
              files::File file = new files::File();
              Document document = file.Document;

              // 2. Content creation.
              Build(document);

              // 3. Serialize the PDF file!
              Serialize(file, "Typesetting", "demonstrating how to add style to contents", "typesetting");
        }
Esempio n. 17
0
        /**
         * <summary>Serializes the given PDF Clown file object.</summary>
         * <param name="file">PDF file to serialize.</param>
         * <param name="fileName">Output file name.</param>
         * <param name="serializationMode">Serialization mode.</param>
         * <param name="title">Document title.</param>
         * <param name="subject">Document subject.</param>
         * <param name="keywords">Document keywords.</param>
         * <returns>Serialization path.</returns>
         */
        protected string Serialize(
            files::File file,
            string fileName,
            files::SerializationModeEnum?serializationMode,
            string title,
            string subject,
            string keywords
            )
        {
            ApplyDocumentSettings(file.Document, title, subject, keywords);

            Console.WriteLine();

            if (!serializationMode.HasValue)
            {
                if (file.Reader == null) // New file.
                {
                    serializationMode = files::SerializationModeEnum.Standard;
                }
                else // Existing file.
                {
                    Console.WriteLine("[0] Standard serialization");
                    Console.WriteLine("[1] Incremental update");
                    Console.Write("Please select a serialization mode: ");
                    try
                    { serializationMode = (files::SerializationModeEnum)Int32.Parse(Console.ReadLine()); }
                    catch
                    { serializationMode = files::SerializationModeEnum.Standard; }
                }
            }

            string outputFilePath = outputPath + fileName + "." + serializationMode + ".pdf";

            // Save the file!

            /*
             * NOTE: You can also save to a generic target stream (see Save() method overloads).
             */
            try
            { file.Save(outputFilePath, serializationMode.Value); }
            catch (Exception e)
            {
                Console.WriteLine("File writing failed: " + e.Message);
                Console.WriteLine(e.StackTrace);
            }
            Console.WriteLine("\nOutput: " + outputFilePath);

            return(outputFilePath);
        }
Esempio n. 18
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. Replace the images!
            ReplaceImages(document);

            // 3. Serialize the PDF file!
            Serialize(file, "Image substitution", "substituting a document's images", "image replacement");
              }
        }
        public override void Run(
            )
        {
            // 1. Opening the PDF file...
            string filePath = PromptFileChoice("Please select a PDF file");

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

                // 2. Replace the images!
                ReplaceImages(document);

                // 3. Serialize the PDF file!
                Serialize(file, "Image substitution", "substituting a document's images", "image replacement");
            }
        }
Esempio n. 20
0
        public override void Run(
            )
        {
            // 1. Open the PDF file!
              string filePath = PromptFileChoice("Please select a PDF file");
              using(files::File file = new files::File(filePath))
              {
            Document document = file.Document;

            // 2. Create a watermark!
            FormXObject watermark = CreateWatermark(document);

            // 3. Apply the watermark to the pages of the document!
            ApplyWatermark(watermark);

            // 4. Serialize the PDF file!
            Serialize(file, "Watermark", "how to place some content behind existing pages", "watermark, transparent text, background, foreground, page, composition");
              }
        }
        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))
            {
                // 2. Iterating through the indirect object collection...
                int index = 0;
                foreach (PdfIndirectObject indirectObject in file.IndirectObjects)
                {
                    // Get the data object associated to the indirect object!
                    PdfDataObject dataObject = indirectObject.DataObject;
                    // Is this data object a stream?
                    if (dataObject is PdfStream)
                    {
                        PdfDictionary header = ((PdfStream)dataObject).Header;
                        // Is this stream an image?
                        if (header.ContainsKey(PdfName.Type) &&
                            header[PdfName.Type].Equals(PdfName.XObject) &&
                            header[PdfName.Subtype].Equals(PdfName.Image))
                        {
                            // Which kind of image?
                            if (header[PdfName.Filter].Equals(PdfName.DCTDecode)) // JPEG image.
                            {
                                // Get the image data (keeping it encoded)!
                                IBuffer body = ((PdfStream)dataObject).GetBody(false);
                                // Export the image!
                                ExportImage(
                                    body,
                                    "ImageExtractionSample_" + (index++) + ".jpg"
                                    );
                            }
                            else // Unsupported image.
                            {
                                Console.WriteLine("Image XObject " + indirectObject.Reference + " couldn't be extracted (filter: " + header[PdfName.Filter] + ")");
                            }
                        }
                    }
                }
            }
        }
Esempio n. 22
0
        public override void Run()
        {
            // 1. Open the PDF file!
            string filePath = PromptFileChoice("Please select a PDF file");

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

                // 2. Create a watermark!
                FormXObject watermark = CreateWatermark(document);

                // 3. Apply the watermark to the pages of the document!
                ApplyWatermark(watermark);

                // 4. Serialize the PDF file!
                Serialize(file, "Watermark", "how to place some content behind existing pages", "watermark, transparent text, background, foreground, page, composition");
            }
        }
Esempio n. 23
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))
       {
     // 2. Iterating through the indirect object collection...
     int index = 0;
     foreach(PdfIndirectObject indirectObject in file.IndirectObjects)
     {
       // Get the data object associated to the indirect object!
       PdfDataObject dataObject = indirectObject.DataObject;
       // Is this data object a stream?
       if(dataObject is PdfStream)
       {
     PdfDictionary header = ((PdfStream)dataObject).Header;
     // Is this stream an image?
     if(header.ContainsKey(PdfName.Type)
       && header[PdfName.Type].Equals(PdfName.XObject)
       && header[PdfName.Subtype].Equals(PdfName.Image))
     {
       // Which kind of image?
       if(header[PdfName.Filter].Equals(PdfName.DCTDecode)) // JPEG image.
       {
         // Get the image data (keeping it encoded)!
         IBuffer body = ((PdfStream)dataObject).GetBody(false);
         // Export the image!
         ExportImage(
           body,
           "ImageExtractionSample_" + (index++) + ".jpg"
           );
       }
       else // Unsupported image.
       {Console.WriteLine("Image XObject " + indirectObject.Reference + " couldn't be extracted (filter: " + header[PdfName.Filter] + ")");}
     }
       }
     }
       }
 }
Esempio n. 24
0
        public override void Run(
            )
        {
            // 1. PDF file instantiation.
              files::File file = new files::File();
              Document document = file.Document;
              // Set default page size (A4)!
              document.PageSize = PageFormat.GetSize();

              // 2. Content creation.
              DateTime creationDate = DateTime.Now;
              // 2.1. Template.
              FormXObject template = BuildTemplate(document, creationDate);
              // 2.2. Welcome page.
              BuildWelcomePage(document, template);
              // 2.3. Free Software definition.
              BuildFreeSoftwareDefinitionPages(document, template);
              // 2.4. Bookmarks.
              BuildBookmarks(document);

              // 3. Serialization.
              Serialize(file, "Complex Typesetting", "complex typesetting", "typesetting, bookmarks, hyphenation, block composer, primitive composer, text alignment, image insertion, article threads");
        }
Esempio n. 25
0
        public override void Run()
        {
            // 1. PDF file instantiation.
            files::File file     = new files::File();
            Document    document = file.Document;

            // Set default page size (A4)!
            document.PageSize = PageFormat.GetSize();

            // 2. Content creation.
            DateTime creationDate = DateTime.Now;
            // 2.1. Template.
            FormXObject template = BuildTemplate(document, creationDate);

            // 2.2. Welcome page.
            BuildWelcomePage(document, template);
            // 2.3. Free Software definition.
            BuildFreeSoftwareDefinitionPages(document, template);
            // 2.4. Bookmarks.
            BuildBookmarks(document);

            // 3. Serialization.
            Serialize(file, "Complex Typesetting", "complex typesetting", "typesetting, bookmarks, hyphenation, block composer, primitive composer, text alignment, image insertion, article threads");
        }
Esempio n. 26
0
        public override void Run(
            )
        {
            // 1. Externalizing the streams...
            string externalizedFilePath;
            {
                // 1.1. Opening the PDF file...
                string filePath = PromptFileChoice("Please select a PDF file");
                using (var file = new files::File(filePath))
                {
                    Document document = file.Document;

                    /*
                     * NOTE: As we are going to export streams using paths relative to the output path, it's
                     * necessary to ensure they are properly resolved (otherwise they will be written relative to
                     * the current user directory).
                     */
                    file.Path = OutputPath;

                    // 1.2. Iterating through the indirect objects to externalize streams...
                    int filenameIndex = 0;
                    foreach (PdfIndirectObject indirectObject in file.IndirectObjects)
                    {
                        PdfDataObject dataObject = indirectObject.DataObject;
                        if (dataObject is PdfStream)
                        {
                            PdfStream stream = (PdfStream)dataObject;
                            if (stream.DataFile == null) // Internal stream to externalize.
                            {
                                stream.SetDataFile(
                                    FileSpecification.Get(
                                        document,
                                        GetType().Name + "-external" + filenameIndex++
                                        ),
                                    true // Forces the stream data to be transferred to the external location.
                                    );
                            }
                        }
                    }

                    // 1.3. Serialize the PDF file!
                    externalizedFilePath = Serialize(file, files::SerializationModeEnum.Standard);
                }
            }

            // 2. Reimporting the externalized streams...
            {
                // 2.1. Opening the PDF file...
                using (var file = new files::File(externalizedFilePath))
                {
                    // 2.2. Iterating through the indirect objects to internalize streams...
                    foreach (PdfIndirectObject indirectObject in file.IndirectObjects)
                    {
                        PdfDataObject dataObject = indirectObject.DataObject;
                        if (dataObject is PdfStream)
                        {
                            PdfStream stream = (PdfStream)dataObject;
                            if (stream.DataFile != null) // External stream to internalize.
                            {
                                stream.SetDataFile(
                                    null,
                                    true // Forces the stream data to be transferred to the internal location.
                                    );
                            }
                        }
                    }

                    // 2.3. Serialize the PDF file!
                    string externalizedFileName = Path.GetFileNameWithoutExtension(externalizedFilePath);
                    string internalizedFilePath = externalizedFileName + "-reimported.pdf";
                    Serialize(file, internalizedFilePath, files::SerializationModeEnum.Standard);
                }
            }
        }
Esempio n. 27
0
        public override void Run(
            )
        {
            // 1. Externalizing the streams...
              string externalizedFilePath;
              {
            // 1.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;
              /*
            NOTE: As we are going to export streams using paths relative to the output path, it's
            necessary to ensure they are properly resolved (otherwise they will be written relative to
            the current user directory).
              */
              file.Path = OutputPath;

              // 1.2. Iterating through the indirect objects to externalize streams...
              int filenameIndex = 0;
              foreach(PdfIndirectObject indirectObject in file.IndirectObjects)
              {
            PdfDataObject dataObject = indirectObject.DataObject;
            if(dataObject is PdfStream)
            {
              PdfStream stream = (PdfStream)dataObject;
              if(stream.DataFile == null) // Internal stream to externalize.
              {
                stream.SetDataFile(
                  FileSpecification.Get(
                    document,
                    GetType().Name + "-external" + filenameIndex++
                    ),
                  true // Forces the stream data to be transferred to the external location.
                  );
              }
            }
              }

              // 1.3. Serialize the PDF file!
              externalizedFilePath = Serialize(file, files::SerializationModeEnum.Standard);
            }
              }

              // 2. Reimporting the externalized streams...
              {
            // 2.1. Opening the PDF file...
            using(files::File file = new files::File(externalizedFilePath))
            {
              // 2.2. Iterating through the indirect objects to internalize streams...
              foreach(PdfIndirectObject indirectObject in file.IndirectObjects)
              {
            PdfDataObject dataObject = indirectObject.DataObject;
            if(dataObject is PdfStream)
            {
              PdfStream stream = (PdfStream)dataObject;
              if(stream.DataFile != null) // External stream to internalize.
              {
                stream.SetDataFile(
                  null,
                  true // Forces the stream data to be transferred to the internal location.
                  );
              }
            }
              }

              // 2.3. Serialize the PDF file!
              string externalizedFileName = Path.GetFileNameWithoutExtension(externalizedFilePath);
              string internalizedFilePath = externalizedFileName + "-reimported.pdf";
              Serialize(file, internalizedFilePath, files::SerializationModeEnum.Standard);
            }
              }
        }
Esempio n. 28
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;
              }
            }
              }
        }
Esempio n. 29
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;
                    }
                }
            }
        }
Esempio n. 30
0
 /**
  * <summary>Serializes the given PDF Clown file object.</summary>
  * <param name="file">PDF file to serialize.</param>
  * <returns>Serialization path.</returns>
  */
 protected string Serialize(
     files::File file
     )
 {
     return(Serialize(file, null, null, null));
 }