我正在使用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