4

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();                                                        

                        }
Karan
  • 3,265
  • 9
  • 54
  • 82

2 Answers2

8

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

enter image description here

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 3
    Isn't it great that the iText CEO answered this question – rajeemcariazo Apr 01 '16 at 08:52
  • This is not actually in accordance with the AGPL v3 (despite what a German court may have ruled). The AGPL reads "Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed *by works containing it;*" A PDF generated by the code *does not contain the code* and so iText requiring it is an invalid "further restriction" (under section 10 of the AGPL). See this discussion for more details: https://lists.fedoraproject.org/pipermail/legal/2011-June/001656.html – ChickenOverlord May 14 '21 at 19:31
0

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;
    }
  • You are aware that in the context of this question it is very likely (see the accepted answer) that if the op used your code, he would break the license under which he uses the library iText. – mkl Oct 22 '20 at 11:05
  • @mkl Pdf does not have to be generated with this particular tool – Duke Nukem Oct 22 '20 at 11:37
  • *"Pdf does not have to be generated with this particular tool"* - That's correct, and that's why I only added that comment and did not attempt anything else. – mkl Oct 22 '20 at 18:25