iText is a library that allows you to generate PDF files on the fly, and it is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation. iText is not an end-user tool. Typically you won't use it on your Desktop as you would use Acrobat or any other PDF application. Rather, you'll build iText into your own applications so that you can automate the PDF creation and manipulation process.
The iText classes are very useful for people who need to generate read-only, platform independent documents containing text, lists, tables and images; or who want to perform specific manipulations on existing PDF documents.
Say we already have a C# project created with Microsoft Visual Studio, and want to generate a PDF file using iTextSharp. Below is my step:
1. Download itextsharp-5.0.2-dll.zip (1.2MB) from http://sourceforge.net/projects/itextsharp/files/
2. Extract the zip file to get itextsharp.dll file.
3. On Visual Studio, Add Reference to that file.
4. Open your source code and put this
using iTextSharp.text;
using iTextSharp.text.pdf;
5. Create a function like this:
private void generatePDF()
{
Document doc = new Document();
string dirPath = AppDomain.CurrentDomain.BaseDirectory + "pdf";
string pdfFile = dirPath + "\\" + "my.pdf";
PdfWriter writer = PdfWriter.GetInstance(doc, new System.IO.FileStream(pdfFile, System.IO.FileMode.Create));
writer.SetEncryption(PdfWriter.STRENGTH128BITS, "readpassw", "editpassw", PdfWriter.AllowCopy PdfWriter.AllowPrinting);
doc.Open();
doc.AddTitle("iTextSharp");
doc.AddSubject("Create Paragraph");
doc.AddCreator("iTextSharp");
doc.AddAuthor("We");
doc.AddTitle("My Title");
string tit = "This document using 128 bits encryption";
doc.Add(new Paragraph(tit, FontFactory.GetFont(FontFactory.HELVETICA, 20, 5,BaseColor.BLUE)));
Paragraph p = new Paragraph(new Chunk("iText is a library that allows you to generate PDF files on the fly", FontFactory.GetFont(FontFactory.HELVETICA, 10)));
p.Add("The most recent version is iText 5.0.3.");
p.Add("iText is an ideal library for developers looking to enhance web-");
p.Add(new Chunk("and other applications with dynamic PDF document generation and/or manipulation."));
p.Add(new Phrase("iText is not an end-user tool."));
p.Add(new Phrase("You'll build iText into your own applications to automate the PDF creation and manipulation process.", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 18)));
doc.Add(p);
doc.NewPage();
p = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.GetFont(FontFactory.TIMES_ITALIC, 11)));
p.Add("In short: the iText classes are very useful for people who need.");
doc.Add(p);
doc.Close();
}
6. Call that function and chek my.pdf file using your PDF reader.
Tidak ada komentar:
Posting Komentar