Exemple #1
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 #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 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 #5
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 #6
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));
        }