Exemple #1
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);
            }
        }
Exemple #2
0
        public static void AddTextWatermark()
        {
            PdfFile     pdfFile  = new PdfFile();
            PdfDocument document = pdfFile.Import(File.ReadAllBytes("sample.pdf"));

            //Get first page of pdf
            PdfPage            page    = document.Pages[0];
            PageContentBuilder builder = new PageContentBuilder(page);

            //Create a block with watermark text
            Block block = new Block();

            //Set text color and font size
            block.GraphicState.FillColor = new RgbColor(100, 255, 0, 0);
            block.TextState.FontSize     = 50;
            //Set a text format.
            block.HorizontalAlignment = Editing.Flow.HorizontalAlignment.Center;
            block.VerticalAlignment   = Editing.Flow.VerticalAlignment.Center;
            block.InsertText("watermark");
            Size needSize = block.Measure();

            //Set watermark text position and rotation
            builder.Position.Translate((page.Size.Width - needSize.Width) / 2, (page.Size.Height - needSize.Height) / 2);
            builder.Position.Rotate(-45);
            builder.DrawBlock(block);

            File.WriteAllBytes("TextWatermark.pdf", pdfFile.Export(document));
        }
Exemple #3
0
        public static void AddShape()
        {
            //This example is using page level builder
            PdfDocument document = new PdfDocument();
            PdfPage     page     = document.Pages.AddPage();

            PageContentBuilder builder = new PageContentBuilder(page);

            //Add line shape
            builder.GraphicState.StrokeColor = new RgbColor(255, 0, 0);
            builder.DrawLine(new Point(10, 10), new Point(200, 20));

            //Add rectangle shape by Block object
            Block block = new Block();

            block.GraphicState.StrokeColor = new RgbColor(255, 0, 0);
            block.GraphicState.FillColor   = new RgbColor(0, 255, 0);
            block.GraphicState.IsFilled    = true;
            block.InsertRectangle(new Rect(0, 0, 50, 50));
            builder.Position.Translate(10, 20);
            builder.DrawBlock(block);

            using (FileStream fs = File.Create("AddShape.pdf"))
            {
                PdfFile pdfFile = new PdfFile();
                pdfFile.Export(document, fs);
            }
        }
Exemple #4
0
        public static void AddImageAsPage()
        {
            PdfDocument document = new PdfDocument();

            using (Stream imgStream = File.OpenRead("sample.jpg"))
            {
                iDiTect.Pdf.Resources.ImageSource image = new iDiTect.Pdf.Resources.ImageSource(imgStream);

                //Create a new page with image's size
                PdfPage page = new PdfPage();
                page.Size = new Size(image.Width, image.Height);
                PageContentBuilder builder = new PageContentBuilder(page);

                //draw image to page at position (0,0)
                builder.DrawImage(image);

                document.Pages.Add(page);
            }

            using (FileStream fs = File.OpenWrite("ConvertImageToPdf.pdf"))
            {
                PdfFile pdfFile = new PdfFile();
                pdfFile.Export(document, fs);
            }
        }
Exemple #5
0
        public static void AddImageWatermark()
        {
            PdfFile     pdfFile = new PdfFile();
            PdfDocument document;

            using (FileStream fs = File.OpenRead("sample.pdf"))
            {
                //Read pdf document from stream
                document = pdfFile.Import(fs);
            }
            //Get first page of pdf
            PdfPage            page    = document.Pages[0];
            PageContentBuilder builder = new PageContentBuilder(page);

            //Set watermark image position
            builder.Position.Translate(100, 100);
            using (Stream stream = File.OpenRead("watermark.png"))
            {
                //Insert watermark image as original size
                builder.DrawImage(stream);
                //Insert watermark image in customized size
                //builder.DrawImage(stream, new Size(80, 80));
            }

            using (FileStream fs = File.OpenWrite("ImageWatermark.pdf"))
            {
                pdfFile.Export(document, fs);
            }
        }
Exemple #6
0
        public static void AddLinkToWebLink()
        {
            PdfFile     pdfFile  = new PdfFile();
            PdfDocument document = pdfFile.Import(File.ReadAllBytes("sample.pdf"));

            PdfPage            page    = document.Pages[0];
            PageContentBuilder builder = new PageContentBuilder(page);

            builder.GraphicState.FillColor = new RgbColor(100, 255, 0, 0);

            //Add external hyperlink with Action
            UriAction uriAction = new UriAction();

            uriAction.Uri = new Uri("http://www.iditect.com");
            Rect rectArea = new Rect(50, 150, 100, 100);
            var  uriLink  = page.Annotations.AddLink(uriAction);

            uriLink.Rect = rectArea;
            builder.DrawRectangle(rectArea);

            //Add external hyperlink with Block
            Block block = new Block();

            block.InsertText("This is a ");
            block.GraphicState.FillColor = new RgbColor(0, 0, 255);
            block.InsertHyperlinkStart(new Uri("http://www.iditect.com"));
            block.InsertText("hyperlink");
            block.InsertHyperlinkEnd();
            builder.Position.Translate(50, 300);
            builder.DrawBlock(block);

            File.WriteAllBytes("AddLink2.pdf", pdfFile.Export(document));
        }
Exemple #7
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);
            }
        }
Exemple #8
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);
            }
        }
