在iTextSharp中设置元数据 [英] Set Metadata in iTextSharp
问题描述
我正在开发一个应用程序,我正在使用iTextSharp库。
I am developing an application and i use the iTextSharp library.
我也在阅读Manning的iText,以便我可以获得参考。
I am also reading the iText in action from Manning so i can get references.
在第12章中,它有以下代码来更改Java中的元数据。
In Chapter 12 it has the following code to change the metadata in 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#中做同样的事情?
How can i do the same in C#?
推荐答案
从Java到C#的转换通常非常简单。按照惯例,Java属性使用 get
和设置
前缀,以便转换为C#,您只需要删除前缀并转向它进入.Net getter / setter调用。 getInfo()
变为信息
和 setMoreInfo(info)
变为 MoreInfo = info
。然后,您只需将本机Java类型转换为其等效的C#类型。在这种情况下,Java FileOutputStream
变为.Net FileStream
和 HashMap< String,String> ;
成为字典<字符串,字符串>
。
Conversion from Java to C# is usually pretty straightforward. By convention, Java properties use get
and set
prefixes so to convert to C# you just need to drop the prefix and turn it into a .Net getter/setter call. getInfo()
becomes Info
and setMoreInfo(info)
becomes MoreInfo = info
. Then you just need to convert the native Java types to their equivalent C# types. In this case the Java FileOutputStream
becomes a .Net FileStream
and the HashMap<String, String>
becomes a Dictionary<String, String>
.
最后,我是更新了代码以反映最近对iTextSharp的更改,现在(从5.1.1.0开始)实现 IDisposable
。
Lastly, I've updated the code to reflect recent changes to iTextSharp that now (as of 5.1.1.0) implement 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();
}
}
}
这篇关于在iTextSharp中设置元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!