Showing posts with label iTextSharp. Show all posts
Showing posts with label iTextSharp. Show all posts

Wednesday, December 8, 2010

Including Metadata When Creating a PDF File Using iTextSharp

This post is a continuation of the previous introduction to iTextSharp see http://wardlawclaims.blogspot.com/2010/10/introduction-to-pdf-file-creation-in-c.html.  In this post I will demonstrate adding metadata to a PDF created using iTextSharp.  Metadata is data included in a PDF file which is about the PDF file rather than part of its contents.  Metadata might include the author and title of the document.  Metadata could be used by an indexing service to locate files based on their metadata; for example by author.

Here is a screenshot of the sample application I will include for download at the end of this post.  I've added the ability to input the metadata for author, title, subject, and keywords.



The C# source code for the application has been modified to include calls to the appropriate methods that will add the metadata to the PDF.  Be aware that the metadata methods needs to called after the PdfWriter.GetInstance method is called.  If the metadata methods are called before GetInstance no exceptions will be thrown, but the metadata will not be included in the file.   The metadata methods must be called before the Close method is called on the Document object or else an exception will be thrown.

Here is the code for adding the metadata.  A link to download the source code for the application is included at the bottom of this post.


//add metadata
//metadata must be added after GetInstance is called
if (!string.IsNullOrEmpty(this.authorText.Text))
{
    pdfDocument.AddAuthor(this.authorText.Text);
}
if(!string.IsNullOrEmpty(this.subjectText.Text))
{
pdfDocument.AddSubject(this.subjectText.Text);
}

if (!string.IsNullOrEmpty(this.keywordsText.Text))
{
    //note that keywords should be separated by commas
    pdfDocument.AddKeywords(this.keywordsText.Text);
}

if (!string.IsNullOrEmpty(this.titleText.Text))
{
    pdfDocument.AddTitle(this.titleText.Text);
}

As you can see there are methods specifically for standard metadata such as author, subject, title, and keywords.

References
The API documentation can be found at the link below.  Note that the documentation is for the Java version, but is useful to programmers using iTextSharp for .NET as well since the .NET library's API is based on the Java version.
http://api.itextpdf.com/ 

You can download the Visual Studio project  file for the example at: 
http://www.wardlawclaims.com/attachments/blog/jeremy/including-metadata-when-creating-a-pdf-file-using-itextsharp/HelloWorldWithMetadataPdfDemo.zip 

Wednesday, October 20, 2010

Introduction to PDF File Creation in C# Using iTextSharp

There are numerous commercial and open source components that enable .NET developers to create PDF files.  I will be providing a brief introduction to using iTextSharp to create a PDF file using C#.  iTextSharp is an open source .NET library based on the iText Java library.  This post will provide some sample code using iTextSharp to create a PDF containing text entered using a Windows forms application.

Here is a screenshot of the user interface.


















For the purpose of this sample I have added all of the program's logic to the "Create PDF" button.  Here is a walkthrough of the relevant code. You must download the iTextSharp DLL or compile it from the source code and add a reference before you will be able to use the iTextSharp namespace in your project.

Here is the C# code that does the work of creating a basic PDF file.  A complete Visual Studio solution containing a Windows Forms application is available to download as well.  The link is at the end of this post.



//name of file to create, since directory is not specified it will be created in the same directory as the program executable file
string fileName = "hello.pdf";

//create file object
System.IO.FileStream destinationFile = new System.IO.FileStream(fileName, System.IO.FileMode.Create);

//create a new pdf document
iTextSharp.text.Document pdfDocument = new iTextSharp.text.Document();

//associate document with file object
iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDocument, destinationFile);

//must call open method before writing file content
pdfDocument.Open();

//loop through the lines input on the form
foreach (string source in this.sourceText.Lines)
{
    //create a block of text with the content of the line followed by a newline 
    iTextSharp.text.Phrase lineOfText = new iTextSharp.text.Phrase(source + "\n");

    //add block of text to PDF document
    pdfDocument.Add(lineOfText);
}

//close document object when we're done adding content
pdfDocument.Close();

//open pdf file in default PDF application
System.Diagnostics.Process.Start(fileName);


As you can see the code required to produce a basic PDF file is not too complicated.  

References

The iTextSharp library can be found at SourceForge:

The API documentation can be found at the link below.  Note that the documentation is for the Java version, but is useful to programmers using iTextSharp for .NET as well since the .NET library's API is based on the Java version.

The author of iText has published a book on the subject.  The book is based on the Java version of the library. 

You can download the Visual Studio project  file for the example at: