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);

            PointF center = new PointF(200, 200);

            // Create clipping path from circle
            Path path = new Path();

            path.AddCircle(center, 100);
            page.Canvas.SetClip(path);

            // Paint rectangle over the clipping circle.
            // Only part of the rectangle intersecting the clipping circle will be drawn.
            SolidBrush brush = new SolidBrush(new ColorRGB(255, 0, 0));

            page.Canvas.DrawRectangle(brush, center.X - 50, center.Y - 50, 200, 200);

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

            // Cleanup
            pdfDocument.Dispose();

            // Open document in default PDF viewer app
            Process.Start("result.pdf");
        }
        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);
        }
Exemple #4
0
        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 two pages
            Page page1 = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page1);
            Page page2 = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page2);

            Font  font  = new Font(StandardFonts.Times, 12);
            Brush brush = new SolidBrush();

            // Create link annotation pointing to Page 2, 200 Points from the top.
            Destination    destination = new Destination(page2, 200);
            LinkAnnotation link        = new LinkAnnotation(destination, 20, 20, 100, 25);

            link.Color = new ColorRGB(255, 0, 0); // you can change the link color
            page1.Annotations.Add(link);

            // Draw link text (optional)
            page1.Canvas.DrawString("Link to Page 2", font, brush, new RectangleF(20, 20, 100, 25),
                                    new StringFormat {
                HorizontalAlign = HorizontalAlign.Center, VerticalAlign = VerticalAlign.Center
            });

            // Draw a text at the link target
            page2.Canvas.DrawString("Link target", font, brush, 20, 200);

            // 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);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            const string inputFile     = @"sample.pdf";
            const int    pageIndex     = 0;
            const string searchPattern = "\\d+\\.\\d+";

            // Prepare TextExtractor
            using (TextExtractor textExtractor = new TextExtractor("demo", "demo"))
            {
                textExtractor.RegexSearch = true;
                textExtractor.LoadDocumentFromFile(inputFile);

                // Load document with PDF SDK
                using (Document pdfDocument = new Document(inputFile))
                {
                    pdfDocument.RegistrationName = "demo";
                    pdfDocument.RegistrationKey  = "demo";

                    Page   pdfDocumentPage = pdfDocument.Pages[pageIndex];
                    Canvas canvas          = pdfDocumentPage.Canvas;

                    SolidBrush fillBrush = new SolidBrush(new ColorRGB(255, 0, 0));
                    fillBrush.Opacity = 50;                     // make the brush transparent

                    // Search for pattern and highlight found pieces
                    if (textExtractor.Find(pageIndex, searchPattern, caseSensitive: false))
                    {
                        do
                        {
                            foreach (var foundPiece in textExtractor.FoundText.Elements)
                            {
                                // Inflate the rectangle a bit
                                RectangleF rect = RectangleF.Inflate(foundPiece.Bounds, 1, 2);
                                // Draw rectangle over the PDF page
                                canvas.DrawRectangle(fillBrush, rect);
                            }
                        } while (textExtractor.FindNext());
                    }

                    // Save as new PDF document
                    pdfDocument.Save("result.pdf");

                    // Open result document in default associated application (for demo purposes)
                    Process.Start("result.pdf");
                }
            }
        }
Exemple #6
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");
        }
        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);

            Canvas canvas = page.Canvas;

            // Prepare pens and brushes
            Pen   borderPen = new SolidPen(new ColorGray(128), 2f);
            Brush brush1    = new SolidBrush(new ColorRGB(255, 0, 0));
            Brush brush2    = new SolidBrush(new ColorRGB(0, 255, 255));

            // Draw transparent rectangle with border only
            canvas.DrawRectangle(borderPen, 100, 100, 100, 50);

            // Draw rounded rectangle with broder and filling
            canvas.DrawRoundedRectangle(borderPen, brush1, 250, 100, 100, 50, 10);

            // Draw rectangle as polygon
            canvas.DrawPolygon(borderPen, brush2, new PointF[] { new PointF(400, 100), new PointF(500, 100), new PointF(500, 150), new PointF(400, 150) });

            // 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";
            // Add two pages
            Page page1 = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page1);
            Page page2 = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page2);

            Font  font  = new Font(StandardFonts.Times, 12);
            Brush brush = new SolidBrush();

            // Create link annotation pointing to Page 2, 200 Points from the top.
            Destination    destination = new Destination(page2, 200);
            LinkAnnotation link        = new LinkAnnotation(destination, 20, 20, 100, 25);

            link.Color = new ColorRGB(255, 0, 0);             // you can change the link color
            page1.Annotations.Add(link);

            // Draw link text (optional)
            page1.Canvas.DrawString("Link to Page 2", font, brush, new RectangleF(20, 20, 100, 25),
                                    new StringFormat {
                HorizontalAlign = HorizontalAlign.Center, VerticalAlign = VerticalAlign.Center
            });

            // Draw a text at the link target
            page2.Canvas.DrawString("Link target", font, brush, 20, 200);

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

            // Cleanup
            pdfDocument.Dispose();

            // Open document in default PDF viewer app
            Process.Start("result.pdf");
        }
