Leaf Software Solutions
  • Custom
    • Custom Applications
    • Power Platform >
      • Power Apps
      • Power BI
      • Power Automate
      • MS Teams
    • Business Innovation
  • CRM
    • Microsoft Dynamics 365
  • ERP
    • Sage Intacct >
      • Outgrowing Quickbooks
    • Microsoft Dynamics 365 Business Central
    • Microsoft Dynamics GP >
      • Microsoft Dynamics GP User Group
      • Microsoft Dynamics GP Training
  • About Leaf
    • About Us
  • Schedule Appointment

Leaf Blog

HTML Emails / PDF / C# .NET

10/10/2017

Comments

 

Converting HTML Emails to PDF in C# .NET
By Ted Elliott, Systems Engineer

We had a requirement from a customer to turn emails that they received into a pdf so that they could be stored in the system along-side the work item that was created based on the email.  I did some research and found some components to help me do that.  The final solution uses a mixture of HtmlAgilityPack, HtmlRenderer, and PDFSharp, which are all free open source libraries.  HtmlRenderer was the key to the solution as it takes html and converts it to pdf, including all the styles.  HtmlAgilityPack is used to manipulate and traverse html, while PDFSharp is used by HtmlRenderer to create pdf documents.
Picture
We’ll start by loading up an email.  For this example I’m just going to populate a System.Net.Mail.MailMessage object with some values, but in actual solution you would probably read from something like MS Exchange or some email provider service.  The actual email looks something like this:
Picture
Here's the code to load it:

    
Once we retrieve the email, in addition to storing the body of the email, we’ll also want the subject, from, to and other relevant information for the email.  We’ll create an html document with HtmlAgilityPack to hold all this info.

​We’ll start with a skeleton html document structure loaded from an html string as this is far easier than creating each node of the html document by hand.
Picture
Then once that is created we’ll grab some references to the head and body nodes so we can add our content.  I use the AppendField helper method which just creates a little html structure with a label and it’s text like so:

    

    
Then we’ll load up the body html (or plain text possibly) into its own document, to prepare for copying it into the document that we’re creating.  We’ll add a Body label to put the contents of the email body into the document.

    
To get the document to look as much like the original as possible we copy the styles from the head tag of the email body to the head tag in our document.

    
Once we’ve done that we create a div tag to hold the html body of the email and copy it in.

    
Once we have the email loaded and an html document generated from the email, then we save HtmlDocument to a string to prepare for converting to pdf.  We pass the html string to HtmlGenerator and it gets saved to a pdf.

    
The resulting pdf file looks like this:
Picture
Comments
    Blog Home

    Archives

    January 2021
    November 2020
    October 2020
    September 2020
    August 2020
    June 2020
    November 2018
    August 2018
    July 2018
    May 2018
    April 2018
    February 2018
    October 2017
    August 2017
    June 2017
    May 2017
    March 2017


    Categories

    All
    365 Business
    365 Enterprise
    Advanced Analytics
    Agile
    Agility
    AI
    Amazon Web Services
    Assisted Living
    Automation
    AWS
    AWS Lambda
    Best Companies Group
    Best Places To Work
    Big Data
    Blockchain
    Business
    Business Applications
    C#
    Canal Nights
    CarDon
    Carmel
    CFO
    Chamber Of Commerce
    Chief Financial Officer
    Cloud
    Cloud Computing
    Cloud Financial Software
    Cloud-Native
    Cognitive Computing
    Collaboration
    Company Data
    Continuous Build
    Corona Virus
    Corporate Dna
    Covid 19
    Covid-19
    Culture
    Customer Service
    Demographics
    Deployment Automation
    Developer Ergonomics
    Doc Date
    Doc Date Verify
    Docker
    Douglas Karr
    Dynamics 365
    Dynamics 365 Apps
    E-commerce
    ERP
    Field Service
    Finance
    Financial Accountability
    Financial Dashboards
    Financial Reports
    Financial Software
    Financial Transparency
    Framework
    Fund Accounting
    Game Development
    Hoosiers
    How To
    HTML
    HtmlDocument
    In-Depth Insights
    Indiana
    Indianapolis
    Indiana State Museum
    Indy
    In-Memory Computing
    Internet Of Things
    IoT
    JavaScript
    Joseph Graves Associates
    Kanban
    LCD Welcome Screen
    Leadership
    Leaf Corporate
    Lean
    Machine Learning
    Manifesto
    Manufacturing
    Marketing
    Mechanical Keyboard
    Methodologies
    Microservices
    Microsoft
    Microsoft 365
    Microsoft Dyanmics 365
    Microsoft Dynamics 365
    Microsoft Dynamics CRM
    Microsoft Dynamics GP
    Microsoft .NET
    Microsoft Visual Basic
    Migrating Cloud Applications
    Mike Wilson
    Mobility & Security
    MS Teams
    .NET
    Nonprofit
    Not For Profit
    Office 365
    Off The Circle
    OpenCV
    Open-Source
    Operational Efficiency
    Operational Insights
    Operations
    PDF
    Performance Insights
    Philosophy
    Power BI
    Practices
    Press Release
    Process Robotics
    Professional Services Tools Library
    Project Service Automation
    PSTL
    Python
    Python Tornado Web Framework
    Rapid Prototyping
    Raspberry PI
    Repetitive Stress Injuries
    Ruby
    SAFe
    Sage Intacct
    Sales
    Scrum
    Senior Living
    Serverless Applications
    Serverless Techniques
    Single-Page App Framework
    Slack
    Software
    Software Solutions
    SPA Framework
    Spreadsheets
    Stakeholder Insights
    SupplyKick
    Systems
    Tech-Savvy
    Tracking Application
    Traditional Vs Machine Learning
    Unit Test Fixtures
    Video Games
    Visibility
    Visualization
    Waterfall
    Windows 10
    XP

STAY IN TOUCH


(317) 814-8000
info@leafsoftwaresolutions.com
Schedule Appointment
Picture

MORE ABOUT LEAF


Our Culture
Leaf Blog
Careers
Contact Us
Best Places to Work
2012-2020

OUR SOLUTIONS


CUSTOM SOFTWARE
  • Custom Applications
  • Power Platform
  • Business Innovation
CRM SOFTWARE
  • Microsoft Dynamics 365
ERP SOFTWARE
  • Sage Intacct
  • Microsoft Dynamics 365 BC
  • Microsoft Dynamics GP​

OUR PARTNERS


Picture
Picture
Picture
Proud sponsor of WFYI Public Media since 2006.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.
Terms and Conditions
Privacy Policy
© 2020 Leaf Software Solutions, Inc. 
  • Custom
    • Custom Applications
    • Power Platform >
      • Power Apps
      • Power BI
      • Power Automate
      • MS Teams
    • Business Innovation
  • CRM
    • Microsoft Dynamics 365
  • ERP
    • Sage Intacct >
      • Outgrowing Quickbooks
    • Microsoft Dynamics 365 Business Central
    • Microsoft Dynamics GP >
      • Microsoft Dynamics GP User Group
      • Microsoft Dynamics GP Training
  • About Leaf
    • About Us
  • Schedule Appointment