Пример #1
0
        public static void AddTextSignature2PDF()
        {
            PdfFile     pdfFile  = new PdfFile();
            PdfDocument document = pdfFile.Import(File.ReadAllBytes("sample.pdf"));

            //Input your certificate and password
            X509Certificate2 certificate = new X509Certificate2("test.pfx", "iditect");

            //First you need create a SignatureField with unique name
            SignatureField signatureField = new SignatureField("iDiTect Sign Field");

            //Add signature object to a signature field, so we can add a visualization to it
            signatureField.Signature = new Signature(certificate);
            //Add signature info as need
            signatureField.Signature.Properties.Reason      = "Sign by iDiTect";
            signatureField.Signature.Properties.Location    = "World Wide Web";
            signatureField.Signature.Properties.ContactInfo = "1234567";

            //Apply a visible signature widiget to represent the contents of the signature field.
            SignatureWidget widget = signatureField.Widgets.AddWidget();

            //Set signature position and size
            widget.Rect = new System.Windows.Rect(new System.Windows.Point(200, 200), new System.Windows.Size(400, 100));
            widget.RecalculateContent();

            //Customize signature appearance, you can show all signature info, or just name or date
            PageContentBuilder signatureAppearance = new PageContentBuilder(widget.Content.NormalContentSource);

            signatureAppearance.Position.Translate(10, 0);
            signatureAppearance.DrawText("Digitally signed by " + certificate.GetNameInfo(X509NameType.SimpleName, true));
            signatureAppearance.Position.Translate(10, 20);
            signatureAppearance.DrawText("Reason: " + signatureField.Signature.Properties.Reason);
            signatureAppearance.Position.Translate(10, 40);
            signatureAppearance.DrawText("Location: " + signatureField.Signature.Properties.Location);
            signatureAppearance.Position.Translate(10, 60);
            signatureAppearance.DrawText("Contact: " + signatureField.Signature.Properties.ContactInfo);
            signatureAppearance.Position.Translate(10, 80);
            signatureAppearance.DrawText("Date: " + DateTime.Now.ToString());

            //Add this signature to first PDF page
            document.Pages[0].Annotations.Add(widget);
            document.AcroForm.FormFields.Add(signatureField);

            //Digital sign feature need the stream support reading, so you have to use ReadWrite mode here
            using (FileStream fs = new FileStream("signed.pdf", FileMode.Create, FileAccess.ReadWrite))
            {
                pdfFile.Export(document, fs);
            }
        }
Пример #2
0
        public static void Decrypt()
        {
            PdfFile     pdfFile = new PdfFile();
            PdfDocument document;

            pdfFile.ImportSettings.UserPassword = "******";

            using (FileStream fs = File.OpenRead("Encrypted.pdf"))
            {
                //Read pdf document from stream
                document = pdfFile.Import(fs);
            }

            int pageCount = document.Pages.Count;

            Console.WriteLine(pageCount);

            //Or you can make all processing and modifying to pdf file
            // Such as adding a text in the beginning of the 1st page
            PageContentBuilder builder = new PageContentBuilder(document.Pages[0]);

            builder.Position.Translate(50, 150);
            builder.DrawText("The PDF document is decrypted!");

            using (FileStream fs = File.Create("Decrypted.pdf"))
            {
                pdfFile.Export(document, fs);
            }
        }
Пример #3
0
        public static void AddText()
        {
            //This example is using page level builder, all the text will be add in current pdf page
            PdfDocument document = new PdfDocument();
            PdfPage     page     = document.Pages.AddPage();

            //Create page level builder
            PageContentBuilder builder = new PageContentBuilder(page);

            //Add text using builder's DrawText directly

            builder.TextState.FontSize     = 18;
            builder.GraphicState.FillColor = RgbColors.Black;
            //Set the position for text
            builder.Position.Translate(50, 50);
            builder.DrawText("Insert text directly in builder");

            //Add text using Block object

            //Set the position for first block
            builder.Position.Translate(50, 100);
            //Create the first block with different font style
            Block block = new Block();

            block.HorizontalAlignment = Editing.Flow.HorizontalAlignment.Left;
            block.TextState.FontSize  = 16;
            block.TextState.SetFont(new FontFamily("Times New Roman"));
            block.InsertText("This is a Normal style sentence with target font.");
            block.TextState.SetFont(new FontFamily("Times New Roman"), FontStyles.Italic, FontWeights.Bold);
            block.InsertText(" This is another sentence in Italic and Bold style with target font.");
            //Add first sentence to page, if you don't know how much height need to show the text, just using
            //PositiveInfinity property, our builder will measure the height depend on the given width automatically
            builder.DrawBlock(block, new Size(page.Size.Width - 50 * 2, double.PositiveInfinity));

            //Get the actual size used by the first block, it works after block drawn
            Size usedSize = block.DesiredSize;

            //Set the position for the second block
            builder.Position.Translate(50, 100 + usedSize.Height);
            //Crete the second block with different text color
            Block block2 = new Block();

            block2.GraphicState.FillColor = RgbColors.Black;
            block2.InsertText("Black Text,");
            block2.GraphicState.FillColor = new RgbColor(255, 0, 0);
            block2.InsertText(" Colorful Text.");

            //Set wanted line width to measure the size used by the second block
            Size needSize2 = block2.Measure(page.Size.Width - 50 * 2);

            //Add second sentence to page
            builder.DrawBlock(block2, needSize2);

            using (FileStream fs = File.OpenWrite("InsertText.pdf"))
            {
                PdfFile pdfFile = new PdfFile();
                pdfFile.Export(document, fs);
            }
        }
