How to add standard and custom Metadata to PDF using iTextSharp
In a previous blog, I explained How to add custom metadata using Syncfusion library. But this package gives so many difficulties when adding metadata to pdf. Such problems are,
1. When once add custom meta data to a pdf, it does not able to change or remove those custom meta data from the pdf.
2. It does not compatible with meta data search engines. It does not show custom meta data properly when those are searched through the search engines.
Due to these reasons, Syncfusion is not good library for custom meta data. But, iTextSharp provides flexible framework to deal with custom meta data as well as standard meta data for PDF. Below I have provided the source code to show usage of iTextSharp library.
You can download iTextShrp.dll from here
PdfReader pdf = new PdfReader(input_path);
PdfStamper st = new PdfStamper(pdf, new FileStream(output_path, FileMode.Create, FileAccess.Write));
Hashtable info = new Hashtable();
//Adding Standard Metadata
info.Add("Keywords", "Test,);
info.Add("Subject", metaInfo.Subject);
info.Add("Title", metaInfo.Name);
//Adding Custom Metadata
info.Add("Email", "nadee@gmail.com");
info.Add("ContactNo", "01128445678");
info.Add("Position", "SE");
info.Add("Department", "IT");
st.MoreInfo = info;
MemoryStream os = new System.IO.MemoryStream();
XmpWriter xmp = new XmpWriter(os, info);
xmp.Close();
st.XmpMetadata = getStreamedMetaData(os.ToArray());
st.Close();
//Getting Metadata from PDF
System.Collections.IEnumerator en= reader.Info.GetEnumerator();
while (en.MoveNext()) {
Type type = en.Current.GetType();
string strValue = type.InvokeMember("Value",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.GetProperty,
null, en.Current,
new object[] { }).ToString();
Console.WriteLine("Meta Data :" + strValue);
}
1. When once add custom meta data to a pdf, it does not able to change or remove those custom meta data from the pdf.
2. It does not compatible with meta data search engines. It does not show custom meta data properly when those are searched through the search engines.
Due to these reasons, Syncfusion is not good library for custom meta data. But, iTextSharp provides flexible framework to deal with custom meta data as well as standard meta data for PDF. Below I have provided the source code to show usage of iTextSharp library.
You can download iTextShrp.dll from here
PdfReader pdf = new PdfReader(input_path);
PdfStamper st = new PdfStamper(pdf, new FileStream(output_path, FileMode.Create, FileAccess.Write));
Hashtable info = new Hashtable();
//Adding Standard Metadata
info.Add("Keywords", "Test,);
info.Add("Subject", metaInfo.Subject);
info.Add("Title", metaInfo.Name);
//Adding Custom Metadata
info.Add("Email", "nadee@gmail.com");
info.Add("ContactNo", "01128445678");
info.Add("Position", "SE");
info.Add("Department", "IT");
st.MoreInfo = info;
MemoryStream os = new System.IO.MemoryStream();
XmpWriter xmp = new XmpWriter(os, info);
xmp.Close();
st.XmpMetadata = getStreamedMetaData(os.ToArray());
st.Close();
//Getting Metadata from PDF
System.Collections.IEnumerator en= reader.Info.GetEnumerator();
while (en.MoveNext()) {
Type type = en.Current.GetType();
string strValue = type.InvokeMember("Value",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.GetProperty,
null, en.Current,
new object[] { }).ToString();
Console.WriteLine("Meta Data :" + strValue);
}
how do i edit metadata of pdf file using itextsharp library plz help!!!!!!!!
ReplyDeleteThis comment has been removed by the author.
ReplyDeletei'm trying to do, but the "os" object always is empty :(
ReplyDeleteI use Dictionary(of String,of string) to interact with the info object instead of Hashtable.
MemoryStream os = new System.IO.MemoryStream();
XmpWriter xmp = new XmpWriter(os, info);
xmp.Close();
Can you help me?
I need to add some custom properties to PDF and PDF/A files.
Thank you very much
You can download and try this interesting high quality software on rasteredge page for c# accessible links in pdf http://www.rasteredge.com/how-to/csharp-imaging/pdf-edit-bookmark-outline/
DeleteYou can download and try this interesting high quality software on rasteredge page for c# accessible links in pdf http://www.rasteredge.com/how-to/csharp-imaging/pdf-document-metadata/
ReplyDeleteYou can download and try this interesting high quality software on rasteredge page for c# acrobat pdf additional metadata on http://www.rasteredge.com/how-to/csharp-imaging/pdf-document-metadata/
ReplyDelete