Exemple #10
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);
            }
        }
        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);

            PointF center = new PointF(200, 200);

            // Create clipping path from circle
            Path path = new Path();

            path.AddCircle(center, 100);
            page.Canvas.SetClip(path);

            // Paint rectangle over the clipping circle.
            // Only part of the rectangle intersecting the clipping circle will be drawn.
            SolidBrush brush = new SolidBrush(new ColorRGB(255, 0, 0));

            page.Canvas.DrawRectangle(brush, center.X - 50, center.Y - 50, 200, 200);

            // 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";
            // Add page
            Page page = new Page(PaperFormat.A4);

            pdfDocument.Pages.Add(page);

            Canvas canvas = page.Canvas;

            // Prepare pens and brushes
            Pen   borderPen = new SolidPen(new ColorGray(128), 2f);
            Brush brush1    = new SolidBrush(new ColorRGB(255, 0, 0));
            Brush brush2    = new SolidBrush(new ColorRGB(0, 255, 255));

            // Draw transparent rectangle with border only
            canvas.DrawRectangle(borderPen, 100, 100, 100, 50);

            // Draw rounded rectangle with broder and filling
            canvas.DrawRoundedRectangle(borderPen, brush1, 250, 100, 100, 50, 10);

            // Draw rectangle as polygon
            canvas.DrawPolygon(borderPen, brush2, new PointF[] { new PointF(400, 100), new PointF(500, 100), new PointF(500, 150), new PointF(400, 150) });

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

            // Cleanup
            pdfDocument.Dispose();

            // Open document in default PDF viewer app
            Process.Start("result.pdf");
        }
        static void Main(string[] args)
        {
            try
            {
                // Parse MessageContents using MsgReader Library
                // MsgReader library can be obtained from: https://github.com/Sicos1977/MSGReader
                using (var msg = new MsgReader.Outlook.Storage.Message("EmailWithAttachments.msg"))
                {
                    // Get Sender information
                    var from = msg.GetEmailSender(false, false);

                    // Message sent datetime
                    var sentOn = msg.SentOn;

                    // Recipient To information
                    var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false);

                    // Recipient CC information
                    var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false);

                    // Recipient BCC information
                    var recipientBcc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Bcc, false, false);

                    // Message subject
                    var subject = msg.Subject;

                    // Get Message Body
                    var msgBody = msg.BodyHtml;

                    // Prepare PDF docuemnt
                    using (Document outputDocument = new Document())
                    {
                        // Add registration keys
                        outputDocument.RegistrationName = "demo";
                        outputDocument.RegistrationKey  = "demo";

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

                        // Add sample content
                        Font  font  = new Font(StandardFonts.Times, 12);
                        Brush brush = new SolidBrush();

                        // Add Email contents
                        int topMargin = 20;
                        page.Canvas.DrawString($"File Name: {msg.FileName}", font, brush, 20, (topMargin += 20));
                        page.Canvas.DrawString($"From: {from}", font, brush, 20, (topMargin += 20));
                        page.Canvas.DrawString($"Sent On: {(sentOn.HasValue ? sentOn.Value.ToString("MM/dd/yyyy HH:mm") : "")}", font, brush, 20, (topMargin += 20));
                        page.Canvas.DrawString($"To: {recipientsTo}", font, brush, 20, (topMargin += 20));

                        if (!string.IsNullOrEmpty(recipientsCc))
                        {
                            page.Canvas.DrawString($"CC: {recipientsCc}", font, brush, 20, (topMargin += 20));
                        }

                        if (!string.IsNullOrEmpty(recipientBcc))
                        {
                            page.Canvas.DrawString($"BCC: {recipientBcc}", font, brush, 20, (topMargin += 20));
                        }

                        page.Canvas.DrawString($"Subject: {subject}", font, brush, 20, (topMargin += 20));
                        page.Canvas.DrawString("Message body in next page.", font, brush, 20, (topMargin += 20));

                        // Convert Html body to PDF in order to retain all formatting.
                        using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
                        {
                            converter.PageSize    = PaperKind.A4;
                            converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait;

                            // Get all inline attachment, and replace them
                            foreach (MsgReader.Outlook.Storage.Attachment itmAttachment in msg.Attachments)
                            {
                                if (itmAttachment.IsInline)
                                {
                                    var oData      = itmAttachment.Data;
                                    var dataBase64 = Convert.ToBase64String(oData);

                                    // Replace within email
                                    msgBody = msgBody.Replace($"src=\"{itmAttachment.FileName}\"", $"src=\"{ "data:image/jpeg;base64," + dataBase64}\"");
                                }
                            }

                            // Convert input HTML to stream
                            byte[]       byteArrayBody = Encoding.UTF8.GetBytes(msgBody);
                            MemoryStream inputStream   = new MemoryStream(byteArrayBody);

                            // Create output stream to store generated PDF file
                            using (var outputStream = new MemoryStream())
                            {
                                // Convert HTML to PDF
                                converter.ConvertHtmlToPdf(inputStream, outputStream);

                                // Create new document from generated output stream
                                Document docContent = new Document(outputStream);

                                // Append all pages to main PDF
                                foreach (Page item in docContent.Pages)
                                {
                                    outputDocument.Pages.Add(item);
                                }

                                // Apped all other attachments
                                foreach (MsgReader.Outlook.Storage.Attachment itmAttachment in msg.Attachments)
                                {
                                    if (!itmAttachment.IsInline)
                                    {
                                        // Attachment is image, so adding accordingly
                                        var    pageAttachment = new Page(PaperFormat.A4);
                                        Canvas canvas         = pageAttachment.Canvas;

                                        var   oAttachmentStream = new MemoryStream(itmAttachment.Data);
                                        Image imageAttachment   = new Image(oAttachmentStream);

                                        canvas.DrawImage(imageAttachment, 20, 20);

                                        // Add attachment
                                        outputDocument.Pages.Add(pageAttachment);
                                    }
                                }

                                // Save output file
                                outputDocument.Save("result.pdf");
                            }
                        }

                        // Open result document in default associated application (for demo purpose)
                        ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf");
                        processStartInfo.UseShellExecute = true;
                        Process.Start(processStartInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Press enter key to exit...");
                Console.ReadLine();
            }
        }
        static void Main(string[] args)
        {
            try
            {
                // Parse MessageContents using MsgReader Library
                // MsgReader library can be obtained from: https://github.com/Sicos1977/MSGReader
                using (var msg = new MsgReader.Outlook.Storage.Message("HtmlSampleEmail.msg"))
                {
                    // Get Sender information
                    var from = msg.GetEmailSender(false, false);

                    // Message sent datetime
                    var sentOn = msg.SentOn;

                    // Recipient To information
                    var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false);

                    // Recipient CC information
                    var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false);

                    // Message subject
                    var subject = msg.Subject;

                    // Get Message Body
                    var msgBody = msg.BodyHtml;

                    // Prepare PDF docuemnt
                    using (Document outputDocument = new Document())
                    {
                        // Add registration keys
                        outputDocument.RegistrationName = "demo";
                        outputDocument.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);
                        outputDocument.Pages.Add(page);

                        // Add sample content
                        Font  font  = new Font(StandardFonts.Times, 12);
                        Brush brush = new SolidBrush();

                        // Add Email contents
                        int topMargin = 20;
                        page.Canvas.DrawString($"File Name: {msg.FileName}", font, brush, 20, (topMargin += 20));
                        page.Canvas.DrawString($"From: {from}", font, brush, 20, (topMargin += 20));
                        page.Canvas.DrawString($"Sent On: {(sentOn.HasValue ? sentOn.Value.ToString("MM/dd/yyyy HH:mm") : "")}", font, brush, 20, (topMargin += 20));
                        page.Canvas.DrawString($"To: {recipientsTo}", font, brush, 20, (topMargin += 20));

                        if (!string.IsNullOrEmpty(recipientsCc))
                        {
                            page.Canvas.DrawString($"CC: {recipientsCc}", font, brush, 20, (topMargin += 20));
                        }

                        page.Canvas.DrawString($"Subject: {subject}", font, brush, 20, (topMargin += 20));
                        page.Canvas.DrawString("Message body in next page.", font, brush, 20, (topMargin += 20));

                        // Convert Html body to PDF in order to retain all formatting.
                        using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
                        {
                            converter.PageSize    = PaperKind.A4;
                            converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait;

                            // Convert input HTML to stream
                            byte[]       byteArrayBody = Encoding.UTF8.GetBytes(msgBody);
                            MemoryStream inputStream   = new MemoryStream(byteArrayBody);

                            // Create output stream to store generated PDF file
                            using (var outputStream = new MemoryStream())
                            {
                                // Convert HTML to PDF
                                converter.ConvertHtmlToPdf(inputStream, outputStream);

                                // Create new document from generated output stream
                                Document docContent = new Document(outputStream);

                                // Append all pages to main PDF
                                foreach (Page item in docContent.Pages)
                                {
                                    outputDocument.Pages.Add(item);
                                }

                                // Save output file
                                outputDocument.Save("result.pdf");
                            }
                        }

                        // Open result document in default associated application (for demo purpose)
                        ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf");
                        processStartInfo.UseShellExecute = true;
                        Process.Start(processStartInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Press enter key to exit...");
                Console.ReadLine();
            }
        }
Exemple #15
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);
        }