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 a simple example, but the code is not written in a special 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
1.PDF File WriterBasic Introduction1.1 Common features supported
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.
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
Private pdffontarialnormal;private pdffontarialbold;private pdffontarialitalic;private PdfFontArialBoldItalic; Private pdffonttimesnormal;private pdffontcomic;private Pdftilingpattern watermark;private PdfDocumentDocument; Private Pdfpagepage;private pdfcontentscontents;
Then create a blank document
Step 1: Create empty documents, document parameters have types, can be selected using enumerations, and return file names 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 a different font type, as shown in 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 Creating a text sample
Contents.drawtext (Comic, 40.0, 4.25, 9.25, Textjustify.center, 0.02, Color.FromArgb (0, 255), Color.FromArgb (255, 0, "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 new pages page = new Pdfpage (Document);//step 4: Add content to 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 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; //Setting 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 Pdfdocument.createfile ();//open PDF file Process Proc = new process (); Proc.startinfo = new ProcessStartInfo (FileName); Proc.start ();
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.
. NET Platform Open Source project quick glance (+) C # write PDF file class library PDF document Writer introduction