我正在开发一个应用程序,并使用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();
我怎样才能在C#中做同样的事情?
答案 0 :(得分:12)
从Java到C#的转换通常非常简单。按照惯例,Java属性使用get
和set
前缀,因此要转换为C#,您只需要删除前缀并将其转换为.Net getter / setter调用。 getInfo()
变为Info
,setMoreInfo(info)
变为MoreInfo = info
。然后,您只需将本机Java类型转换为其等效的C#类型。在这种情况下,Java FileOutputStream
变为。FileStream
,HashMap<String, String>
变为Dictionary<String, String>
。
最后,我更新了代码以反映iTextSharp的最新更改,现在(从5.1.1.0开始)实现IDisposable
。
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();
}
}
}
答案 1 :(得分:7)
我在PdfWriter对象的监视窗口中搜索正确的位置后创建了这个,它改变了PDF中的“PDF Creator”,因为默认情况下无法访问它:
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"));
}
我认为它只能通过反思来实现,但我很欣赏受过更多教育的人的合作:)
THX!
答案 2 :(得分:0)
public void pdfproperties()
{
string inputFile = @"D:\1.pdf";
string outputFile = @"D:\48.pdf";
PdfReader reader = new PdfReader(inputFile);
foreach (KeyValuePair<string, string> KV in reader.Info)
{
reader.Info.Remove(KV.Key);
}
using (FileStream FS = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (Document Doc = new Document())
{
using (PdfCopy writer = new PdfCopy(Doc, FS))
{
Doc.Open();
Doc.AddTitle("Add Title");
Doc.AddSubject("Add Subject");
Doc.AddKeywords("Add Keywords");
Doc.AddCreator("Application Creator");
Doc.AddAuthor("Add Author");
for (int i = 1; i <= reader.NumberOfPages; i++)
{
writer.AddPage(writer.GetImportedPage(reader, i));
}
writer.Info.Put(new PdfName("Producer"), new PdfString("Producer Name"));
Doc.Close();
}
}
}
}
&#13;