简体   繁体   English

iText连续PDF编辑java

[英]iText continuous PDF editing java

I'm using iText library to create and add data to a PDF. 我正在使用iText库创建数据并将数据添加到PDF。

I want to add some textLines and an image to the PDF more than once until i close the file. 我想多次将一些textLines和一个图像添加到PDF中,直到我关闭文件。

 numOfSamples = timeIHitTheButton();
.
.
.
 *a loop tha call it the number of times choosen by numOfSamples*
 DSM.saveData();

The DataStore (DSM is a DataStore instance) class creates the Document doc.pdf correctly and DSM.addText() and DSM.addPicture() prints correctly three textlines an an image on the file, but only if I press the button just once !! DataStore(DSM是一个DataStore实例)类正确创建Document doc.pdf,DSM.addText()和DSM.addPicture()正确打印三个文本行和文件上的图像,但仅当我按下按钮一次! !

I WANT TO WRITE THE SAME STRING AND AN IMAGE EVERY TIME I PRESS THE BUTTON (if I press it once i have one sample, if trwice i have two samples etc). 我想写相同的字符串和图像每次我按下按钮(如果我按下它一次我有一个样本,如果我有两个样本等)。 IF I PRESS IT JUST ONCE AND I TERMINATE, I GET MY PDF WITH THE STRING AND THE PICTURES, BUT IF I PRESS IT MORE THAN ONCE, I GOT AN UNREADABLE AND DAMAGED PDF FILE. 如果我只是按下它并终止,我会用字符串和图片获取我的PDF,但如果我按下它的话,我会得到一个难以置信和损坏的PDF文件。 I DON'T KNOW WHY. 我不知道为什么。 HOW CAN I CONTINUE WRITIN A PICTURE AND THE STRING CONTINUOSLY UNTIL THE NUMBER OF SAMPLES IS FINISHED? 在完成样本数量之后,我如何继续写图片和字符串连续写入?

Here i post some code if useful ("newPic1.jpg" "newPic2.jpg" etc are the stored pictures to add to the PDF togheter with the text.): 在这里,我发布一些代码,如果有用(“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--;
}
}

You use iText commands in the wrong order: 您以错误的顺序使用iText命令:

  • Your DataStore constructor creates a new Document and calls its open method (which is too early as there is no writer yet). 您的DataStore构造函数创建一个新的Document并调用其open方法(由于还没有编写器,因此太早了)。
  • Some time later, in the first saveData call, you call createFile which creates the PdfWriter . 一段时间后,在第一个saveData调用中,调用createFile创建PdfWriter
  • In all saveData calls addText is called which for Samples > 0 opens the document again each time (which is ok at the first time but shall not be done multiple times). 在所有saveData调用中,调用addText ,对于Samples > 0 ,每次都会再次打开文档(第一次就可以了,但不能多次执行)。
  • Eventually, in the saveData call with Samples == 0 you close the document. 最后,在使用Samples == 0saveData调用中关闭文档。

Thus, in essence you do this: 因此,实质上你这样做:

document = new Document();
document.open();
[...]
PdfWriter.getInstance(document, file);
[...]
[for `Samples` times]
    document.open();
    [add some paragraphs]
    [add an image]
[end for]
document.close();

Compare this to how it should be done: 将其与应如何做的进行比较:

// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
[add content to the PDF]
// step 5
document.close();

(copied from the HelloWorld.java sample from iText in Action — 2nd Edition ) (从iText in Action中HelloWorld.java样本复制- 第2版

Only for Samples == 1 you have it about right (the superfluous document.open() in the constructor being ignored as there is no writer yet); 只有对于Samples == 1你才能得到它(构造函数中多余的document.open()被忽略,因为还没有编写者); for larger values of Samples, though, you open the document multiple times with a writer present which will likely append a PDF start over and over again to the output stream. 但是,对于较大的“ Samples,值,您可以使用存在的编写器多次打开文档,这可能会反复将PDF启动附加到输出流。

Quite likely you can fix the issue by removing all your current document.open() calls (including the if(Samples > 0) in addText() ) and add one in createFile() right after PdfWriter.getInstance(document, file). 您很可能通过删除所有当前的document.open()调用(包括addText()if(Samples > 0) addText() )并在PdfWriter.getInstance(document, file).之后的createFile()添加一个来解决问题PdfWriter.getInstance(document, file).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM