Reputation: 3328
I am using iTextSharp product to change the PDF properties as follows. I am unable to change the "PDF Producer" property at all. Please suggest, where am i getting wrong.
The code line info["Producer"] = "My producer";
is not working as it should be.
string sourcePath = tbPath.Text;
IList<string> dirs = null;
string pdfName = string.Empty;
string OutputPath = string.Empty;
DirectoryInfo di = new DirectoryInfo(sourcePath);
DirectoryInfo dInfo = Directory.CreateDirectory(sourcePath + "\\" + "TempDir");
OutputPath = Path.Combine(sourcePath,"TempDir");
dirs = Directory.GetFiles(di.FullName, "*.pdf").ToList();
for (int i = 0; i <= dirs.Count - 1; i++)
{
try
{
PdfReader pdfReader = new PdfReader(dirs[i]);
using (FileStream fileStream = new FileStream(Path.Combine(OutputPath, Path.GetFileName(dirs[i])),
FileMode.Create,
FileAccess.Write))
{
PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);
Dictionary<string, string> info = pdfReader.Info;
info["Title"] = "";
info["Author"] = "";
info["Producer"] = "My producer"; ////THIS IS NOT WORKING..
pdfStamper.MoreInfo = info;
pdfStamper.Close();
pdfReader.Close();
}
Upvotes: 4
Views: 4925
Reputation: 13
If you are using known producer, you can replace bytes in PDF file. You need producer to be at least length of your Company (or producer replacement text) name. In this example I'm assuming that producer has at least 20 chars. You have to determine that by editing PDF file with text editor. Before using this check licence for the program used to create PDF Here is an example in C#.
// find producer bytes: "producer... " in array and replace
// them with "COMPANY", and after fitth with enough spaces (code: 32)
var textForReplacement = "producer";
var bytesForReplacement = System.Text.Encoding.UTF8.GetBytes(textForReplacement);
var newText = "COMPANY";
var newBytes = System.Text.Encoding.UTF8.GetBytes(newText);
var result = this.Search(pdf, bytesForReplacement);
if (result > -1)
{
var j = 0;
for (var i = result; i < result + 20; i++)
{
// if we have new bytes, then replace them
if (i < result + newBytes.Length)
{
pdf[i] = newBytes[j];
j++;
}
// if not, fill spaces (32)
else
{
pdf[i] = 32;
}
}
}
return pdf;
}
int Search(byte[] src, byte[] pattern)
{
int c = src.Length - pattern.Length + 1;
int j;
for (int i = 0; i < c; i++)
{
if (src[i] != pattern[0]) continue;
for (j = pattern.Length - 1; j >= 1 && src[i + j] == pattern[j]; j--) ;
if (j == 0) return i;
}
return -1;
}
Upvotes: 0
Reputation: 77546
You can only change the producer line if you have a license key. A license key needs to be purchased from iText Software. Instructions on how to apply the license key are sent along with that key.
If you want to use iText for free, you can't change the producer line. See the license header of every file in the open source version of iText:
* In accordance with Section 7(b) of the GNU Affero General Public License,
* a covered work must retain the producer line in every PDF that is created
* or manipulated using iText.
For your info: iText Group has successfully sued a German company that changed the producer line without purchasing a license. You can find some documents related to this case here: IANAL: What developers should know about IP and Legal (slide 57-62)
By the way, I won a JavaOne Rockstar award with this talk: https://twitter.com/itext/status/704278659012681728
Summarized: if you don't have a commercial license for iText, you can not legally change the producer line in iText. If you have a commercial license, you need to apply the license key.
Upvotes: 8