Exemplo n.º 1
0
        /**
         * This is the most simple way to change a PDF into a
         * portable collection. Choose one of the following names:
         * <ul>
         * <li>PdfName.D (detailed view)
         * <li>PdfName.T (tiled view)
         * <li>PdfName.H (hidden)
         * </ul>
         * Pass this name as a parameter and your PDF will be
         * a portable collection with all the embedded and
         * attached files as entries.
         * @param initialView can be PdfName.D, PdfName.T or PdfName.H
         */
        public void MakePackage(PdfName initialView)
        {
            PdfCollection collection = new PdfCollection(0);

            collection.Put(PdfName.VIEW, initialView);
            stamper.MakePackage(collection);
        }
Exemplo n.º 2
0
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            using (PdfDocument pdf = new PdfDocument(@"..\Sample Data\ImageScaleAndRotate.pdf"))
            {
                PdfCollection <PdfPaintedImage> paintedImages = pdf.Pages[0].GetPaintedImages();

                PdfPaintedImage image = paintedImages[0];

                // save image as is
                image.Image.Save("PdfImage.Save");

                // save image as painted
                var options = new PdfPaintedImageSavingOptions
                {
                    Format = PdfExtractedImageFormat.Tiff,
                    HorizontalResolution = 300,
                    VerticalResolution   = 300
                };
                image.SaveAsPainted("PdfPaintedImage.SaveAsPainted.tiff", options);
            }

            Console.WriteLine($"The output is located in {Environment.CurrentDirectory}");
        }
        private static string getTextFromLink(PdfGoToAction linkAction)
        {
            PdfPage targetPage = linkAction.View.Page;

            if (targetPage == null)
            {
                return(String.Empty);
            }

            StringBuilder result = new StringBuilder();

            const float eps = 5.0f; // small reserve for text start vertical position
            PdfCollection <PdfTextData> textFromTargetPage = targetPage.Canvas.GetTextData();

            foreach (PdfTextData textData in textFromTargetPage)
            {
                if (textData.Position.Y < targetPage.Height - linkAction.View.Top - eps)
                {
                    continue;
                }

                result.Append(textData.GetText() + " ");
            }

            return(result.ToString());
        }
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            using (PdfDocument pdf = new PdfDocument(@"Sample Data\BorderPinksOranges.pdf"))
            {
                PdfCollection <PdfLayer> layers = pdf.Layers;
                foreach (PdfLayer layer in layers)
                {
                    string message = string.Format("Name = {0}\nVisible = {1}\nIntents = ",
                                                   layer.Name, layer.Visible);

                    foreach (PdfLayerIntent intent in layer.GetIntents())
                    {
                        message += intent.ToString();
                        message += " ";
                    }

                    System.Windows.Forms.MessageBox.Show(message, "Layer Info");
                }
            }
        }
Exemplo n.º 5
0
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            using (PdfDocument pdf = new PdfDocument(@"Sample Data\ImageScaleAndRotate.pdf"))
            {
                PdfCollection <PdfPaintedImage> paintedImages = pdf.Pages[0].GetPaintedImages();

                PdfPaintedImage image = paintedImages[0];

                // save image as is
                string imageAsIs = "PdfImage.Save";
                string fullPath  = image.Image.Save(imageAsIs);
                Process.Start(fullPath);

                // save image as painted
                // NOTE: PdfPaintedImage.SaveAsPainted() method is not supported in version for .NET Standard
                string imageAsPainted = "PdfPaintedImage.SaveAsPainted.tiff";
                image.SaveAsPainted(imageAsPainted, PdfExtractedImageFormat.Tiff);
                Process.Start(imageAsPainted);
            }
        }
