iTextSharp .NET PDF - 无法更改 PDF Producer

iTextSharp .NET PDF - unable to change PDF Producer

我正在使用 iTextSharp 产品来更改 PDF 属性,如下所示。 我根本无法更改 "PDF Producer" 属性。请建议,我哪里错了。

代码行 信息["Producer"] = "My producer";

没有正常工作。

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

                        }

只有拥有许可证密钥才能更改生产商行。需要从 iText Software 购买许可证密钥。有关如何应用许可证密钥的说明随该密钥一起发送。

如果你想免费使用iText,你不能更改制作者行。查看开源版iText中每个文件的license header:

 * 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.

供您参考:iText Group 已成功起诉一家德国公司,该公司在未购买许可证的情况下更改生产线。您可以在此处找到与此案例相关的一些文档:IANAL: What developers should know about IP and Legal(幻灯片 57-62)

顺便说一句,我通过这次演讲获得了 JavaOne Rockstar 奖:https://twitter.com/itext/status/704278659012681728

总结:如果您没有 iText 的商业许可,您将无法合法地更改 iText 中的生产者行。如果您有商业许可证,则需要申请许可证密钥。

如果您使用的是知名生产商,您可以替换 PDF 文件中的字节。 您需要生产者至少是您公司(或生产者替换文本)名称的长度。 在此示例中,我假设生产者至少有 20 个字符。您必须通过使用文本编辑器编辑 PDF 文件来确定。 在使用此检查用于创建 PDF 的程序许可证之前 这是 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;
    }