使用iTextSharp读取/修改PDF元数据,而不向pdf属性的用户显示任何数据 [英] Read/Modify PDF Metadata using iTextSharp without showing any data to user on pdf properties
问题描述
我正在尝试使用iTextSharp来读取/修改PDF元数据。不向用户显示任何信息。我通过以下代码:
I am trying to use iTextSharp to read/modify PDF metadata. Without showing any information to the user. I gone through below code:
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 20f, 20f, 20f, 20f);
using (MemoryStream memStream = new MemoryStream())
{
using (PdfWriter wri = PdfWriter.GetInstance(document, memStream))
{
document.Open(); document.AddSubject("Test"); document.Close();
}
}
我非常感谢任何有关解决方案的指示。
I would greatly appreciate any pointers to the solution.
推荐答案
您使用的代码片段是关于向从头创建的PDF文档添加元数据。您要求从现有 PDF文档中读取元数据。请查看iText网站上的文档,更具体地说是Q& A条目如何使用自定义属性从PDF添加/删除/检索信息?
The code snippet you are using is about adding metadata to a PDF document that is created from scratch. You are asking to read metadata from an existing PDF document. Please take a look at the documentation on the iText website, more specifically to Q&A entry How to add / delete / retrieve information from a PDF using a custom property?
当然,使用的代码在这个问题的答案是用Java编写的,但将它移植到C#相当容易。这是另一个片段:
Granted, the code used in the answer to that question is written in Java, but it's fairly easy to port it to C#. Here's another snippet:
public byte[] ManipulatePdf(byte[] src) {
PdfReader reader = new PdfReader(src);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
Dictionary<String, String> info = reader.Info;
info["Title"] = "Hello World stamped";
info["Subject"] = "Hello World with changed metadata";
info["Keywords"] = "iText in Action, PdfStamper";
info["Creator"] = "Silly standalone example";
info["Author"] = "Also Bruno Lowagie";
stamper.MoreInfo = info;
}
return ms.ToArray();
}
}
您可以通过<从现有PDF检索元数据code> PdfReader 。你得到一个字典
,其中的键与ISO-32000-1中信息字典的键对应。如果要更改元数据,可以使用 PdfStamper
进行更改。
You can retrieve the metadata from an existing PDF through PdfReader
. You get a Dictionary
where the keys correspond with the keys of the info dictionary in ISO-32000-1. If you want to change the metadata, you can do so with PdfStamper
.
此功能将读取并调整信息字典(这是您在代码段中引用的内容)。 PDF还可以包含XMP元数据。
This functionality will read and adapt the Info dictionary (which is what you referred to in your code snippet). The PDF can also contains XMP metadata.
XMP元数据可以这样读取:
XMP metadata can be read like this:
public string ReadXmpMetadata(byte[] src) {
PdfReader reader = new PdfReader(src);
byte[] b = reader.Metadata;
return Encoding.UTF8.GetString(b, 0, b.Length);
}
您可以像这样更改XMP元数据:
You can change the XMP metadata like this:
public byte[] ManipulatePdf(byte[] src) {
PdfReader reader = new PdfReader(src);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
Dictionary<String, String> info = reader.Info;
using (MemoryStream msXmp = new MemoryStream()) {
XmpWriter xmp = new XmpWriter(msXmp, info);
xmp.Close();
stamper.XmpMetadata = msXmp.ToArray();
}
}
return ms.ToArray();
}
}
我假设您知道信息之间的区别字典和PDF格式的XMP元数据流。
I am assuming that you know the difference between the Info dictionary and an XMP metadata stream in PDF.
这篇关于使用iTextSharp读取/修改PDF元数据,而不向pdf属性的用户显示任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!