Create PDF/XPS/Doc/Docx/Html documents with Spire.PDF

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 880 red flag

In this article, we will create PDF/XPS/Doc/Docx/Html file(s) using Spire.PDF


 Download source code for Create PDF/XPS/Doc/Docx/Html documents with Spire.PDF

Recommendation
Read Mathematical Expression Evaluation with NCalc before this article.

Introduction

Spire.PDF for .NET is a versatile PDF library that enables software developers to generate, edit, read and manipulate PDF files within their own .NET applications. As a standalone PDF component, Spire.PDF for .NET provides users an incredible wealth of features without installing Adobe Acrobat. This component is completely written in C#, but also supports VB.NET, Windows Forms and ASP.NET.

With the Spire.PDF for .NET API, we can get the ability to read PDF text and image even attachments with high efficiency. Even the special content such as language written from right to left can be extracted such as Herbrew and Arabic. Other basic features such as text, image, table, shapes, hyperlinks, bookmark, headings, footnotes, lists, form fields, pagination, booklet barcode, attachments, transparency, annotations, actions, overlay etc all work great for users. As for security, Spire.PDF for .NET enables we to protect your PDF files by passwords and digital signature.

In this article, we will create PDF/XPS/Doc/Docx/Html file(s) using Spire.PDF

Using the code

Let us open a Console Application and then from the Nu-get Package Manager Console install Spire.PDF for .NET as under

PM> Install-Package Spire.PDF

Now, let us write the below piece of code as under

using Spire.Pdf;
using Spire.Pdf.Barcode;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Linq;

namespace CreatePDFWithSpirePDF
{
    class Program
    {
        static void Main(string[] args)
        {

            //create a pdf document.            
            using (var doc = new PdfDocument())
            {
                //create one page
                PdfPageBase page = doc.Pages.Add();
                float pageWidth = page.Canvas.ClientSize.Width;
                float y = 0;

                //create page header
                var pen = new PdfPen(Color.BurlyWood, 4f);
                var brush = new PdfSolidBrush(Color.BlueViolet);
                var font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
                var format = new PdfStringFormat(PdfTextAlignment.Right);
                var text = "Demo of PDF documents creation with Spire.PDF - RNA Team";
                page.Canvas.DrawString(text, font, brush, pageWidth, y, format);
                var size = font.MeasureString(text, format);
                y = y + size.Height + 1;
                page.Canvas.DrawLine(pen, 0, y, pageWidth, y);

                //create page title
                y = y + 5;
                PdfBrush brush2 = new PdfSolidBrush(Color.Black);
                PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
                PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center);
                format2.CharacterSpacing = 1f;
                text = "Know about Spire.PDF for .NET";
                page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);
                size = font2.MeasureString(text, format2);
                y = y + size.Height + 6;

                //add barcode to the document
                var barcode = new PdfCode128BBarcode("this is the sample bar code generated by using Spire.PDF- RNA Team");
                barcode.BarHeight = 20;
                barcode.BarcodeToTextGapHeight = 1f;
                barcode.TextDisplayLocation = TextLocation.Bottom;
                barcode.TextColor = Color.Fuchsia;
                barcode.Draw(page, new PointF(0, y));
                y = y + 30;

                //add picture to the document
                PdfImage image = PdfImage.FromFile(@"1.png");
                page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y));
                var imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2;
                var imageBottom = image.PhysicalDimension.Height + y;

                //add text content to the document
                var fmt = new PdfStringFormat();               
                text = System.IO.File.ReadAllText(@"1.txt");
                var fnt = new PdfTrueTypeFont(new Font("Arial", 15f));
                fmt.LineSpacing = fnt.Size * 1.5f;
                PdfStringLayouter textLayouter = new PdfStringLayouter();
                float imageLeftBlockHeight = imageBottom - y;
                var result = textLayouter.Layout(text, fnt, fmt, new SizeF(imageLeftSpace, imageLeftBlockHeight));

                if (result.ActualSize.Height < imageBottom - y)
                {
                    imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight;
                    result = textLayouter.Layout(text, fnt, fmt, new SizeF(imageLeftSpace, imageLeftBlockHeight));
                }

                result.Lines.ToList().ForEach
                (
                    line =>
                    {
                        page.Canvas.DrawString(line.Text, fnt, brush2, 0, y, fmt);
                        y = y + result.LineHeight;
                    }
                );
               
                var textWidget = new PdfTextWidget(result.Remainder, fnt, brush2);
                var textLayout = new PdfTextLayout();
                textLayout.Break = PdfLayoutBreakType.FitPage;
                textLayout.Layout = PdfLayoutType.Paginate;
                RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
                textWidget.StringFormat = fmt;
                textWidget.Draw(page, bounds, textLayout);

                 //finally save the document(s)

                doc.SaveToFile("RNADoc.pdf", FileFormat.PDF); //pdf format
                doc.SaveToFile("RNADoc.xps", FileFormat.XPS);  //xps format             
                doc.SaveToFile("RNADoc.doc", FileFormat.DOC); //doc format
                doc.SaveToFile("RNADoc.docx", FileFormat.DOC); //docx format
                doc.SaveToFile("RNADoc.html", FileFormat.HTML); //html format
            }           

            //view the file(s)

            //open the pdf
            System.Diagnostics.Process.Start("RNADoc.pdf");

            //open the xps
            System.Diagnostics.Process.Start("RNADoc.xps");

            //open the doc
            System.Diagnostics.Process.Start("RNADoc.doc");

            //open the docx
            System.Diagnostics.Process.Start("RNADoc.docx");

        }
    }
}

The code is heavily documented and henceforth no further explanation was provided.

The output will be similar to as under (for the pdf document)

Reference

Free .NET PDF Library

Conclusion

Hope this will be helpful.Thanks for reading.Zipped file attached.

Recommendation
Read Map Domain Objects using Automapper after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)