Exemplo n.º 6
0
    public void Write(Stream stream)
    {
        using (Document document = new Document()) {
            PdfWriter writer = PdfWriter.GetInstance(document, stream);

            document.Open();
            document.Add(new Paragraph("This document contains a collection of PDFs"));

            PdfIndirectReference parentFolderObjectReference = writer.PdfIndirectReference;
            PdfIndirectReference childFolder1ObjectReference = writer.PdfIndirectReference;
            PdfIndirectReference childFolder2ObjectReference = writer.PdfIndirectReference;

            PdfDictionary parentFolderObject = GetFolderDictionary(0);
            parentFolderObject.Put(new PdfName("Child"), childFolder1ObjectReference);
            parentFolderObject.Put(PdfName.NAME, new PdfString());

            PdfDictionary childFolder1Object = GetFolderDictionary(1);
            childFolder1Object.Put(PdfName.NAME, new PdfString("Folder 1"));
            childFolder1Object.Put(PdfName.PARENT, parentFolderObjectReference);
            childFolder1Object.Put(PdfName.NEXT, childFolder2ObjectReference);

            PdfDictionary childFolder2Object = GetFolderDictionary(2);
            childFolder2Object.Put(PdfName.NAME, new PdfString("Folder 2"));
            childFolder2Object.Put(PdfName.PARENT, parentFolderObjectReference);

            PdfCollection       collection = new PdfCollection(PdfCollection.DETAILS);
            PdfCollectionSchema schema     = CollectionSchema();
            collection.Schema = schema;
            collection.Sort   = new PdfCollectionSort(keys);
            collection.Put(new PdfName("Folders"), parentFolderObjectReference);
            writer.Collection = collection;

            PdfFileSpecification fs;
            PdfCollectionItem    item;

            fs   = PdfFileSpecification.FileEmbedded(writer, file1Path, File1, null);
            item = new PdfCollectionItem(schema);
            item.AddItem("Type", "pdf");
            fs.AddCollectionItem(item);
            // the description is apparently used to place the
            // file in a particular folder.  The number between the < and >
            // is used to put the file in the folder that has the matching id
            fs.AddDescription(GetDescription(1, File1), false);
            writer.AddFileAttachment(fs);

            fs   = PdfFileSpecification.FileEmbedded(writer, file2Path, File2, null);
            item = new PdfCollectionItem(schema);
            item.AddItem("Type", "pdf");
            fs.AddCollectionItem(item);
            fs.AddDescription(GetDescription(2, File2), false);
            writer.AddFileAttachment(fs);

            writer.AddToBody(parentFolderObject, parentFolderObjectReference);
            writer.AddToBody(childFolder1Object, childFolder1ObjectReference);
            writer.AddToBody(childFolder2Object, childFolder2ObjectReference);

            document.Close();
        }
    }
Exemplo n.º 7
0
// ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            // step 1
            using (Document document = new Document()) {
                // step 2
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
                // step 3
                document.Open();
                // step 4
                document.Add(new Paragraph(
                                 "This document contains a collection of PDFs,"
                                 + " one per Stanley Kubrick movie."
                                 ));

                PdfCollection       collection = new PdfCollection(PdfCollection.DETAILS);
                PdfCollectionSchema schema     = _collectionSchema();
                collection.Schema = schema;
                PdfCollectionSort sort = new PdfCollectionSort("YEAR");
                sort.SetSortOrder(false);
                collection.Sort            = sort;
                collection.InitialDocument = "Eyes Wide Shut";
                writer.Collection          = collection;

                PdfCollectionItem   item;
                IEnumerable <Movie> movies = PojoFactory.GetMovies(1);
                foreach (Movie movie in movies)
                {
                    PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(
                        writer, null,
                        String.Format("kubrick_{0}.pdf", movie.Imdb),
                        CreateMoviePage(movie)
                        );
                    fs.AddDescription(movie.Title, false);

                    item = new PdfCollectionItem(schema);
                    item.AddItem("TITLE", movie.GetMovieTitle(false));
                    if (movie.GetMovieTitle(true) != null)
                    {
                        item.SetPrefix("TITLE", movie.GetMovieTitle(true));
                    }
                    item.AddItem("DURATION", movie.Duration.ToString());
                    item.AddItem("YEAR", movie.Year.ToString());
                    fs.AddCollectionItem(item);
                    writer.AddFileAttachment(fs);
                }
            }
        }
Exemplo n.º 8
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            doc.Add(new Paragraph("Portable collection"));

            PdfCollection collection = new PdfCollection();

            collection.SetView(PdfCollection.TILE);
            pdfDoc.GetCatalog().SetCollection(collection);

            AddFileAttachment(pdfDoc, DATA, "united_states.csv");
            AddFileAttachment(pdfDoc, HELLO, "hello.pdf");
            AddFileAttachment(pdfDoc, IMG, "berlin2013.jpg");

            doc.Close();
        }
