Background
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.
1 2 3 4 5 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//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:
1 2 3 4 5 6 7 8 9 10 11 |
//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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
// 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