I have an existing PDF generated by Telerik Reporting which contains the Application and Producer metadata stating the version of the library. I want these two elements stripped. In the app we use a licensed version of ITextSharp 5.
The Creator (Application) line is properly removed, but the Producer one does not change. During debugging I can see that the code below changes both elements, but when inspecting the resulted PDF the Producer remains.
I have changed the license key to test that it is working and I receive an error leading me to believe that the license is valid.
Throughout Stack Overflow I read that it is only possible to change the Producer line with a valid license. Maybe it refers to PDFs created by ITextSharp and not existing ones?
My code below:
internal static class PdfMetadataStripService
{
internal static byte[] StripMetadata(byte[] pdfBytes)
{
LoadLicenseKey();
PdfReader reader = new PdfReader(pdfBytes);
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
Dictionary<string, string> info = reader.Info;
info["Creator"] = string.Empty;
info["Producer"] = string.Empty;
stamper.MoreInfo = info;
using (var xmpMs = new MemoryStream())
{
var xmp = new XmpWriter(xmpMs, info);
stamper.XmpMetadata = xmpMs.ToArray();
xmp.Close();
}
}
return ms.ToArray();
}
}
private static void LoadLicenseKey()
{
var path = string.Format("{0}itextkey.xml", HostingEnvironment.ApplicationPhysicalPath);
LicenseKey.LoadLicenseFile(path);
}
}
"iTextSharp" version="5.5.10"
"itextsharp.licensekey" version="1.0.4"