Exemplo n.º 9
0
 public static void Main()
 {
     using (PdfDocument document = new PdfDocument("DocumentName.pdf"))
     {
         PdfCollection <PdfWidget> widgets = document.Pages[0].Widgets;
         foreach (PdfWidget widget in widgets)
         {
             PdfComboBox comboBox = widget as PdfComboBox;
             if (comboBox != null)
             {
                 foreach (string item in comboBox.Items)
                 {
                     // do something with combo box option
                 }
             }
         }
     }
 }
Exemplo n.º 10
0
        public void CreatePdf(String dest)
        {
            Document  document = new Document();
            PdfWriter writer   = PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create));

            document.Open();
            document.Add(new Paragraph("Portable collection"));
            PdfCollection collection = new PdfCollection(PdfCollection.TILE);

            writer.Collection = collection;
            PdfFileSpecification fileSpec = PdfFileSpecification.FileEmbedded(writer, DATA, "united_states.csv", null);

            writer.AddFileAttachment("united_states.csv", fileSpec);
            fileSpec = PdfFileSpecification.FileEmbedded(writer, HELLO, "hello.pdf", null);
            writer.AddFileAttachment("hello.pdf", fileSpec);
            fileSpec = PdfFileSpecification.FileEmbedded(writer, IMG, "berlin2013.jpg", null);
            writer.AddFileAttachment("berlin2013.jpg", fileSpec);
            document.Close();
        }
Exemplo n.º 11
0
        public void Write(Stream stream, string[] pfade)
        {
            using (Document document = new Document())
            {
                PdfWriter writer = PdfWriter.GetInstance(document, stream);

                document.Open();
                document.Add(new Paragraph(" "));

                PdfIndirectReference parentFolderObjectReference = writer.PdfIndirectReference;

                PdfCollection       collection = new PdfCollection(PdfCollection.DETAILS);
                PdfCollectionSchema schema     = CollectionSchema();
                collection.Schema = schema;
                collection.Sort   = new PdfCollectionSort(keys);
                collection.Put(new PdfName("Vorlagen BA"), parentFolderObjectReference);
                writer.Collection = collection;

                PdfFileSpecification fs;
                PdfCollectionItem    item;

                int nummer = 1;

                foreach (string pfad in pfade)
                {
                    String Filename = Path.GetFileName(pfad);
                    fs   = PdfFileSpecification.FileEmbedded(writer, pfad, string.Format("{0} - {1}", nummer, Filename), null);
                    item = new PdfCollectionItem(schema);
                    item.AddItem("Type", "pdf");
                    fs.AddCollectionItem(item);
                    fs.AddDescription(GetDescription(Filename), false);
                    writer.AddFileAttachment(fs);

                    nummer++;
                }

                document.Close();
            }
        }
