Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在iTextSharp中设置元数据_C#_Itextsharp - Fatal编程技术网

C# 在iTextSharp中设置元数据

C# 在iTextSharp中设置元数据,c#,itextsharp,C#,Itextsharp,我正在开发一个应用程序,我使用iTextSharp库 我也在阅读曼宁的iText,以便获得参考资料 在第12章中,它有以下代码来更改Java中的元数据 PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); HashMap<String, String> info = reader.getInfo(); info.pu

我正在开发一个应用程序,我使用iTextSharp库

我也在阅读曼宁的iText,以便获得参考资料

在第12章中,它有以下代码来更改Java中的元数据

PdfReader reader = new PdfReader(src);
PdfStamper stamper =
new PdfStamper(reader, new FileOutputStream(dest));
HashMap<String, String> info = reader.getInfo();
info.put("Title", "Hello World stamped");
info.put("Subject", "Hello World with changed metadata");
info.put("Keywords", "iText in Action, PdfStamper");
info.put("Creator", "Silly standalone example");
info.put("Author", "Also Bruno Lowagie");
stamper.setMoreInfo(info);
stamper.close();
PdfReader阅读器=新的PdfReader(src);
PdfStamper压模=
新的PdfStamper(读卡器,新的FileOutputStream(dest));
HashMap info=reader.getInfo();
信息输入(“标题”、“Hello World盖章”);
info.put(“主题”,“元数据已更改的Hello World”);
info.put(“关键字”,“iText正在运行,PdfStamper”);
info.put(“创建者”、“愚蠢的独立示例”);
info.put(“作者”,“也是布鲁诺·洛瓦吉”);
母版。setMoreInfo(信息);
压模关闭();
如何在C#中实现同样的功能?

从Java到C#的转换通常非常简单。按照惯例,Java属性使用
get
set
前缀,因此要转换为C,只需删除前缀并将其转换为.Net getter/setter调用
getInfo()
变成
Info
setMoreInfo(Info)
变成
MoreInfo=Info
。然后,您只需要将本机Java类型转换为等效的C#类型。在这种情况下,Java
FileOutputStream
成为.Net
FileStream
HashMap
成为
字典

最后,我更新了代码,以反映iTextSharp最近的更改,现在(从5.1.1.0开始)实现了
IDisposable
now

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string inputFile = Path.Combine(workingFolder, "Input.pdf");
            string outputFile = Path.Combine(workingFolder, "Output.pdf");

            PdfReader reader = new PdfReader(inputFile);
            using(FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)){
                using (PdfStamper stamper = new PdfStamper(reader, fs))
                {
                    Dictionary<String, String> info = reader.Info;
                    info.Add("Title", "Hello World stamped");
                    info.Add("Subject", "Hello World with changed metadata");
                    info.Add("Keywords", "iText in Action, PdfStamper");
                    info.Add("Creator", "Silly standalone example");
                    info.Add("Author", "Also Bruno Lowagie");
                    stamper.MoreInfo = info;
                    stamper.Close();
                }
            }

            this.Close();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Windows.Forms;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
string workingFolder=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
字符串inputFile=Path.Combine(workingFolder,“Input.pdf”);
字符串outputFile=Path.Combine(workingFolder,“Output.pdf”);
PdfReader reader=新PdfReader(输入文件);
使用(FileStream fs=newfilestream(outputFile,FileMode.Create,FileAccess.Write,FileShare.None)){
使用(PdfStamper压模=新PdfStamper(读卡器,fs))
{
字典信息=reader.info;
信息添加(“标题”、“Hello World盖章”);
添加(“主题”,“元数据已更改的Hello World”);
信息添加(“关键字”、“正在运行的iText、PdfStamper”);
信息添加(“创建者”、“愚蠢的独立示例”);
信息添加(“作者”,“也包括布鲁诺·洛瓦吉”);
stamper.MoreInfo=info;
压模关闭();
}
}
这个。关闭();
}
}
}

我在PdfWriter对象的监视窗口中搜索了正确的位置后,才创建了此文件,它更改了PDF中的“PDF创建者”,因为默认情况下无法访问:

private static void ReplacePDFCreator(PdfWriter writer)
    {
        Type writerType = writer.GetType();
        PropertyInfo writerProperty = writerType.GetProperties(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).Where(p => p.PropertyType == typeof(PdfDocument)).FirstOrDefault();

        PdfDocument pd =  (PdfDocument)writerProperty.GetValue(writer);
        Type pdType = pd.GetType();
        FieldInfo infoProperty = pdType.GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).Where(p => p.Name == "info").FirstOrDefault();
        PdfDocument.PdfInfo pdfInfo = (PdfDocument.PdfInfo)infoProperty.GetValue(pd);

        PdfString str = new PdfString("YOUR NEW PDF CREATOR HERE");
        pdfInfo.Remove(new PdfName("Producer"));
        pdfInfo.Put(new PdfName("Producer"), str);
    }
我从“@yannic donot text”那里得到了一个建议,它更干净了

private static void ReplacePDFCreator(PdfWriter writer)
    {
       writer.Info.Put(new PdfName("Producer"), new PdfString("YOUR NEW PDF CREATOR HERE"));
    }
我认为这只能通过思考来理解,但我欣赏受过更多教育的人的合作:)

谢谢

public void pdfproperty()
{
字符串inputFile=@“D:\1.pdf”;
字符串outputFile=@“D:\48.pdf”;
PdfReader reader=新PdfReader(输入文件);
foreach(reader.Info中的KeyValuePair)
{
读卡器信息移除(KV键);
}
使用(FileStream FS=newfilestream(outputFile,FileMode.Create,FileAccess.Write,FileShare.None))
{
使用(文档文档=新文档())
{
使用(PdfCopy编写器=新的PdfCopy(文档,FS))
{
Doc.Open();
文件AddTitle(“添加标题”);
添加主题文件(“添加主题”);
文档添加关键字(“添加关键字”);
Doc.AddCreator(“应用程序创建者”);
添加作者文件(“添加作者”);

对于(int i=1;就是这样。非常感谢您的回答:)我正在尝试用您的反射方法替换PDF生成器。它对我不起作用。它对您起作用了吗。我不再有那个源代码,但在一个类似的项目中,我下载了PdfSharp的源代码并编辑了“override void PrepareForSave()”,就像:if(info.Elements[“/Creator”]==null)info.Creator=“此处的文本”;string str=info.Producer;if(str.Length==0)str=“此处的文本”;else if(!str.StartsWith(“此处的文本”))str=“PDFsharp 1.32.2608-g(www.PDFsharp.net)(原文:“+str+”);