C # generate PDF summary

Source: Internet
Author: User
Tags pdfinfo

A C # generate PDF summary

(1) Itextsharp control to ITEXTSHARP research can also form, text, various GDI objects, pictures, watermarks, text rotation
(2) Aspose controls
(3) PDF library (only text, tables and text) http://www.codeproject.com/KB/dotnet/PdfLibrary.aspx
(4) directly with the. NET RDLC report can be, to PDF effect is very good, can also deal with the user variable, can to other formats.

(ii) ITEXTSHARP generate PDF Display columns

Using system;using system.collections.generic;using system.linq;using system.text;using System.Windows;using System.windows.controls;using system.windows.data;using system.windows.documents;using System.Windows.Input;using System.windows.media;using system.windows.media.imaging;using system.windows.navigation;using System.windows.shapes;using itextsharp.text;using itextsharp.text.pdf;using system.io;namespace PdfDemo{//< summary>///MainWindow.xaml interactive logic///</summary> public partial class Mainwindow:window {p        Ublic MainWindow () {InitializeComponent ();            }///<summary>///I have to first PDF program///</summary> private void CreatePDF () { String fileName = String.            Empty;            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog (); Dlg.            FileName = "My first pdf"; Dlg.            DEFAULTEXT = ". pdf"; Dlg. Filter = "Text documents (. pdf) |*.pdf"; nullable<bool> result = dlg.            ShowDialog (); if (result = = True) {FileName = dlg.                FileName;                                Document document = new document ();                Pdfwriter.getinstance (document, New FileStream (FileName, FileMode.Create)); Document.                Open ();                ITextSharp.text.Paragraph Paragraph = new ITextSharp.text.Paragraph ("Hello World"); Document.                ADD (paragraph); Document.                         Close ();        }//end if}///<summary> Set page size, author, title and other related information settings///</summary> private void Createpdfsetinfo () {string fileName = string.            Empty;            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog (); Dlg.            FileName = "My first pdf"; Dlg.            DEFAULTEXT = ". pdf"; Dlg.            Filter = "Text documents (. pdf) |*.pdf"; nullable<bool> result = dlg. ShoWdialog (); if (result = = True) {FileName = dlg.                FileName;                Set Page size ITextSharp.text.Rectangle pageSize = new ITextSharp.text.Rectangle (216f, 716f);                Pagesize.backgroundcolor = new ITextSharp.text.BaseColor (0xFF, 0xFF, 0xDE);                Set Border Document document = new Document (PageSize, 36f, 72f, 108f, 180f);                Pdfwriter.getinstance (document, New FileStream (FileName, FileMode.Create)); Add Document Information documents.                AddTitle ("Pdfinfo"); Document.                Addsubject ("Demo of Pdfinfo"); Document.                Addkeywords ("Info, PDF, Demo"); Document.                Addcreator ("Setpdfinfodemo"); Document.                Addauthor ("Gio Tao"); Document.                Open (); Add document contents for (int i = 0; i < 5; i++) {document. ADD (New ITextSharp.text.Paragraph ("Hello world! Hello people!            " +"Hello sky! Hello sun! Hello moon!                              Hello stars! "));} Document.            Close (); }//end if}///<summary>//Create multiple PDF pages///</summary> private void Createn Ewpdfpage () {string fileName = string.            Empty;            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog (); Dlg.            FileName = "Create multiple PDF new pages"; Dlg.            DEFAULTEXT = ". pdf"; Dlg.            Filter = "Text documents (. pdf) |*.pdf"; nullable<bool> result = dlg.            ShowDialog (); if (result = = True) {FileName = dlg.                FileName;                            Document document = new document (Pagesize.note);                                PDFWriter writer= pdfwriter.getinstance (document, New FileStream (FileName, FileMode.Create)); Document.                Open (); The first page of document. ADD (New ITextSharp.text.Paragraph ("PDF1, PDF1, PDF1, PDF1, PDF1 ")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF1, PDF1, PDF1, PDF1, PDF1")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF1, PDF1, PDF1, PDF1, PDF1")); Document.                             ADD (New ITextSharp.text.Paragraph ("PDF1, PDF1, PDF1, PDF1, PDF1")); Add a new page document.                NewPage (); Second page//Add second page content document.                ADD (New ITextSharp.text.Paragraph ("PDF2, PDF2, PDF2, PDF2, PDF2")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF2, PDF2, PDF2, PDF2, PDF2")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF2, PDF2, PDF2, PDF2, PDF2")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF2, PDF2, PDF2, PDF2, PDF2")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF2, PDF2, PDF2, PDF2, PDF2")); Document.                          ADD (New ITextSharp.text.Paragraph ("PDF2, PDF2, PDF2, PDF2, PDF2")); Add a new pageFace document.                NewPage (); Third page//Add new content document.                ADD (New ITextSharp.text.Paragraph ("PDF3, PDF3, PDF3, PDF3, PDF3")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF3, PDF3, PDF3, PDF3, PDF3")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF3, PDF3, PDF3, PDF3, PDF3")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF3, PDF3, PDF3, PDF3, PDF3")); Start Page Count document again.                Resetpagecount (); Create a new page of document.                NewPage (); Fourth page//Add fourth page content document.                ADD (New ITextSharp.text.Paragraph ("PDF4, PDF4, PDF4, PDF4, PDF4")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF4, PDF4, PDF4, PDF4, PDF4")); Document.                ADD (New ITextSharp.text.Paragraph ("PDF4, PDF4, PDF4, PDF4, PDF4")); Document.       ADD (New ITextSharp.text.Paragraph ("PDF4, PDF4, PDF4, PDF4, PDF4"));         Document.            Close ();  }//end if}///<summary> Generate Picture PDF page (insert picture in PDF)///</summary> public void            ImageDirect () {string imagePath = AppDomain.CurrentDomain.BaseDirectory + @ "image\1.jpg";//Temporary file path String fileName = String.            Empty;            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog (); Dlg.            FileName = "My first pdf"; Dlg.            DEFAULTEXT = ". pdf"; Dlg.            Filter = "Text documents (. pdf) |*.pdf"; nullable<bool> result = dlg.            ShowDialog (); if (result = = True) {FileName = dlg.                FileName;                Document document = new document ();                PDFWriter writer = pdfwriter.getinstance (document, New FileStream (FileName, FileMode.Create)); Document.                Open ();                ITextSharp.text.Image img = iTextSharp.text.Image.GetInstance (imagePath); Img. SetAbsolutePosition(Pagesize.postcard.width-img. Scaledwidth)/2, (pagesize.postcard.height-img.                Scaledheight)/2); Writer.                Directcontent.addimage (IMG); ITextSharp.text.Paragraph p = new ITextSharp.text.Paragraph ("Foobar Film Festival", New ITextSharp.text.Font (                Font.FontFamily.HELVETICA, 22f));                P.alignment = Element.align_center; Document.                ADD (P); Document.            Close ();            }//end if} private void Readpdf () {Console.WriteLine ("Read PDF document"); try {//Create a Pdfreader object Pdfreader reader = new Pdfreader (@ D:\ Technical Documentation \sj\c# Threading Reference Manual. P                DF "); get document pages int n = reader.                Numberofpages; Get the size of the first page iTextSharp.text.Rectangle psize = reader.                GetPageSize (1); Float width = psize.                Width; float height = psize.                Height; Create a document variable documents = new DOCUment (Psize, 50, 50, 50, 50); Create the document PDFWriter writer = pdfwriter.getinstance (document, New FileStream (@ "C:\Read.pdf", FileMode.Create)                ); Open document documents.                Open (); Add content Pdfcontentbyte cb = writer.                Directcontent;                int i = 0;                int p = 0;                Console.WriteLine ("A total of" + N + "pages."); while (I < n) {document.                    NewPage ();                    p++;                    i++; Pdfimportedpage Page1 = writer.                    Getimportedpage (reader, i); Cb.                    Addtemplate (Page1,. 5f, 0, 0,. 5f, 0, HEIGHT/2);                    Console.WriteLine ("Process" + i + "page");                        if (I < n) {i++; Pdfimportedpage Page2 = writer.                        Getimportedpage (reader, i); Cb. Addtemplate (Page2,. 5f, 0, 0,. 5f, WIDTH/2, Height/2);                    Console.WriteLine ("Process" + i + "page");                        } if (I < n) {i++; Pdfimportedpage Page3 = writer.                        Getimportedpage (reader, i); Cb.                        Addtemplate (Page3,. 5f, 0, 0,. 5f, 0, 0);                    Console.WriteLine ("Process" + i + "page");                        } if (I < n) {i++; Pdfimportedpage Page4 = writer.                        Getimportedpage (reader, i); Cb.                        Addtemplate (Page4,. 5f, 0, 0,. 5f, WIDTH/2, 0);                    Console.WriteLine ("Process" + i + "page"); } CB.                    Setrgbcolorstroke (255, 0, 0); Cb.                    MoveTo (0, HEIGHT/2); Cb.                    LineTo (width, HEIGHT/2); Cb.                    Stroke (); Cb.                    MoveTo (WIDTH/2, height); Cb.        LineTo (WIDTH/2, 0);            Cb.                    Stroke ();                    Basefont bf = Basefont.createfont (Basefont.helvetica, basefont.cp1252, basefont.not_embedded); Cb.                    Begintext (); Cb.                    Setfontandsize (BF, 14); Cb. Showtextaligned (Pdfcontentbyte.align_center, "page" + P + "of" + ((N/4) + (n% 4 > 0?)                    1:0)), WIDTH/2, 40, 0); Cb.                Endtext (); }//Close document documents.            Close (); } catch (Exception de) {Console.Error.WriteLine (DE.                Message); Console.Error.WriteLine (DE.            StackTrace); }}///<summary>//Create Form///</summary> public void Createfirstta Ble () {string imagePath = AppDomain.CurrentDomain.BaseDirectory + @ "image\1.pm";//Temp File Path St Ring FileName = string.            Empty; Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDiaLog (); Dlg.            FileName = "My first pdf"; Dlg.            DEFAULTEXT = ". pdf"; Dlg.            Filter = "Text documents (. pdf) |*.pdf"; nullable<bool> result = dlg.            ShowDialog (); if (result = = True) {FileName = dlg.                FileName;                Document document = new document ();                PDFWriter writer = pdfwriter.getinstance (document, New FileStream (FileName, FileMode.Create)); Document.                Open ();                pdfptable table = new pdfptable (3);                Pdfpcell cell;                Cell=new Pdfpcell (New Phrase ("Cell with colspan 3")); Cell.                Colspan = 3; Table.                Addcell (cell);                Cell = new Pdfpcell (new Phrase ("Cell with RowSpan 2")); Cell.                Rowspan = 2; Table.                Addcell (cell); Table. Addcell ("Row 1;                Cell 1 "); Table. Addcell ("Row 1;                Cell 2 "); Table. Addcell ("Row 2;                Cell 1 "); Table. Addcell("Row 2;                Cell 2 "); Document.                ADD (table); Document.            Close ();                  }//end if} private void Button1_Click (object sender, RoutedEventArgs e) {//createpdf ();            Createpdfpagesize ();        Createnewpdfpage ();        } private void Button2_Click (object sender, RoutedEventArgs e) {createfirsttable ();        } private void Button3_Click (object sender, RoutedEventArgs e) {ImageDirect ();        } private void Button4_Click (object sender, RoutedEventArgs e) {readpdf (); }       }}

(c) Code download

Code download

(iii) Reference links

Http://www.cnbeta.com/articles/60484.htm a good place to export PDFs online
http://bbs.csdn.net/topics/310095053 PDF Export Discussion
Http://www.cnblogs.com/EKPK/archive/2009/06/04/1495867.html creating PDF files with C # all in all
http://blog.csdn.net/aasswwe/article/details/7639768
http://blog.sina.com.cn/s/blog_82662ce70100t0s6.html PDF Common Usage
Http://www.tuicool.com/articles/nuyAFz HTML Generation PDF (C #)
Http://stackoverflow.com/questions/tagged/itextsharp Itextsharp Related issues
Http://www.itextpdf.com/book/examples.php official documents, though Java versions, are slightly different, some getfunction and setfunction in Java are converted to properties in C #, Can be used as a reference document.

C # generate PDF summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.