C# iTextSharp.NET PDF-无法更改PDF制作程序
我正在使用iTextSharp产品更改PDF属性,如下所示。 我根本无法更改“PDF Producer”属性。请告诉我,我哪里做错了 代码行 信息[“制作人”]=“我的制作人”强> 没有按应有的方式工作C# iTextSharp.NET PDF-无法更改PDF制作程序,c#,asp.net,itextsharp,C#,Asp.net,Itextsharp,我正在使用iTextSharp产品更改PDF属性,如下所示。 我根本无法更改“PDF Producer”属性。请告诉我,我哪里做错了 代码行 信息[“制作人”]=“我的制作人” 没有按应有的方式工作 string sourcePath = tbPath.Text; IList<string> dirs = null; string pdfName = string.Empty;
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();
}
string sourcePath=tbPath.Text;
IList dirs=null;
string pdfName=string.Empty;
string OutputPath=string.Empty;
DirectoryInfo di=新的DirectoryInfo(sourcePath);
DirectoryInfo dInfo=Directory.CreateDirectory(sourcePath+“\\”+“TempDir”);
OutputPath=Path.Combine(sourcePath,“TempDir”);
dirs=Directory.GetFiles(di.FullName,*.pdf”).ToList();
对于(int i=0;i您只有在拥有许可证密钥的情况下才能更改生产商行。需要从购买许可证密钥。有关如何应用许可证密钥的说明随该密钥一起发送
如果您想免费使用iText,则无法更改生产者行。请参阅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.
供您参考:iText Group已成功起诉一家德国公司,该公司在未购买许可证的情况下更改了生产线。您可以在此处找到一些与本案相关的文件:(幻灯片57-62)
顺便说一下,我通过这次演讲获得了JavaOne Rockstar奖:
概述:如果您没有iText的商业许可证,则无法合法更改iText中的生产者行。如果您有商业许可证,则需要应用许可证密钥。如果您使用的是已知生产者,则可以替换PDF文件中的字节。
您需要至少为公司名称(或生产商替换文本)的长度。
在本例中,我假设producer至少有20个字符。您必须通过使用文本编辑器编辑PDF文件来确定这一点。
在使用用于创建PDF的程序的本检查许可证之前
下面是C#中的一个例子
//在数组中查找生产者字节:“生产者…”并替换
//它们带有“公司”字样,装配后带有足够的空间(代码:32)
var textForReplacement=“生产者”;
var bytesForReplacement=System.Text.Encoding.UTF8.GetBytes(textForReplacement);
var newText=“公司”;
var newBytes=System.Text.Encoding.UTF8.GetBytes(newText);
var result=this.Search(pdf,bytesForReplacement);
如果(结果>-1)
{
var j=0;
对于(变量i=结果;i<结果+20;i++)
{
//如果我们有新的字节,那么替换它们
if(i<结果+新字节长度)
{
pdf[i]=新字节[j];
j++;
}
//如果没有,请填充空格(32)
其他的
{
pdf[i]=32;
}
}
}
返回pdf;
}
int搜索(字节[]src,字节[]模式)
{
intc=src.Length-pattern.Length+1;
int j;
对于(int i=0;i=1&&src[i+j]==pattern[j];j--);
如果(j==0)返回i;
}
返回-1;
}
尝试此信息。添加(“制作人”、“我的制作人”);可能的副本无效。不会改变。iText首席执行官回答此问题不是很好吗?这实际上不符合AGPL v3(尽管德国法院可能已作出裁决)。AGPL如下“要求在该材料或包含该材料的作品所展示的适当法律通知中保留指定的合理法律通知或作者归属”;由该规范生成的PDF不包含该规范,因此要求该规范的文本是无效的“进一步限制”(根据AGPL第10节)。有关更多详细信息,请参阅此讨论:您知道,在这个问题的上下文中,如果op使用您的代码,他很可能会破坏使用库iText的许可证。@mkl Pdf不必使用此特定工具生成”Pdf不必使用这个特定的工具生成”——这是正确的,这就是为什么我只添加了那个评论,没有尝试其他任何东西。
// 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;
}