gpt4 book ai didi

java - itext 使用 Java 在 pdf 中创建 XMP

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:24:27 26 4
gpt4 key购买 nike

我需要在 java 中(使用 itext)创建以下 xmp 元数据并将其放入我的一个 pdf 中。

<rdf:Description rdf:about="" xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#" xmlns:pdfaType="http://www.aiim.org/pdfa/ns/type#" xmlns:pdfaField="http://www.aiim.org/pdfa/ns/field#"> <pdfaExtension:schemas>
<rdf:Bag>
<rdf:li rdf:parseType="Resource">
<pdfaSchema:schema>ABI Assegni Schema</pdfaSchema:schema> <pdfaSchema:namespaceURI>http://abi.it/std/cheque/xmlns</pdfaSchema:namespaceURI> <pdfaSchema:prefix>assegni</pdfaSchema:prefix>
<pdfaSchema:property>
<rdf:Seq>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>IDDocumento</pdfaProperty:name> <pdfaProperty:valueType>Text</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Identificativo univoco del documento</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>datachiusura</pdfaProperty:name> <pdfaProperty:valueType>Date</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Data e ora della produzione del file</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>oggettodocumento</pdfaProperty:name> <pdfaProperty:valueType>Text</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Oggetto del documento</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>soggettoproduttore</pdfaProperty:name> <pdfaProperty:valueType>soggetto</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Soggetto produttore</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>destinatario</pdfaProperty:name> <pdfaProperty:valueType>soggetto</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Destinatario</pdfaProperty:description>
</rdf:li> </rdf:Seq>

</pdfaSchema:property>
</rdf:li>
</rdf:Bag>
</pdfaExtension:schemas>
</rdf:Description>

到目前为止,我尝试了这部分代码:

PdfReader reader = new PdfReader(baos.toByteArray());
PdfAStamper stamper = new PdfAStamper(reader, baos, PdfAConformanceLevel.PDF_A_1B);

String namespaceExtension = new String("http://www.aiim.org/pdfa/ns/extension/");
String namespaceSchema = new String("http://www.aiim.org/pdfa/ns/schema#");
String namespaceProperty = new String("http://www.aiim.org/pdfa/ns/property#");
String namespaceType = new String("http://www.aiim.org/pdfa/ns/type#");
String namespaceField = new String("http://www.aiim.org/pdfa/ns/field#");
XMPSchemaRegistry registry = XMPMetaFactory.getSchemaRegistry();
registry.registerNamespace(namespaceExtension, "pdfaExtension");
registry.registerNamespace(namespaceSchema, "pdfaSchema");
registry.registerNamespace(namespaceProperty, "pdfaProperty");
registry.registerNamespace(namespaceType, "pdfaType");
registry.registerNamespace(namespaceField, "pdfaField");

XmpWriter w = new XmpWriter(baos);
w.appendArrayItem(namespaceExtension, "schemas", "a");

w.close();

writer.setXmpMetadata(baos.toByteArray());

创建的xmp如下:

<pdfaExtension:schemas>
<rdf:Bag>
<rdf:li>a</rdf:li>
</rdf:Bag>

现在我无法理解如何继续。关于如何执行此操作的任何想法?

提前致谢

最佳答案

我能够用 iText5 来回答这个问题,尽管我认为这个答案有点“hack”,因为它没有使用任何 iText 语义元数据对象,其中大部分似乎是已弃用。请特别注意,xmp.DublinCoreSchemaxmp.PdfSchemaxmp.XmpArrayxmp.XmpSchema 已弃用而 xmp.CustomSchema 不再存在。

iText 文档在这方面非常糟糕。

答案应该可用hereherehere但这些都没有帮助。它们只展示了如何操作信息部分。

一个解决方案可以从线程Adding & retrieve custom properties to PDF using XMP 中导出,但所有使用的 iText 类都已弃用。

最后,我注意到任何 XML 都可以通过stamper.setXmpMetadata(metadata) 插入,其中metadata 是一个 byte[] 包含 XML。这个 XML 可以用 DOM 创建,但在下面的快速和脏中使用一个文件。

package itext.sandpit;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.xmp.XMPException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class ItextSandpit {

public static void main(String[] args) throws DocumentException,
IOException,
XMPException {

// Create PDF
Document document = new Document();
PdfWriter.getInstance(
document, new FileOutputStream("mypdf.pdf"));
document.open();
document.add(new Paragraph("Hello World!"));
document.close();

// Read metadata
File fff = new File("metadata.xml");
FileInputStream fileInputStream = new FileInputStream(fff);
int byteLength = (int) fff.length(); //bytecount of the file-content
byte[] metadatabytes = new byte[byteLength];
fileInputStream.read(metadatabytes, 0, byteLength);

// Add metadata
PdfReader reader = new PdfReader("mypdf.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("mypdf_plus_xmp.pdf"));

stamper.setXmpMetadata(metadatabytes);
stamper.close();
reader.close();
}

}

创建文件 metadata.xml 并将 OP 中的 XML 复制并粘贴到该文件中,然后运行。要确认元数据确实存在于创建的 PDF 中,pdfinfo -meta mypdf_plus_xmp.pdf 产生

Producer:       iText® 5.5.12 ©2000-2017 iText Group NV (AGPL-version); modified using iText® 5.5.12 ©2000-2017 iText Group NV (AGPL-version)
CreationDate: Tue Oct 10 21:01:21 2017
ModDate: Tue Oct 10 21:01:21 2017
Tagged: no
UserProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 1
Encrypted: no
Page size: 595 x 842 pts (A4)
Page rot: 0
File size: 3224 bytes
Optimized: no
PDF version: 1.4
Metadata:
<rdf:Description rdf:about="" xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#" xmlns:pdfaType="http://www.aiim.org/pdfa/ns/type#" xmlns:pdfaField="http://www.aiim.org/pdfa/ns/field#"> <pdfaExtension:schemas>
<rdf:Bag>
<rdf:li rdf:parseType="Resource">
<pdfaSchema:schema>ABI Assegni Schema</pdfaSchema:schema> <pdfaSchema:namespaceURI>http://abi.it/std/cheque/xmlns</pdfaSchema:namespaceURI> <pdfaSchema:prefix>assegni</pdfaSchema:prefix>
<pdfaSchema:property>
<rdf:Seq>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>IDDocumento</pdfaProperty:name> <pdfaProperty:valueType>Text</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Identificativo univoco del documento</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>datachiusura</pdfaProperty:name> <pdfaProperty:valueType>Date</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Data e ora della produzione del file</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>oggettodocumento</pdfaProperty:name> <pdfaProperty:valueType>Text</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Oggetto del documento</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>soggettoproduttore</pdfaProperty:name> <pdfaProperty:valueType>soggetto</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Soggetto produttore</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>destinatario</pdfaProperty:name> <pdfaProperty:valueType>soggetto</pdfaProperty:valueType> <pdfaProperty:category>external</pdfaProperty:category> <pdfaProperty:description>Destinatario</pdfaProperty:description>
</rdf:li> </rdf:Seq>

</pdfaSchema:property>
</rdf:li>
</rdf:Bag>
</pdfaExtension:schemas>
</rdf:Description>

如果可能,使用iText“包装器”,例如 PDFBox或移动到 iText7。

关于java - itext 使用 Java 在 pdf 中创建 XMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38593088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com