static void Main()
        {
            // Create new document
            Document pdfDocument = new Document();

            pdfDocument.RegistrationName = "demo";
            pdfDocument.RegistrationKey  = "demo";

            // If you wish to load an existing document uncomment the line below and comment the Add page section instead
            // pdfDocument.Load(@".\existing_document.pdf");

            // Add page
            Page page = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page);

            // Add sample page content
            SolidBrush   brush        = new SolidBrush();
            Font         font         = new Font("Arial", 16);
            RectangleF   rect         = new RectangleF(0, 50, page.Width, 100);
            StringFormat stringFormat = new StringFormat();

            stringFormat.HorizontalAlign = HorizontalAlign.Center;
            page.Canvas.DrawString("Signature Test", font, brush, rect, stringFormat);

            // Signing parameters
            string certficateFile     = ".\\demo_certificate.pfx";
            string certficatePassword = "******";
            // Optional parameters
            string signingReason = "Approval";
            string contactName   = "John Smith";
            string location      = "Corporate HQ";

            // Invisible signature
            //pdfDocument.Sign(certficateFile, certficatePassword);

            // Visible signature
            RectangleF signatureRect = new RectangleF(400, 50, 150, 100);

            pdfDocument.Sign(certficateFile, certficatePassword, signatureRect, signingReason, contactName, location);

            // Save document to file
            pdfDocument.Save("result.pdf");

            // Cleanup
            pdfDocument.Dispose();

            // Open result document in default associated application (for demo purpose)
            ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf");

            processStartInfo.UseShellExecute = true;
            Process.Start(processStartInfo);
        }
        static void Main()
        {
            // Create new document
            Document pdfDocument = new Document();

            pdfDocument.RegistrationName = "demo";
            pdfDocument.RegistrationKey  = "demo";

            // If you wish to load an existing document uncomment the line below and comment the Add page section instead
            // pdfDocument.Load(@".\existing_document.pdf");

            // Add page
            Page page = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page);

            // Draw simple text
            Font  font       = new Font("Arial", 24);
            Brush blackBrush = new SolidBrush();

            page.Canvas.DrawString("Simple text.", font, blackBrush, 20, 20);

            // Draw text with alignment in specified rectangle
            StringFormat stringFormat = new StringFormat();

            stringFormat.HorizontalAlign = HorizontalAlign.Right;
            stringFormat.VerticalAlign   = VerticalAlign.Bottom;
            page.Canvas.DrawString("Aligned text", font, blackBrush, new RectangleF(20, 100, 200, 60), stringFormat);
            page.Canvas.DrawRectangle(new SolidPen(), 20, 100, 200, 60);

            // Draw colored text
            Font  boldFont = new Font("Arial", 32, true, false, false, false);
            Brush redBrush = new SolidBrush(new ColorRGB(255, 0, 0));
            Pen   bluePen  = new SolidPen(new ColorRGB(0, 0, 255));

            page.Canvas.DrawString("Colored text", boldFont, redBrush, 20, 200);
            page.Canvas.DrawString("Outlined colored text", boldFont, redBrush, bluePen, 20, 240);
            page.Canvas.DrawString("Outlined transparent text", boldFont, bluePen, 20, 280);

            // Save document to file
            pdfDocument.Save("result.pdf");

            // Cleanup
            pdfDocument.Dispose();

            // Open result document in default associated application (for demo purpose)
            ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf");

            processStartInfo.UseShellExecute = true;
            Process.Start(processStartInfo);
        }
Beispiel #3
0
        static void Main()
        {
            // Create new document
            Document pdfDocument = new Document();

            pdfDocument.RegistrationName = "demo";
            pdfDocument.RegistrationKey  = "demo";
            // Add page
            Page page = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page);

            // Add sample page content
            SolidBrush   brush        = new SolidBrush();
            Font         font         = new Font("Arial", 16);
            RectangleF   rect         = new RectangleF(0, 50, page.Width, 100);
            StringFormat stringFormat = new StringFormat();

            stringFormat.HorizontalAlign = HorizontalAlign.Center;
            page.Canvas.DrawString("Signature Test", font, brush, rect, stringFormat);

            // Signing parameters
            string certficateFile     = ".\\demo_certificate.pfx";
            string certficatePassword = "******";
            // Optional parameters
            string signingReason = "Approval";
            string contactName   = "John Smith";
            string location      = "Corporate HQ";

            // Invisible signature
            //pdfDocument.Sign(certficateFile, certficatePassword);

            // Visible signature
            RectangleF signatureRect = new RectangleF(400, 50, 150, 100);

            pdfDocument.Sign(certficateFile, certficatePassword, signatureRect, signingReason, contactName, location);

            // Save document to file
            pdfDocument.Save("result.pdf");

            // Cleanup
            pdfDocument.Dispose();

            // Open document in default PDF viewer application
            Process.Start("result.pdf");
        }
        static void Main()
        {
            // Create new document
            Document pdfDocument = new Document();

            pdfDocument.RegistrationName = "demo";
            pdfDocument.RegistrationKey  = "demo";
            // Add page
            Page page = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page);

            // Draw simple text
            Font  font       = new Font("Arial", 24);
            Brush blackBrush = new SolidBrush();

            page.Canvas.DrawString("Simple text.", font, blackBrush, 20, 20);

            // Draw text with alignment in specified rectangle
            StringFormat stringFormat = new StringFormat();

            stringFormat.HorizontalAlign = HorizontalAlign.Right;
            stringFormat.VerticalAlign   = VerticalAlign.Bottom;
            page.Canvas.DrawString("Aligned text", font, blackBrush, new RectangleF(20, 100, 200, 60), stringFormat);
            page.Canvas.DrawRectangle(new SolidPen(), 20, 100, 200, 60);

            // Draw colored text
            Font  boldFont = new Font("Arial", 32, true, false, false, false);
            Brush redBrush = new SolidBrush(new ColorRGB(255, 0, 0));
            Pen   bluePen  = new SolidPen(new ColorRGB(0, 0, 255));

            page.Canvas.DrawString("Colored text", boldFont, redBrush, 20, 200);
            page.Canvas.DrawString("Outlined colored text", boldFont, redBrush, bluePen, 20, 240);
            page.Canvas.DrawString("Outlined transparent text", boldFont, bluePen, 20, 280);

            // Save document to file
            pdfDocument.Save("result.pdf");

            // Cleanup
            pdfDocument.Dispose();

            // Open document in default PDF viewer app
            Process.Start("result.pdf");
        }