Exemple #9
0
        public static void AddTable(DataTable dt)
        {
            PdfDocument        document = new PdfDocument();
            PdfPage            page     = document.Pages.AddPage();
            PageContentBuilder builder  = new PageContentBuilder(page);

            builder.Position.Translate(50, 50);
            Table table = CreateSimpleTable(dt);

            table.LayoutType = TableLayoutType.FixedWidth;
            builder.DrawTable(table);

            PdfFile pdfFile = new PdfFile();

            File.WriteAllBytes("Reporte_Alumnos_" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".pdf", pdfFile.Export(document));
        }
Exemple #10
0
        public static void AddTable()
        {
            PdfDocument        document = new PdfDocument();
            PdfPage            page     = document.Pages.AddPage();
            PageContentBuilder builder  = new PageContentBuilder(page);

            builder.Position.Translate(50, 50);
            Table table = CreateSimpleTable();

            table.LayoutType = TableLayoutType.FixedWidth;
            builder.DrawTable(table, 500);

            PdfFile pdfFile = new PdfFile();

            File.WriteAllBytes("AddTable.pdf", pdfFile.Export(document));
        }
Exemple #11
0
        public static void AddImage()
        {
            PdfFile     pdfFile = new PdfFile();
            PdfDocument document;

            using (FileStream fs = File.OpenRead("sample.pdf"))
            {
                //Read pdf document from stream
                document = pdfFile.Import(fs);
            }
            //Get first page of pdf
            PdfPage page = document.Pages[0];
            //Create page level builder
            PageContentBuilder builder = new PageContentBuilder(page);

            //Add image using builder's DrawImage directly
            using (Stream imgStream = File.OpenRead("sample.jpg"))
            {
                //Set image position
                builder.Position.Translate(100, 100);

                //insert image as original size
                builder.DrawImage(imgStream);
                //insert image with customized size
                //builder.DrawImage(imgStream, new Size(80, 80));
            }

            //Add image using Block object
            using (Stream imgStream = File.OpenRead("sample2.jpg"))
            {
                //Set image position
                builder.Position.Translate(100, 400);

                Block block = new Block();
                //insert image as original size
                //block.InsertImage(imgStream);
                //insert image with customized size
                block.InsertImage(imgStream, new Size(100, 100));

                builder.DrawBlock(block);
            }

            using (FileStream fs = File.OpenWrite("InsertImage.pdf"))
            {
                pdfFile.Export(document, fs);
            }
        }
Exemple #12
0
        public static void AddImageSignature2PDF()
        {
            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(50, 50));
            widget.RecalculateContent();

            //Customize signature appearance, insert an image as signature displaying
            PageContentBuilder signatureAppearance = new PageContentBuilder(widget.Content.NormalContentSource);

            using (Stream imgStream = File.OpenRead("sample.jpg"))
            {
                signatureAppearance.DrawImage(imgStream);
            }

            //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);
            }
        }
Exemple #13
0
        public static void AddLinkInsideDocument()
        {
            PdfFile     pdfFile  = new PdfFile();
            PdfDocument document = pdfFile.Import(File.ReadAllBytes("sample.pdf"));

            PdfPage            page    = document.Pages[0];
            PageContentBuilder builder = new PageContentBuilder(page);

            builder.GraphicState.FillColor = new RgbColor(100, 255, 0, 0);

            //Add internal link with Destination
            PageFit pageFit = document.Destinations.AddPageFit();

            //Navigate to the second page
            pageFit.Page = document.Pages[1];
            Rect rectArea            = new Rect(50, 100, 100, 100);
            Link linkWithDestination = new Link(pageFit);

            linkWithDestination.Rect = rectArea;
            page.Annotations.Add(linkWithDestination);
            builder.DrawRectangle(rectArea);

            //Add internal link with Location
            Location location = new Location();

            //Navigate to second page
            location.Page = document.Pages[1];
            //Navigate to the target point in page
            location.Left = 275;
            location.Top  = 400;
            //Set Zoom value in viewer, the value "3" means showing in 300% of original size
            location.Zoom = 3;
            rectArea      = new Rect(50, 250, 100, 100);
            Link linkWithLocation = page.Annotations.AddLink(location);

            linkWithLocation.Rect = rectArea;
            builder.DrawRectangle(rectArea);

            //Add internal link with Action
            GoToAction goToAction = new GoToAction();

            //Set navigation to either Destination or Location
            goToAction.Destination = location;
            //goToAction.Destination = pageFit;
            rectArea = new Rect(50, 400, 100, 100);
            Link goToLink = page.Annotations.AddLink(goToAction);

            goToLink.Rect = rectArea;
            builder.DrawRectangle(rectArea);

            //Add internal link with Block
            Block block = new Block();

            block.InsertText("This is a ");
            block.GraphicState.FillColor = new RgbColor(0, 0, 255);
            //Set navigation to either Destination or Location
            block.InsertHyperlinkStart(pageFit);
            //block.InsertHyperlinkStart(location);
            block.InsertText("navigation link");
            block.InsertHyperlinkEnd();
            builder.Position.Translate(50, 550);
            builder.DrawBlock(block);

            File.WriteAllBytes("AddLink.pdf", pdfFile.Export(document));
        }
        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);
            }
        }