Пример #4
0
        public static void CreateFormField()
        {
            PdfDocument        document = new PdfDocument();
            PdfPage            page     = document.Pages.AddPage();
            PageContentBuilder builder  = new PageContentBuilder(page);

            double padding   = 10;
            double topOffset = 100;

            //Create CheckBox FormField
            CheckBoxField check = new CheckBoxField("checkBox1");

            check.IsChecked = true;
            //Add CheckBox to document fields
            document.AcroForm.FormFields.Add(check);
            //Draw CheckBox to page
            builder.Position.Translate(50, topOffset);
            builder.DrawWidget(check, new Size(10, 10));
            //Draw CheckBox description
            builder.Position.Translate(50 + 10 + padding, topOffset);
            builder.DrawText("CheckBox");

            //Create ComboBox FormField
            ComboBoxField combo = new ComboBoxField("comboBox1");

            combo.TextState.FontSize = 16;
            combo.Options.Add(new ChoiceOption("Combo 1"));
            combo.Options.Add(new ChoiceOption("Combo 2"));
            combo.Options.Add(new ChoiceOption("Combo 3"));
            combo.Value = combo.Options[2];
            //Add ComboBox to document fields
            document.AcroForm.FormFields.Add(combo);
            //Draw ComboBox to page
            topOffset += 30;
            builder.Position.Translate(50, topOffset);
            builder.DrawWidget(combo, new Size(100, 30));
            //Draw ComboBox description
            builder.Position.Translate(50 + 100 + padding, topOffset);
            builder.DrawText("ComboBox");

            //Create ListBox FormField
            ListBoxField list = new ListBoxField("listBox1");

            list.AllowMultiSelection = true;
            list.TextState.FontSize  = 16;
            list.Options.Add(new ChoiceOption("List 1"));
            list.Options.Add(new ChoiceOption("List 2"));
            list.Options.Add(new ChoiceOption("List 3"));
            list.Value = new ChoiceOption[] { list.Options[0], list.Options[2] };
            //Add ListBox to document fields
            document.AcroForm.FormFields.Add(list);
            //Draw ListBox to page
            topOffset += 30;
            builder.Position.Translate(50, topOffset);
            builder.DrawWidget(list, new Size(100, 100));
            //Draw ListBox description
            builder.Position.Translate(50 + 100 + padding, topOffset);
            builder.DrawText("ListBox");

            //Create RadioButton FormField
            RadioButtonField radio = new RadioButtonField("radioButton1");

            radio.Options.Add(new RadioOption("Radio 1"));
            radio.Options.Add(new RadioOption("Radio 2"));
            radio.Value = radio.Options[1];
            //Add RadioButton to document fields
            document.AcroForm.FormFields.Add(radio);
            //Draw RadioButton to page
            topOffset += 100;
            foreach (RadioOption option in radio.Options)
            {
                topOffset += 30;
                builder.Position.Translate(50, topOffset);
                builder.DrawWidget(radio, option, new Size(10, 10));
            }
            //Draw ListBox description
            builder.Position.Translate(50 + 10 + padding, topOffset);
            builder.DrawText("RadioButton");

            //Create TextBox FormField
            TextBoxField textBox = new TextBoxField("textBox1");

            textBox.TextState.FontSize = 16;
            textBox.Value = "Input...";
            //Add TextBox to document fields
            document.AcroForm.FormFields.Add(textBox);
            //Draw TextBox to page
            topOffset += 30;
            builder.Position.Translate(50, topOffset);
            builder.DrawWidget(textBox, new Size(100, 30));
            //Draw TextBox description
            builder.Position.Translate(50 + 100 + padding, topOffset);
            builder.DrawText("TextBox");

            using (FileStream fs = File.OpenWrite("CreateFormField.pdf"))
            {
                PdfFile pdfFile = new PdfFile();
                pdfFile.Export(document, fs);
            }
        }