Esempio n. 1
0
        public override void Run(
            )
        {
            // 1. Opening the PDF file...
              string filePath = PromptFileChoice("Please select a PDF file");
              using(File file = new File(filePath))
              {
            Document document = file.Document;

            PageStamper stamper = new PageStamper(); // NOTE: Page stamper is used to draw contents on existing pages.

            // 2. Iterating through the document pages...
            foreach(Page page in document.Pages)
            {
              Console.WriteLine("\nScanning page " + (page.Index+1) + "...\n");

              stamper.Page = page;

              Extract(
            new ContentScanner(page), // Wraps the page contents into a scanner.
            stamper.Foreground
            );

              stamper.Flush();
            }

            // 3. Decorated version serialization.
            Serialize(file);
              }
        }
Esempio n. 2
0
        private void Stamp(
            Document document
            )
        {
            // 1. Instantiate the stamper!
              /* NOTE: The PageStamper is optimized for dealing with pages. */
              PageStamper stamper = new PageStamper();

              // 2. Numbering each page...
              StandardType1Font font = new StandardType1Font(
            document,
            StandardType1Font.FamilyEnum.Courier,
            true,
            false
            );
              DeviceRGBColor redColor = DeviceRGBColor.Get(System.Drawing.Color.Red);
              int margin = 32;
              foreach(Page page in document.Pages)
              {
            // 2.1. Associate the page to the stamper!
            stamper.Page = page;

            // 2.2. Stamping the page number on the foreground...
            {
              PrimitiveComposer foreground = stamper.Foreground;

              foreground.SetFont(font,16);
              foreground.SetFillColor(redColor);

              SizeF pageSize = page.Size;
              int pageNumber = page.Index + 1;
              bool pageIsEven = (pageNumber % 2 == 0);
              foreground.ShowText(
            pageNumber.ToString(),
            new PointF(
              (pageIsEven
                ? margin
                : pageSize.Width - margin),
              pageSize.Height - margin
              ),
            (pageIsEven
              ? XAlignmentEnum.Left
              : XAlignmentEnum.Right),
            YAlignmentEnum.Bottom,
            0
            );
            }

            // 2.3. End the stamping!
            stamper.Flush();
              }
        }
Esempio n. 3
0
        private void ApplyWatermark(
            FormXObject watermark
            )
        {
            // 1. Instantiate the stamper!
              /* NOTE: The PageStamper is optimized for dealing with pages. */
              PageStamper stamper = new PageStamper();

              // 2. Inserting the watermark into each page of the document...
              foreach(Page page in watermark.Document.Pages)
              {
            // 2.1. Associate the page to the stamper!
            stamper.Page = page;

            // 2.2. Stamping the watermark on the foreground...
            // Get the content composer!
            PrimitiveComposer composer = stamper.Foreground;
            // Show the watermark into the page background!
            composer.ShowXObject(watermark);

            // 2.3. End the stamping!
            stamper.Flush();
              }
        }
Esempio n. 4
0
        public static void Main(string[] args)
        {
            if (args.Length != 3) {
                Console.WriteLine ("Usage: NbTest <background.pdf> <text> <output.pdf>");
                return;
            }

            if (!File.Exists (args [0])) {
                Console.Error.WriteLine ("Invalid text filename, it doesn't exist.");
                return;
            }

            string text = args [1];
            PDFFiles.File file = new PDFFiles.File (args [0]);
            Document document = file.Document;
            PageStamper stamper = new PageStamper (document.Pages [0]);
            PrimitiveComposer composer = stamper.Foreground;
            PDFFonts.Font font = PDFFonts.Font.Get (document, FONT_NAME);

            if (document.Layer == null) {
                document.Layer = new LayerDefinition (document);
            }

            document.PageMode = Document.PageModeEnum.Layers;

            Layer layer = new Layer(document, "Personalization");
            document.Layer.Layers.Add(layer);

            var margin = new RectangleF(MilimitersToPoints(2), MilimitersToPoints(1),
                                        MilimitersToPoints(6), MilimitersToPoints(1));
            //var frame = new RectangleF(MilimitersToPoints(0), MilimitersToPoints(179),
            //                           MilimitersToPoints(255), MilimitersToPoints(26));
            var frame = new RectangleF(MilimitersToPoints(105), MilimitersToPoints(155),
                                       MilimitersToPoints(100), MilimitersToPoints(26));
            var text_frame = new RectangleF(frame.X + margin.X,
                                            frame.Y + margin.Y,
                                            frame.Width - margin.Right,
                                            frame.Height - margin.Bottom);
            int font_size = GetFontSizeToFit(text, text_frame);

            composer.BeginLayer(layer);

            //composer.Rotate(90, new PointF(0, MilimitersToPoints(255)));

            composer.SetFillColor(new DeviceRGBColor(86f/255,175f/255,49f/255));
            composer.DrawRectangle(frame);
            composer.Fill();

            composer.SetFillColor(DeviceRGBColor.White);
            composer.SetFont(font, font_size);

            BlockComposer blockComposer = new BlockComposer(composer);
            blockComposer.Begin(text_frame, AlignmentXEnum.Center, AlignmentYEnum.Middle);
            blockComposer.ShowText(text);
            blockComposer.End();

            composer.End();
            stamper.Flush();

            file.Save(args [2], PDFFiles.SerializationModeEnum.Standard);

            file.Dispose();
        }
Esempio n. 5
0
        /**
         * <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;
                    Annotation.FlagsEnum flags = widget.Flags;
                    // Is the widget to be rendered?
                    if (((flags & Annotation.FlagsEnum.Hidden) == 0 || hiddenRendered) &&
                        ((flags & Annotation.FlagsEnum.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);
                            }

                            RectangleF 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();
            }
        }