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
No comments:
Post a Comment