Beispiel #5
0
        static void DrawTransparentText(Page documentPage, List <TextChunk> textChunks, string fontName = "Arial", float maxFontSize = 4f)
        {
            var stringFormat     = new StringFormat();
            var transparentBrush = new SolidBrush {
                Opacity = 0
            };

            foreach (var textChunk in textChunks)
            {
                Font font = new Font(fontName, Math.Max(textChunk.Rect.Height, maxFontSize));

                // Fit drawn text into chunk rectangle
                float renderedWidth = font.GetTextWidth(textChunk.Text);
                stringFormat.Scaling = textChunk.Rect.Width / renderedWidth * 100;

                documentPage.Canvas.DrawString(textChunk.Text, font, transparentBrush,
                                               textChunk.Rect.Left, textChunk.Rect.Top, stringFormat);
            }
        }
Beispiel #6
0
        static void Main()
        {
            // Load document
            Document pdfDocument = new Document(@".\RotatedPages.pdf");

            pdfDocument.RegistrationName = "demo";
            pdfDocument.RegistrationKey  = "demo";

            // If you wish to load an existing document uncomment the line below and comment the Add page section instead
            // pdfDocument.Load(@".\existing_document.pdf");

            string headerText = "Sample Header";
            string footerText = "Sample Footer";

            // Prepare StringFormat with Center text alignment
            StringFormat stringFormat = new StringFormat {
                HorizontalAlign = HorizontalAlign.Center
            };
            // Prepare drawing tools
            Font  font  = new Font(StandardFonts.Helvetica, 9);
            Brush brush = new SolidBrush(new ColorRGB(255, 0, 0));

            for (int i = 0; i < pdfDocument.Pages.Count; ++i)
            {
                Page page = pdfDocument.Pages[i];

                page.Canvas.SaveGraphicsState();

                // Calculate the coordinates of the text taking into account the page rotation:

                float headerY = 10;
                float footerY;
                float textRectWidth;
                float textRectHeight = font.GetTextHeight() + 5;

                if (page.RotationAngle == RotationAngle.Rotate0 || page.RotationAngle == RotationAngle.Rotate180)
                {
                    footerY       = page.Height - textRectHeight - 10;
                    textRectWidth = page.Width;
                }
                else
                {
                    footerY       = page.Width - textRectHeight - 10;
                    textRectWidth = page.Height;
                }

                // Rotate page canvas according to page rotation
                switch (page.RotationAngle)
                {
                case RotationAngle.Rotate90:
                    page.Canvas.RotateTransform(-90);
                    page.Canvas.TranslateTransform(-page.Height, 0);
                    break;

                case RotationAngle.Rotate180:
                    page.Canvas.RotateTransform(180);
                    page.Canvas.TranslateTransform(-page.Width, -page.Height);
                    break;

                case RotationAngle.Rotate270:
                    page.Canvas.RotateTransform(-270);
                    page.Canvas.TranslateTransform(0, -page.Width);
                    break;
                }

                // Draw header and footer
                page.Canvas.DrawString(headerText, font, brush, new RectangleF(0, headerY, textRectWidth, textRectHeight), stringFormat);
                page.Canvas.DrawString(footerText, font, brush, new RectangleF(0, footerY, textRectWidth, textRectHeight), stringFormat);

                page.Canvas.RestoreGraphicsState();
            }

            // Save document to file
            pdfDocument.Save("result.pdf");

            // Cleanup
            pdfDocument.Dispose();

            // Open result PDF document in default associated application (for demo purpose)
            ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf");

            processStartInfo.UseShellExecute = true;
            Process.Start(processStartInfo);
        }