Read Catalogue
- 1.PDF File Writer Basic introduction
- 2. A simple use case
- 3. Resources
1 years ago, I was in the article: these. NET open source project you know? NET platform open source document and report Processing component (9th), we recommend an open source free PDF Read-write component Pdfsharp,pdfsharp I have seen it 2 years ago, using simple examples, but the code is not written in a specific article. Recently, when looking for information, I found a small C # component that wrote PDF files: PDF file writer. The open source component is in CodeProject, not yet hosted elsewhere, so it took some time to get to know it and share it with everyone.
. NET Open Source directory: "Directory" of this blog other. NET open source project articles Directory
This text address: http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_PdfFileWriter.html
Back to directory 1. PDF File writer Basic Introduction 1.1 supported common functions
The PDF file Writer component can create pdf-formatted files directly in. NET Applications. The lowest development environment is. NET 4.0 + VS 2013. Let's take a look at some of the features of the PDF that the component supports:
Graphics: Supports drawing lines, rectangles, polygons, Bezier curves, foreground and background colors, patterns, and shadows.
Image: Supports bitmap images and vector images
Text: Supports line text and column text
Barcode: Support Barcode: Barcode, Barcode, Barcode interleaved 2 of 5 etc.
QR Code: Support two-dimensional barcode
Encryption: Supports AES-128 encryption algorithm
Web Links: Support Web hyperlinks
Bookmarks: Supporting document outlines
Chart: Support Microsoft chart, support data table, support sound, video playback;
1.2 Steps to create a PDF using PDF File writer
The main steps to create a PDF file in a program using PDF document writer are as follows:
Step 1: Create a Pdfdocument file object
Step 2: Create resource objects such as text (Pdffont), images (pdfimage), etc.
Step 3: Create a File Page object Pdfpage
Step 4: Create a Content Object pdfcontents
Step 5: Add text to content objects, or images, etc.
Repeat 3, 4, 5 create additional pages
Step 6: Create a PDF using the CreateFile method of the Pdfdocument object
PDF file effect preview created by 1.3 PDF document Writer
It's a good idea to look at the PDF created with PDF File writer. This is one of the most important reasons why I occasionally come across very shocked and shared.
Back to Table of Contents 2. A simple use case
We can get started quickly and take a look at the basic code, according to the official examples.
2.1 Create a Base object first
|
1
2
3
4
5
6
7
8
9
10
|
private PdfFont ArialNormal;
private PdfFont ArialBold;
private PdfFont ArialItalic;
private PdfFont ArialBoldItalic;
private PdfFont TimesNormal;
private PdfFont Comic;
private PdfTilingPattern WaterMark;
private PdfDocument Document;
private PdfPage Page;
private PdfContents Contents;
|
|
Then create a blank document
|
// Step 1: Create an empty document, the document parameters have types, you can use enumeration to select, and the file name returned Document = new PdfDocument (PaperType.Letter, false, UnitOfMeasure.Inch, FileName); // Cryptographic test example //Document.SetEncryption(null, null, Permission.All & ~ Permission.Print, EncryptionType.Aes128); // Create PDF file information directory PdfInfo Info = PdfInfo.CreatePdfInfo (Document); Info.Title ("Article Example"); Info.Author ("Uzi Granot Granotech Limited"); Info.Keywords ("PDF, .NET, C #, Library, Document Creator"); Info.Subject ("PDF File Writer C # Class Library (Version 1.14.1)"); |
2.2 Creating resources such as fonts
|
// Define different font types, as shown below String FontName1 = "Arial"; String FontName2 = "Times New Roman"; ArialNormal = PdfFont.CreatePdfFont (Document, FontName1, FontStyle.Regular, true); ArialBold = PdfFont.CreatePdfFont (Document, FontName1, FontStyle.Bold, true); ArialItalic = PdfFont.CreatePdfFont (Document, FontName1, FontStyle.Italic, true); ArialBoldItalic = PdfFont.CreatePdfFont (Document, FontName1, FontStyle.Bold | FontStyle.Italic, true); TimesNormal = PdfFont.CreatePdfFont (Document, FontName2, FontStyle.Regular, true); Comic = PdfFont.CreatePdfFont (Document, "Comic Sans MS", FontStyle.Bold, true); 2.3 Create text example |
2.3 Creating a text sample
|
Contents.DrawText (Comic, 40.0, 4.25, 9.25, TextJustify.Center, 0.02, Color.FromArgb (128, 0, 255), Color.FromArgb (255, 0, 128), "PDF FILE WRITER"); Contents.SaveGraphicsState (); Contents.SetColorNonStroking (Color.Purple); Contents.DrawText (Comic, 30.0, 4.25, 8.75, TextJustify.Center, "Example"); Contents.RestoreGraphicsState (); // Step 3: Add a new page Page = new PdfPage (Document); // Step 4: Add content to the page Contents = new PdfContents (Page); |
2.4 Drawing Barcodes
|
Contents.SaveGraphicsState (); BarcodeEAN13 Barcode1 = new BarcodeEAN13 ("1234567890128"); Contents.DrawBarcode (1.3, 7.05, 0.012, 0.75, Barcode1, ArialNormal, 8.0); PdfQRCode QRCode = new PdfQRCode (Document, "http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version", ErrorCorrection.M); Contents.DrawQRCode (QRCode, 6.0, 6.8, 1.2); // Add a link Page.AddWebLink (6.0, 6.8, 7.2, 8.0, "http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version"); //save Contents.RestoreGraphicsState (); |
2.5 Drawing a chart
|
Contents.SaveGraphicsState (); // Create MS Chart chart Chart PieChart = PdfChart.CreateChart (Document, 1.8, 1.5, 300.0); PdfImageControl ImageControl = new PdfImageControl (); ImageControl.SaveAs = SaveImageAs.IndexedImage; PdfChart PiePdfChart = new PdfChart (Document, PieChart, ImageControl); PieChart.AntiAliasing = AntiAliasingStyles.None; // Set the color PieChart.BackColor = Color.FromArgb (220, 220, 255); PieChart.Palette = ChartColorPalette.BrightPastel; // default font Font DefaultFont = PiePdfChart.CreateFont ("Verdana", FontStyle.Regular, 0.05, FontSizeUnit.UserUnit); Font TitleFont = PiePdfChart.CreateFont ("Verdana", FontStyle.Bold, 0.07, FontSizeUnit.UserUnit); // set the title Title Title1 = new Title ("Pie Chart Example", Docking.Top, TitleFont, Color.Purple); PieChart.Titles.Add (Title1); //legend Legend Legend1 = new Legend (); PieChart.Legends.Add (Legend1); Legend1.BackColor = Color.FromArgb (230, 230, 255); Legend1.Docking = Docking.Bottom; Legend1.Font = DefaultFont; // chart area ChartArea ChartArea1 = new ChartArea (); PieChart.ChartAreas.Add (ChartArea1); ChartArea1.BackColor = Color.FromArgb (255, 200, 255); Series Series1 = new Series (); PieChart.Series.Add (Series1); Series1.ChartType = SeriesChartType.Pie; Series1.Font = DefaultFont; Series1.IsValueShownAsLabel = true; Series1.LabelFormat = "{0}%"; Series1.Points.Add (22.0); Series1.Points [0] .LegendText = "Apple"; Series1.Points.Add (27.0); Series1.Points [1] .LegendText = "Banana"; Series1.Points.Add (33.0); Series1.Points [2] .LegendText = "Orange"; Series1.Points.Add (18.0); Series1.Points [3] .LegendText = "Grape"; Contents.DrawChart (PiePdfChart, 5.6, 5.0); // save Contents.RestoreGraphicsState (); |
2.6 Generate PDF
|
// Step 6: Create PDF Document.CreateFile (); // Open PDF file Process Proc = new Process (); Proc.StartInfo = new ProcessStartInfo (FileName); Proc.Start (); |
Back to catalog 3. Resources
1.Codeproject Article connection: http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version
2.PDF File Writer dll Download: pdffilewriter_dll.zip
3.PDF file Writer Help document: Pdffilewriterchm.rar
4.PDF File Writer source code and Demo:pdffilewriter-code.rar
Note: The relevant material in the source code is streamlined, otherwise the file is larger and the long passing is larger. If there is a need to go to the article link to download the original, or leave a separate mailbox, I have the time to send.
C # write PDF file class library PDF document Writer introduction