我正在使用iText庫來創建和添加數據到PDF。iText連續PDF編輯java
我想添加一些textLines和圖像到PDF不止一次,直到我關閉文件。
numOfSamples = timeIHitTheButton();
.
.
.
*a loop tha call it the number of times choosen by numOfSamples*
DSM.saveData();
數據存儲(DSM是一個數據存儲實例)類正確地創建文檔doc.pdf和DSM.addText()和DSM.addPicture()打印正確3個文本行的圖像上的文件,但僅當我只按一次按鈕!
我想每次都寫一個字符串和一個圖像我按下按鈕(如果我按下一次,我有一個樣本,如果trwice我有兩個樣本等)。如果我只是按一下和終止,我可以通過字符串和圖片獲取我的PDF,但如果我按下了一次以上的按鈕,我就會看到一個不可讀的和損壞的PDF文件。我不知道爲什麼。如何繼續編輯圖片和字符串,直至樣本數完成?
在這裏,我後,如果有用的一些代碼(「newPic1.jpg」「newPic2.jpg」等都是存儲的圖片添加到PDF togheter與文本。):
public class DataStore{ ....
.
.
.
public DataStore(String Str1, String Str2, String Str3, int numOfSemples)
throws Exception{
document = new Document();
String1 = str1;
String2 = str2;
String3 = str3;
Samples = numOfSemples;
document.open();
}
privatevoid saveData(){
if(!created){
this.createFile();
created=true;
}
this.addText();
this.addPicture();
}
private void createFile(){
try {
OutputStream file = new FileOutputStream(
new File("Doc.pdf"));
PdfWriter.getInstance(document, file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
private void addText(){
try {
if(Samples > 0)
document.open();
document.add(new Paragraph(Double.toString(String1)));
document.add(new Paragraph(Double.toString(String2)));
document.add(new Paragraph(Double.toString(String3)));
} catch (DocumentException e) {
e.printStackTrace();
}
}
private void addPicture(){
try {
Image img = Image.getInstance("NewPic" + Samples + ".jpg");
document.add(img);
} catch (BadElementException bee) {
bee.printStackTrace();
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (DocumentException dee) {
dee.printStackTrace();
}
if(Samples == 0)
document.close();
else Samples--;
}
}
感謝大家能或者希望幫助:)
目前尚不清楚究竟是如何使用您發佈的代碼片段右側添加一個在
createFile()
。請把它們放在一起。 – mkl同樣在這裏:我不明白這個問題。它需要澄清。 –
@mkl ..我編輯我的問題,希望它更容易理解 – Igr