예제 #1
0
        static void Main()
        {
            // Create converter instance
            using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
            {
                // Perform conversion
                converter.ConvertHtmlToPdf("sample.html", "result.pdf");
            }

            // Open result document in default PDF viewer
            Process.Start("result.pdf");
        }
예제 #2
0
        static void Main()
        {
            using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
            {
                converter.PageSize    = PaperKind.Letter;
                converter.Orientation = PaperOrientation.Portrait;

                // Convert input HTML to output pdf
                converter.ConvertHtmlToPdf("invoice.html", "result.pdf");
            }

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

            processStartInfo.UseShellExecute = true;
            Process.Start(processStartInfo);
        }
예제 #3
0
        static void Main(string[] args)
        {
            using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
            {
                converter.PageSize    = PaperKind.A4;
                converter.Orientation = PaperOrientation.Portrait;
                converter.Footer      = "<p style=\"color: blue;\">FOOTER TEXT</p>";

                converter.ConvertHtmlToPdf("sample.html", "result.pdf");

                // You can also pass a link instead of the input file:
                //converter.ConvertHtmlToPdf("http://google.com", "result.pdf");
            }

            // Open result file in default PDF viewer
            Process.Start("result.pdf");
        }
예제 #4
0
        static void Main(string[] args)
        {
            // HtmlToPdfConverter can convert HTML files with SVG content, or SVG files directly.
            using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
            {
                converter.PageSize = PaperKind.A4;

                converter.ConvertHtmlToPdf("drawing.svg", "result.pdf");

                // You can also pass a link instead of the input file:
                //converter.ConvertHtmlToPdf("http://somesite.com/files/drawing.svg", "result.pdf");
            }

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

            processStartInfo.UseShellExecute = true;
            Process.Start(processStartInfo);
        }
예제 #5
0
        static void Main(string[] args)
        {
            using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
            {
                converter.PageSize    = PaperKind.A4;
                converter.Orientation = PaperOrientation.Portrait;
                converter.Footer      = "FOOTER TEXT"; // optional footer text

                converter.ConvertHtmlToPdf("sample.html", "result.pdf");

                // You can also pass a link instead of the input file:
                //converter.ConvertHtmlToPdf("http://google.com", "result.pdf");
            }

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

            processStartInfo.UseShellExecute = true;
            Process.Start(processStartInfo);
        }
        /// <summary>
        /// Demonstrate Html to PDF Conversation
        /// </summary>
        /// <returns></returns>
        private FileResult _DemonstrateHtml2PDFConversation()
        {
            // HTML to PDF Conversion
            using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
            {
                converter.PageSize    = PaperKind.A4;
                converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait;
                converter.Footer      = "<p style=\"color: blue;\">FOOTER TEXT</p>";

                // Get html document in input stream
                FileStream inputFileStream = new FileStream(Server.MapPath("~/SampleFiles/sample.html"), FileMode.Open);                    // Define output stream

                MemoryStream outputStream = new MemoryStream();
                // Get converted PDF docuement in output stream
                converter.ConvertHtmlToPdf(inputFileStream, outputStream);

                // Stream back to beginning to allow the data to be read back out
                outputStream.Seek(0, SeekOrigin.Begin);

                // Return result
                return(File(outputStream, "text/pdf", "sample_ConvertedFromHTML.pdf"));
            }
        }
예제 #7
0
        /*
         * IF YOU SEE TEMPORARY FOLDER ACCESS ERRORS:
         *
         * Temporary folder access is required for web application when you use ByteScout SDK in it.
         * If you are getting errors related to the access to temporary folder like "Access to the path 'C:\Windows\TEMP\... is denied" then you need to add permission for this temporary folder to make ByteScout SDK working on that machine and IIS configuration because ByteScout SDK requires access to temp folder to cache some of its data for more efficient work.
         *
         * SOLUTION:
         *
         * If your IIS Application Pool has "Load User Profile" option enabled the IIS provides access to user's temp folder. Check user's temporary folder
         *
         * If you are running Web Application under an impersonated account or IIS_IUSRS group, IIS may redirect all requests into separate temp folder like "c:\temp\".
         *
         * In this case
         * - check the User or User Group your web application is running under
         * - then add permissions for this User or User Group to read and write into that temp folder (c:\temp or c:\windows\temp\ folder)
         * - restart your web application and try again
         *
         */

        #region Events

        /// <summary>
        /// Handle HTML to PDF conversation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPDFConversionFromHtml_Click(object sender, EventArgs e)
        {
            try
            {
                // HTML to PDF Conversion
                using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
                {
                    converter.PageSize    = PaperKind.A4;
                    converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait;
                    converter.Footer      = "<p style=\"color: blue;\">FOOTER TEXT</p>";

                    // Get html document in input stream
                    FileStream inputFileStream = new FileStream(Server.MapPath("~/SampleFiles/sample.html"), FileMode.Open);

                    // Define output stream
                    MemoryStream outputStream = new MemoryStream();

                    // Get converted PDF docuement in output stream
                    converter.ConvertHtmlToPdf(inputFileStream, outputStream);

                    // Download converted document
                    Response.Clear();
                    Response.ClearHeaders();

                    Response.AppendHeader("Content-Length", outputStream.Length.ToString());
                    Response.ContentType = "text/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=\"sample_ConvertedFromHTML.pdf\"");

                    Response.BinaryWrite(outputStream.ToArray());
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                lblPDFConversationFromHTML.Text = "Error: " + ex.Message;
            }
        }
        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();
            }
        }
예제 #9
0
        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();
            }
        }
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Please wait while PDF is being created...");

                // Parse MessageContents using MsgReader Library
                // MsgReader library can be obtained from: https://github.com/Sicos1977/MSGReader
                using (var msg = new MsgReader.Outlook.Storage.Message("TxtSampleEmail.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);

                    #region Generate and save html

                    // Get Html
                    HtmlGenerator oHtmlGenerator = new HtmlGenerator();
                    oHtmlGenerator.Title = $"Subject: {msg.Subject}";

                    oHtmlGenerator.AddParagraphBodyItem($"File Name: {msg.FileName}");
                    oHtmlGenerator.AddParagraphBodyItem($"From: {from}");
                    oHtmlGenerator.AddParagraphBodyItem($"Sent On: {(sentOn.HasValue ? sentOn.Value.ToString("MM/dd/yyyy HH:mm") : "")}");
                    oHtmlGenerator.AddParagraphBodyItem($"To: {recipientsTo}");
                    oHtmlGenerator.AddParagraphBodyItem($"Subject: {msg.Subject}");

                    if (!string.IsNullOrEmpty(recipientsCc))
                    {
                        oHtmlGenerator.AddParagraphBodyItem($"CC: {recipientsCc}");
                    }

                    oHtmlGenerator.AddRawBodyItem("<hr/>");

                    var msgBodySplitted = msg.BodyText.Split("\n".ToCharArray());

                    foreach (var itmBody in msgBodySplitted)
                    {
                        oHtmlGenerator.AddParagraphBodyItem(itmBody);
                    }

                    // Generate Html
                    oHtmlGenerator.SaveHtml("result.html");

                    #endregion

                    using (HtmlToPdfConverter converter = new HtmlToPdfConverter())
                    {
                        converter.PageSize    = PaperKind.A4;
                        converter.Orientation = Bytescout.PDF.Converters.PaperOrientation.Portrait;

                        converter.ConvertHtmlToPdf("result.html", "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();
            }
        }