2017-08-15 59 views
0

我想使用JPod庫將圖像添加到生成的pdf中。如何使用JPod在PDF中嵌入圖像?

的JPOD DrawInLineImagePlain示例(在https://github.com/born2snipe/learning-jpod/blob/master/not-in-repo/examples/de/intarsys/pdf/example/content/DrawInlineImagePlain.java)建議添加圖像像這樣(假定一個CSCreator創建者預先定義):

// image bytes 
private byte[] data = new byte[] { (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170 }; 

creator.transform(400, 0, 0, 400, 100, 100); 
creator.perform("BI"); 
creator.perform("/W 16 /H 16 /BPC 1 /CS /G ID"); 
COSString stringData = COSString.create(data); 
creator.perform("EI", stringData); 

creator.close(); 

然而,我無法做到這一點,因爲JPOD不再支持執行()。

看來,inlineImage()應該能夠嵌入圖像。 JPod文檔描述如下的inlineImage():

描邊內嵌圖像。 PDF圖形操作符「BI」,「ID」,「EI」。

這使我假定inlineImage()可以編碼所有的圖形運營商在DrawInLineImagePlain示例,並且還可以編碼寬度,高度,位每成分,色彩空間,並且將圖像數據使用PDImage參數中的信息。

所以,我試圖平行的例子,像這樣(再次假設一個CSCreator創建者預先定義):

// image bytes 
byte[] data = new byte[] { (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170, 
    (byte) 170, (byte) 170, (byte) 170, (byte) 170 }; 

PDImage imageData = (PDImage) PDImage.META.createNew(); 
imageData.setBytes(data); 
imageData.setHeight(16); 
imageData.setWidth(16); 
imageData.setBitsPerComponent(1); 
imageData.setColorSpace(PDColorSpace.getNamed(PDColorSpace.CN_CS_DeviceGray)); 

creator.inlineImage(imageData); 

creator.close(); 

此,不幸的是產生空白PDF。我需要更改什麼才能讓圖像顯示在PDF中?

我在猜測文檔或頁面設置沒有問題,因爲我可以用這個圖像嵌入替代文本或線條圖,文本或線條在PDF中顯示得很好。

回答

0

inlineImage不幸在CSCreator中實現。使用addOperand直接將內容添加到內容流中。您的圖像的代碼是:

content.addOperation(new CSOperation(CSOperators.CSO_BI)); 
CSOperation operationID = new CSOperation(CSOperators.CSO_ID); 
operationID.addOperand(PDImage.DK_W); 
operationID.addOperand(COSInteger.create(16)); 
operationID.addOperand(PDImage.DK_H); 
operationID.addOperand(COSInteger.create(16)); 
operationID.addOperand(PDImage.DK_BPC); 
operationID.addOperand(COSInteger.create(1)); 
operationID.addOperand(PDImage.DK_CS); 
operationID.addOperand(PDColorSpace.CN_CS_G); 
content.addOperation(operationID); 
CSOperation operationEI = new CSOperation(CSOperators.CSO_EI); 
operationEI.addOperand(COSString.create(data)); 
content.addOperation(operationEI); 

常規圖像或者添加到您的文檔與doImage