As a computer programmer, sometimes you may need to automatically create and edit PDF document. Here I present you a free PDF processing library (Spire.PDF) which just meets the need.
Introduction
This library is very powerful and easy to use. Check the sample code below. Just three line of code, it creates a PDF document with one blank page. See, it is very easy. The method Add will just add a blank page into the PDF document.
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); doc.SaveToFile("result.pdf");
After adding a blank page, you should add some content to the page. Unless a blank page is what you want. You can add text, image, shapes, barcode, table, grid and all other stuff you want to page of PDF document.
These are some methods that the library provides:
page.Canvas.DrawString()
Add text to page.
page.Canvas.DrawImage()
Add image to page.
Class: PdfUnidimensionalBarcode
Draw(PdfPageBase page, PointF location);
Add barcode to page.
Class: PdfTable
Draw(page, new PointF(0, y));
Add table to page.
This library also supports watermark, encryption, digital signature, conversion to many file format, merge documents, split document, extracting text and image…. In a word, you can manipulate PDF files easily and flexibly using this library.
For example:
Encrypt the document:
//load pdf file PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //set the password doc.Security.UserPassword = "test"; //save pdf file. doc.SaveToFile("Encryption.pdf"); doc.Close();
Convert to XPS file:
//load pdf file PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //convert to xps file. doc.SaveToFile("result.xps", FileFormat.XPS); doc.Close();
Sample Code
In this part, I will give a demo showing how to use this library to create a very simple PDF document. If you find it difficult to understand the code, please check the above part.
Code:
// create a pdf document. PdfDocument doc = new PdfDocument(); // create one page PdfPageBase page = doc.Pages.Add(); float pageWidth = page.Canvas.ClientSize.Width; float y = 0; //add barcode PdfCodabarBarcode barcode1 = new PdfCodabarBarcode("00:12-3456/7890"); barcode1.BarcodeToTextGapHeight = 1f; barcode1.EnableCheckDigit = true; barcode1.ShowCheckDigit = true; barcode1.TextDisplayLocation = TextLocation.Bottom; barcode1.TextColor = Color.Blue; barcode1.BarHeight = 20; barcode1.Draw(page, new PointF(5, 3)); y = y + 32; PdfBrush brush2 = new PdfSolidBrush(Color.Black); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); // add image into document PdfImage image = PdfImage.FromFile("pic.bmp"); page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y)); float imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2; float imageBottom = image.PhysicalDimension.Height + y; // add text into document PdfStringFormat format4 = new PdfStringFormat(); String text = System.IO.File.ReadAllText("sample.txt"); PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial", 10f)); format4.LineSpacing = font5.Size * 1.5f; PdfStringLayouter textLayouter = new PdfStringLayouter(); float imageLeftBlockHeight = imageBottom - y; PdfStringLayoutResult result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); if (result.ActualSize.Height < imageBottom - y) { imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight; result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); } foreach (LineInfo line in result.Lines) { page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4); y = y + result.LineHeight; } PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2); PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize); textWidget.StringFormat = format4; textWidget.Draw(page, bounds, textLayout); // save pdf file. doc.SaveToFile(@"..\..\sample.pdf"); doc.Close();
Conclusion:
This would help Developers if they are searching for any handy tool to reduce their work load ..
Happy Coding
]]>I could not believe that we could incorporate Microsoft Word document creation capabilities into any developers’ .NET applications. Strange right!! It supports C#, VB.NET, ASP.NET and ASP.NET MVC. One important thing is it can convert the documents to the formats with High Quality.
Its easy and simple, not complex as trivial ones. The Spire.Doc installation is clean, professional and wrapped up in a MSI installer. The first few slides are mandatory informatory and license agreement as it is in most of the products we install right!! and none of us in individuals when we install read the EULA(End User License Agreement)but as an organization or company, it is required to completely go through it before investing.
Spire Doc. is a light weight product, it consumes only 180 MB for installation. Yes when as we download and install it creates chaos if our net speed is slow, but thats not the case in any organizations.
The MSI option provides a full experience, including:
After the installation, the developer must manually add a reference to the assembly. Locally-installed documentation is available by means of Windows HTML Help, which is completely available and searchable while disconnected from the Internet.
Below are the list of functionalities provided by the Spire.Doc(I call it “Magic Doc“):-
And Many More…This is what I explored in this span of time.
STEPS
Spire.Doc.Document document = new Doc.Document(); //instance of the class Section section = document.AddSection();//adds a section Paragraph para = section.AddParagraph();//adds a paragraph para.AppendText("This is a magic Product"); document.SaveToFile("SpireDemo.docx",doc.FileFormat.Docx);//Saving the file in .doc format
We could save in any format as mentioned for instance
document.SaveToFile("SpireDemo.pdf",doc.FileFormat.pdf);//Saves the file in pdf format
All the File formats are present in an Enumeration FileFormat.cs
Now Question is Did that work?
Yes perfect !! Its working really nice. Playing and juggling with this product, I will explore many new things. Will keep on sharing.
Thank you.(E-Iceblue)
]]>