使用 C# 编辑 PDF 文件的元数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1465434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:49:32  来源:igfitidea点击:

Edit Metadata of PDF File with C#

c#filepdfmetadata

提问by subprime

i searching for methods or libarys to edit metadata of a pdf file like the programm becypdfmetaedit.

我正在搜索方法或库来编辑 pdf 文件的元数据,例如程序becypdfmetaedit

I want to write a program and i need this opton in this program. Perhaps you have some samples for c#.

我想写一个程序,我在这个程序中需要这个选项。也许您有一些 c# 示例。

Thanks

谢谢

采纳答案by crauscher

Using PDF Sharp works like this:

使用 PDF Sharp 的工作方式如下:

using System;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main (string[] args)
    {
      Program p = new Program();
      p.Test();

    }

    public void Test ()
    {
      PdfDocument document = PdfReader.Open ("Test.pdf");

      document.Info.Author = "ME";

      document.Save ("Result");
    }
  }

}

}

回答by crauscher

Aspose.PDFor Aspose.PDF.Kitcan do this for you.

Aspose.PDFAspose.PDF.Kit可以为您做到这一点。

回答by crauscher

Does the PdfDocumentInformation class from PDF Sharpfulfill your requirements.

PDF Sharp的 PdfDocumentInformation 类是否满足您的要求。

回答by Carles Company

I suppose you can do it with iTextSharp.

我想你可以用iTextSharp做到这一点。

回答by plinth

Pimping here - my company, Atalasoft, makes .NET components for working with images. Part of the suite includes the ability to read/write PDF document metadata. It's not free, but it is run-time royalty free for desktop applications.

在这里拉皮条 -我的公司Atalasoft制作用于处理图像的 .NET 组件。该套件的一部分包括读/写 PDF 文档元数据的能力。它不是免费的,但对于桌面应用程序是免版税的。

The code for reading is simple:

阅读代码很简单:

PdfDocumentMetadata metadata = PdfDocumentMetadata.FromStream(sourceStream);

to edit it and write it back to the same stream:

编辑它并将其写回同一个流:

meta.Title = "Knicholas Knickleby";
meta.Author = "Edmund Wells";
sourceStream.Seek(0, SeekOrigin.Begin);
meta.Append(sourceStream, false); // false means don't merge - overwrite

Custom fields are supported through a hashtable.

通过哈希表支持自定义字段。

回答by Bobrovsky

Docotic.Pdf librarycan be used to read and update metadata in PDF documents.

Docotic.Pdf 库可用于读取和更新 PDF 文档中的元数据。

There is PdfDocument.Infoproperty that can be used to change metadata of a PDF document(properties such as "Author", "Title").

有一个PdfDocument.Info属性可用于更改 PDF 文档的元数据(如“作者”、“标题”等属性)。

And there is also PdfDocument.Metadataproperty that is useful if you need to access embedded XMP metadata in a PDF document. The library supports pre-defined XMP schemas and can also be used to set custom application-defined properties.

而且也有PdfDocument.Metadata属性,如果你需要访问嵌入是有用的XMP元数据的PDF文档中。该库支持预定义的 XMP 模式,还可用于设置自定义应用程序定义的属性。

The library is free for non-commercial applications.(The library is no longer free since the 15th of February 2012)

该库对于非商业应用程序是免费的。(图书馆自 2012 年 2 月 15 日起不再免费)

Disclaimer: I work for the company.

免责声明:我为公司工作。

回答by Hugo Geijteman

For PDFSharp: If you would like to change/add the metadata on the Custom Propertiesof a PDF you can use the PdfDocument.Info.Elements object.

对于 PDFSharp:如果您想更改/添加PDF自定义属性上的元数据,您可以使用 PdfDocument.Info.Elements 对象。

    String filename = @"d:\temp\Hugo-input.pdf";
    String outputfile = @"d:\temp\Hugo-output.pdf";
    PdfDocument document = PdfReader.Open(filename);
    document.Info.Elements.Add(new KeyValuePair<String,PdfItem>("/MyKey",new PdfString("MyValue")));
    document.Save(outputfile);

Always start a custom key with a slash!

始终以斜线开头自定义键!

You can find the key and value when you open this document in Adobe Acrobat Reader -> File -> Properties -> Custom.

您可以在 Adob​​e Acrobat Reader -> 文件 -> 属性 -> 自定义中打开此文档时找到键和值。

This works with PDFSharp 1.32

这适用于 PDFSharp 1.32