2016-07-26 248 views
6

我需要在java(使用itext)中創建以下xmp元數據並將其放入我的pdf中。Itext使用Java創建PDF格式的XMP

<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> 

現在,我不能就如何去理解。任何想法如何做到這一點?

在此先感謝

+1

你試過了什麼?您使用的是哪種版本的iText?從某個5.x.y版本(我沒有檢查哪一個版本)開始,iText使用Adobe提供的庫來創建XMP。對於iText 7,API完全改變了,所以請不要期望沒有共享您正在使用的iText版本的答案。 –

+0

我試過這個: XmpWriter w = new XmpWriter(baos); \t \t \t w.appendArrayItem(namespaceExtension,「schemas」,「a」); \t \t \t \t \t \t w.close(); \t \t \t \t \t \t writer.setXmpMetadata(baos。toByteArray()); 創建此: 一個 如何去不知道。使用itext 5.5.8 – Giamma

+0

@Giamma請在問題中添加這樣的說明(其下有一個[編輯](http://stackoverflow.com/posts/38593088/edit)鏈接);評論中的代碼很難閱讀。 – mkl

回答

3

我能夠回答這個問題與iText5措辭雖然我認爲這個答案是一個有點「黑客」,在某種意義上說,它並沒有使用任何iText的語義元數據的對象,其中大部分似乎已被棄用。請注意,xmp.DublinCoreSchema,xmp.PdfSchema,xmp.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。

+1

PDFBox不是iText包裝,它是不同的產品。 –

+0

@TilmanHausherr謝謝。我需要驗證爲什麼我的構建依賴於iText,而使用PDFBox,罪魁禍首是*不* PDFBox。 – fundagain