iText7 for .NET SetAuthor() 使作者值加倍

发布于 2025-01-09 01:02:32 字数 1093 浏览 1 评论 0原文

当使用 iText7 库设置 PDF 文档的属性时,内置属性作者的值会加倍,例如“姓氏、名字”;姓;名'。应该是“姓氏,名字”。它添加了双引号,名称值两次,逗号更改为分号。这在两个版本 7.1.17 和 7.2.1 中都发生过。

创建 PDF 的步骤如下:

  1. 使用 Microsoft.Interop.Word Document.ExportAsFixedFormat() 创建 readerPDF 使用的第一个 PDF。这不会填充自定义文档属性。我需要设置四个自定义属性,供此过程中的后续步骤使用。

  2. 使用 iText7 读取上述 PDF,添加自定义文档属性,还重置内置属性并将其写入通过 writerPDF 访问的第二个文件。 iText7 仅通过读取文件并写入第二个文件来修改 PDF。

在步骤 2 中,代码调用命令来设置作者属性 PdfDocument.PdfDocumentInfo.SetAuthor(authorvalue);

这个问题似乎只发生在作者值中的逗号时,我需要逗号来执行姓氏、名字。这是一个要求。如果我不重置属性 Author is 有双引号,这对我们的项目没有用。所有其他属性(内置属性和自定义属性)均按预期工作。

代码如下所示:

iText.Kernel.Pdf.PdfReader readerPDF;
iText.Kernel.Pdf.PdfWriter writerPDF;
string authorValue = "Lastname, Firstname";

readerPDF = new PdfReader(saveAsPathAndNameTemp);
writerPDF = new PdfWriter(pSavedPathAndPDFName);
PdfDocument pdfdocument = new PdfDocument(readerPDF, writerPDF);
PdfDocumentInfo info = pdfdocument.GetDocumentInfo();

info.SetAuthor(string.Empty);
info.SetAuthor(authorValue);

pdfdocument.Close();
readerPDF.Close();
writerPDF.Close();

When using the iText7 library to set a PDF document's properties the value for the builtin property author is getting doubled like '"Lastname, Firstname"; Lastname; Firstname'. It should be 'Lastname, Firstname'. It is getting the double quotes added, the name value twice and a comma changed to a semicolon. This has happened in two versions, 7.1.17 and 7.2.1.

The steps in creating the PDF are:

  1. Use Microsoft.Interop.Word Document.ExportAsFixedFormat() to create the first PDF used by readerPDF. This does not get the custom document properties to populate. I need to set four custom properties used by a later step in this process.

  2. Use iText7 to read the above PDF, add the custom document properties and also reset the built in properties and write that out to a second file accessed trough writerPDF. iText7 only modifies a PDF by reading from file and writing to a second.

In step 2 the code calls the command to set the author property, PdfDocument.PdfDocumentInfo.SetAuthor(authorvalue);

The problem seems to only happen with commas in the author value, and I need the commas to do Lastname, Firstname. That is a requirement. If I do not reset the property Author is has double quotes around it, that is not useful for our project. All other properties, builtin and custom are working as expected.

The code looks like this:

iText.Kernel.Pdf.PdfReader readerPDF;
iText.Kernel.Pdf.PdfWriter writerPDF;
string authorValue = "Lastname, Firstname";

readerPDF = new PdfReader(saveAsPathAndNameTemp);
writerPDF = new PdfWriter(pSavedPathAndPDFName);
PdfDocument pdfdocument = new PdfDocument(readerPDF, writerPDF);
PdfDocumentInfo info = pdfdocument.GetDocumentInfo();

info.SetAuthor(string.Empty);
info.SetAuthor(authorValue);

pdfdocument.Close();
readerPDF.Close();
writerPDF.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2025-01-16 01:02:32

Adobe Acrobat 阅读器存在问题。我使用下一个代码在 Java 上重现您的问题:

String filename = DESTINATION_FOLDER + "openSimpleDoc.pdf";
String author = "Test, Author";
String title = "Test, Title";
String subject = "Test, Subject";

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename));
pdfDoc.getDocumentInfo().setAuthor(author).setTitle(title).setSubject(subject);
pdfDoc.addNewPage();
pdfDoc.close();

PdfReader reader = new PdfReader(filename);
pdfDoc = new PdfDocument(reader);
Assert.assertEquals(author, pdfDoc.getDocumentInfo().getAuthor());
Assert.assertEquals(title, pdfDoc.getDocumentInfo().getTitle());
Assert.assertEquals(subject, pdfDoc.getDocumentInfo().getSubject());

pdfDoc.close();

如您所见,我没有两次设置作者,当我在 Adob​​e Acrobat 中打开生成的 PDF 时,我看到作者的姓名包含在两个引号中:

Adobe Acrobat

但实际上没有两个引号。您可以在 PDF Studio、RUPS 和 Notepad++ 中查看它:

There is issue in Adobe Acrobat reader. I used the next code to reproduce your issue on Java:

String filename = DESTINATION_FOLDER + "openSimpleDoc.pdf";
String author = "Test, Author";
String title = "Test, Title";
String subject = "Test, Subject";

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename));
pdfDoc.getDocumentInfo().setAuthor(author).setTitle(title).setSubject(subject);
pdfDoc.addNewPage();
pdfDoc.close();

PdfReader reader = new PdfReader(filename);
pdfDoc = new PdfDocument(reader);
Assert.assertEquals(author, pdfDoc.getDocumentInfo().getAuthor());
Assert.assertEquals(title, pdfDoc.getDocumentInfo().getTitle());
Assert.assertEquals(subject, pdfDoc.getDocumentInfo().getSubject());

pdfDoc.close();

As you can see, I didn't set the author twice and when I open the resulting PDF in Adobe Acrobat, I see that the author's name is enclosed in two quotes:

Adobe Acrobat

But in fact there are no two quotes. You can see it in PDF Studio, RUPS and Notepad++:

数理化全能战士 2025-01-16 01:02:32

我解决了主要问题,即如果值中包含逗号,则作者值会重复。我将参考 BouncyCastle.Crypto 更新为 1.9.0.0。这解决了主要问题。 Nikita Kovaliov 在下面解决了属性对话框中双引号的第二个问题。我感谢这张海报的投入。

I resolved the main issue, the duplicating of the Author value if a comma was in the value. I updated the reference BouncyCastle.Crypto to 1.9.0.0. That resolved the main issue. the secondary issue of the double quotes in properties dialog box is address below by Nikita Kovaliov. I thank this poster for there input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文