Exemplo n.º 12
0
 /**
  * Adds or replaces the Collection Dictionary in the Catalog.
  * @param    collection  the new collection dictionary.
  */
 public void MakePackage(PdfCollection collection)
 {
     stamper.MakePackage(collection);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Sets collection dictionary that a conforming reader shall use to enhance the presentation of file attachments
 /// stored in the PDF document.
 /// </summary>
 /// <param name="collection"/>
 public virtual iText.Kernel.Pdf.PdfCatalog SetCollection(PdfCollection collection)
 {
     Put(PdfName.Collection, collection.GetPdfObject());
     return(this);
 }
Exemplo n.º 14
0
// ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            // step 1
            using (Document document = new Document()) {
                // step 2
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
                // step 3
                document.Open();


                // step 4
                PdfCollection       collection = new PdfCollection(PdfCollection.HIDDEN);
                PdfCollectionSchema schema     = _collectionSchema();
                collection.Schema = schema;
                PdfCollectionSort sort = new PdfCollectionSort(KEYS);
                collection.Sort   = sort;
                writer.Collection = collection;

                PdfCollectionItem    collectionitem = new PdfCollectionItem(schema);
                PdfFileSpecification fs             = PdfFileSpecification.FileEmbedded(
                    writer, IMG_KUBRICK, "kubrick.jpg", null
                    );
                fs.AddDescription("Stanley Kubrick", false);
                collectionitem.AddItem(TYPE_FIELD, "JPEG");
                fs.AddCollectionItem(collectionitem);
                writer.AddFileAttachment(fs);

                Image img = Image.GetInstance(IMG_BOX);
                document.Add(img);
                List           list = new List(List.UNORDERED, 20);
                PdfDestination dest = new PdfDestination(PdfDestination.FIT);
                dest.AddFirst(new PdfNumber(1));
                PdfTargetDictionary intermediate;
                PdfTargetDictionary target;
                Chunk     chunk;
                ListItem  item;
                PdfAction action = null;

                IEnumerable <Movie> box = PojoFactory.GetMovies(1)
                                          .Concat(PojoFactory.GetMovies(4))
                ;
                StringBuilder sb = new StringBuilder();
                foreach (Movie movie in box)
                {
                    if (movie.Year > 1960)
                    {
                        sb.AppendLine(String.Format(
                                          "{0};{1};{2}", movie.MovieTitle, movie.Year, movie.Duration
                                          ));
                        item = new ListItem(movie.MovieTitle);
                        if (!"0278736".Equals(movie.Imdb))
                        {
                            target = new PdfTargetDictionary(true);
                            target.EmbeddedFileName          = movie.Title;
                            intermediate                     = new PdfTargetDictionary(true);
                            intermediate.FileAttachmentPage  = 1;
                            intermediate.FileAttachmentIndex = 1;
                            intermediate.AdditionalPath      = target;
                            action = PdfAction.GotoEmbedded(null, intermediate, dest, true);
                            chunk  = new Chunk(" (see info)");
                            chunk.SetAction(action);
                            item.Add(chunk);
                        }
                        list.Add(item);
                    }
                }
                document.Add(list);

                fs = PdfFileSpecification.FileEmbedded(
                    writer, null, "kubrick.txt",
                    Encoding.UTF8.GetBytes(sb.ToString())
                    );
                fs.AddDescription("Kubrick box: the movies", false);
                collectionitem.AddItem(TYPE_FIELD, "TXT");
                fs.AddCollectionItem(collectionitem);
                writer.AddFileAttachment(fs);

                PdfPTable table = new PdfPTable(1);
                table.SpacingAfter = 10;
                PdfPCell cell = new PdfPCell(new Phrase("All movies by Kubrick"));
                cell.Border = PdfPCell.NO_BORDER;
                fs          = PdfFileSpecification.FileEmbedded(
                    writer, null, KubrickMovies.FILENAME,
                    Utility.PdfBytes(new KubrickMovies())
                    //new KubrickMovies().createPdf()
                    );
                collectionitem.AddItem(TYPE_FIELD, "PDF");
                fs.AddCollectionItem(collectionitem);
                target = new PdfTargetDictionary(true);
                target.FileAttachmentPagename = "movies";
                target.FileAttachmentName     = "The movies of Stanley Kubrick";
                cell.CellEvent = new PdfActionEvent(
                    writer, PdfAction.GotoEmbedded(null, target, dest, true)
                    );
                cell.CellEvent = new FileAttachmentEvent(
                    writer, fs, "The movies of Stanley Kubrick"
                    );
                cell.CellEvent = new LocalDestinationEvent(writer, "movies");
                table.AddCell(cell);
                writer.AddFileAttachment(fs);

                cell        = new PdfPCell(new Phrase("Kubrick DVDs"));
                cell.Border = PdfPCell.NO_BORDER;
                fs          = PdfFileSpecification.FileEmbedded(
                    writer, null,
                    KubrickDvds.RESULT, new KubrickDvds().CreatePdf()
                    );
                collectionitem.AddItem(TYPE_FIELD, "PDF");
                fs.AddCollectionItem(collectionitem);
                cell.CellEvent = new FileAttachmentEvent(writer, fs, "Kubrick DVDs");
                table.AddCell(cell);
                writer.AddFileAttachment(fs);

                cell        = new PdfPCell(new Phrase("Kubrick documentary"));
                cell.Border = PdfPCell.NO_BORDER;
                fs          = PdfFileSpecification.FileEmbedded(
                    writer, null,
                    KubrickDocumentary.RESULT, new KubrickDocumentary().CreatePdf()
                    );
                collectionitem.AddItem(TYPE_FIELD, "PDF");
                fs.AddCollectionItem(collectionitem);
                cell.CellEvent = new FileAttachmentEvent(
                    writer, fs, "Kubrick Documentary"
                    );
                table.AddCell(cell);
                writer.AddFileAttachment(fs);

                document.NewPage();
                document.Add(table);
            }
        }
Exemplo n.º 15
0
 public void MakePackage(PdfCollection collection);