Convert HTML String to PDF using Free API in C#

Converting from HTML to PDF is not an easy task. There are some tools that can do this, but most of them costs thousands of dollars. This article provides a free solution with only few lines of C# code, using free Spire.PDF.

Free Spire.PDF is a community edition of the fully featured Spire.PDF for .NET. It allows you to create PDF documents directly from HTML pages in your ASP.NET and MVC Websites, Desktop Applications and Services.

You can use the free library in .NET 4.0 and later applications, in C# and VB.NET languages, on 32-bit and 64-bit Windows machines.

2017-02-07_144504

Using the code

PdfDocument pdf = new PdfDocument();
PdfPageSettings setting = new PdfPageSettings();
setting.Size = PdfPageSize.A4;

PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
htmlLayoutFormat.IsWaiting = false;                    
string htmlCode = File.ReadAllText("HTML example.html");
Thread thread = new Thread(() =>
{ pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();

pdf.SaveToFile("Html2Pdf.pdf");

Output

2017-02-07_144945

Advertisements

How to Print PDF to XPS in C#/VB.NET

Windows 7 comes with a Microsoft XPS Document Writer (MXDW) printer which can be used to print any document to an XPS file. It is also included in later releases, such as Windows 10. This article will introduce how to programmatically connect MXDW and print a PDF file into a XPS file.

To use following code snippets to print PDF file, you need to install Spire.PDF via NuGet and add the dll files into .NET assemblies.

Code Snippets

using Spire.Pdf;
namespace PrintPdfToXps
{
    class Program
    {
        static void Main(string[] args)
        {
            //load a Pdf document
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"example.pdf");

            //specify printer 
            doc.PrintDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
            //print Pdf to a file instead a port
            doc.PrintDocument.PrinterSettings.PrintToFile = true;
            doc.PrintDocument.PrinterSettings.PrintFileName = "PrintToXps.xps";

            //start printing process
            doc.PrintDocument.Print();
        }
    }
}

Split Word Documents by Page Breaks or Section Breaks in C#

Page breaks and section breaks are two useful features for controlling page layout in MS Word and other desktop publishing programs. Page breaks are used to end a page without filling it with text. Section breaks are used to allow formatting changes (i.e., different margins, page number styles, etc.) in the same document.

Sometimes, we may want to pull the different parts that are separated by page breaks or section breaks out of the whole document storing as several individual files. This article provides two brilliant solutions in C# to split a Word document by page breaks and section breaks.

Split by Page Breaks

using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Split_Word_Document_by_Page_Break
{
    class Program
    {
        static void Main(string[] args)
        {
            Document original = new Document();
            original.LoadFromFile("New Zealand.docx");
            Document newWord = new Document();
            Section section = newWord.AddSection();

            int index = 0;
            foreach (Section sec in original.Sections)
            {
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph para = obj as Paragraph;
                        section.Body.ChildObjects.Add(para.Clone());

                        foreach (DocumentObject parobj in para.ChildObjects)
                        {
                            if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak)
                            {
                                int i = para.ChildObjects.IndexOf(parobj);
                                section.Body.LastParagraph.ChildObjects.RemoveAt(i);
                                newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx);
                                index++;

                                newWord = new Document();
                                section = newWord.AddSection();
                                section.Body.ChildObjects.Add(para.Clone());
                                if (section.Paragraphs[0].ChildObjects.Count == 0)
                                {
                                    section.Body.ChildObjects.RemoveAt(0);
                                }
                                else
                                {
                                    while (i >= 0)
                                    {
                                        section.Paragraphs[0].ChildObjects.RemoveAt(i);
                                        i--;
                                    }
                                }
                            }
                        }
                    }
                    if (obj is Table)
                    {
                        section.Body.ChildObjects.Add(obj.Clone());
                    }
                }
            }
            newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx);
        }
    }
}

Split by Section Breaks

using System;
using Spire.Doc;

namespace Split_Word_Document
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("Test.doc");
            Document newWord;
            for (int i = 0; i < document.Sections.Count; i++)
            {
                newWord = new Document();
                newWord.Sections.Add(document.Sections[i].Clone());
                newWord.SaveToFile(String.Format(@"test\out_{0}.docx", i));
            }
        }
    }
}

Note: These solutions are relied on free .NET Word Component, you can download it from here and reference the DLL file to your own project.

View and Print Office Files in Windows Forms Application

Nowadays, we are most likely to receive or create electronic reports in office file formats, such as .docx, .xlsx, .pptx and .pdf. It’s quite often that programmers are requested to build a .NET application that can view and print office files, so that their colleagues or customers can access the documents too.

This article presents how to view and print Word, Excel, PowerPoint and PDF Documents in a Windows Forms Application via a powerful component Spire.OfficeViewer. To create your own Office document viewer,  you need firstly integrate Spire.OfficeViewer controls to Toolbox in your Visual Studio. Here is how to.

  • Download Spire.OfficeViewer and install it on system
  • Right-click on Toolbox panel, click ‘Add Tab’ to add a new tab
  • Right-click the new tab and select ‘Choose Items…’
  • Choose ‘.NET Framework Components’ tab
  • Click ‘Browse…’ button
  • Choose ‘Spire.OfficeViewer.Forms.dll’ in open file dialog
  • Click ‘OK’, then the controls will be successfully added to Toolbox

Drag ‘ OfficeViewer’ control to form1 and it looks like:

2016-11-07_160901

Run the program, you’re able to get an Office viewer like below screenshot. Look at the toolbar, it provides many more functions other than Open and Print.

2016-11-07_162015

Click Open button to open an office file from disk.

2016-11-07_163716

It is extremely easy to build an Offer viewer using this control, right? If you want to custom the toolbar with specified functions, just remove ‘OfficeViewer’ control and drag ‘DocumentViewer’ control to the form1, and then create your own toolbar using the Common Controls provided by VS, but you need to figure out the code inside the toolbar.

How to Create a Signature Field in PDF in C#

A signature field enables the user to place his signature in a form. This field type allows instant validation from a customer through his digital signature, eliminating the need of paper work. To prepare a document for digital signing, add one or more digital signature fields (depending on how many signatures are needed). This article presents how to create signature fields in a PDF document using Spire.PDF in C#.

Adding Necessary Namespaces

using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;

Using the code

STEP 1. Create a PDF document, create a blank page.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

STEP 2. Draw some text in PDF.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);
PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
string s1 = "Representative #1";
string s2 = "Representative #2";
SizeF size = font.MeasureString(s1);
int x = 180;
int y1 = 120, y2 = 180;
page.Canvas.DrawString(s1, font, brush, new PointF(x, y1));
page.Canvas.DrawString(s2, font, brush, new PointF(x, y2));

STEP 3. Draw two line shapes on PDF.

PdfPen pen = new PdfPen(Color.Black, 0.5f);
page.Canvas.DrawLine(pen, new PointF(x + size.Width + 2, y1 +size.Height+ 2), new PointF(x + size.Width + 126, y1 + size.Height + 2));
page.Canvas.DrawLine(pen, new PointF(x + size.Width + 2, y2 + size.Height + 2), new PointF(x + size.Width + 126, y2 + size.Height + 2));

STEP 4. Create two signature fields in PDF and set the properties respectively.

PdfSignatureField signaturefield1 = new PdfSignatureField(page, "Signature1");
signaturefield1.BorderWidth = 1.0f;
signaturefield1.BorderStyle = PdfBorderStyle.Solid;
signaturefield1.BorderColor = new PdfRGBColor(System.Drawing.Color.White);
signaturefield1.HighlightMode = PdfHighlightMode.Outline;
signaturefield1.Bounds = new RectangleF(x+size.Width+5, y1 - 50 + size.Height, 120, 50);

PdfSignatureField signaturefield2 = new PdfSignatureField(page, "Signature2");
signaturefield2.BorderWidth = 1.0f;
signaturefield2.BorderStyle = PdfBorderStyle.Solid;
signaturefield2.BorderColor = new PdfRGBColor(System.Drawing.Color.White);
signaturefield2.HighlightMode = PdfHighlightMode.Outline;
signaturefield2.Bounds = new RectangleF(x+size.Width+5, y2 - 50 + size.Height, 120, 50);

doc.Form.Fields.Add(signaturefield1);
doc.Form.Fields.Add(signaturefield2);

STEP 5. Save to file.

doc.SaveToFile("SignatureField.pdf", FileFormat.PDF);

Output

addsignature