繁体   English   中英

iText连续PDF编辑java

[英]iText continuous PDF editing java

我正在使用iText库创建数据并将数据添加到PDF。

我想多次将一些textLines和一个图像添加到PDF中,直到我关闭文件。

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

DataStore(DSM是一个DataStore实例)类正确创建Document doc.pdf,DSM.addText()和DSM.addPicture()正确打印三个文本行和文件上的图像,但仅当我按下按钮一次! !

我想写相同的字符串和图像每次我按下按钮(如果我按下它一次我有一个样本,如果我有两个样本等)。 如果我只是按下它并终止,我会用字符串和图片获取我的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--;
}
}

您以错误的顺序使用iText命令:

  • 您的DataStore构造函数创建一个新的Document并调用其open方法(由于还没有编写器,因此太早了)。
  • 一段时间后,在第一个saveData调用中,调用createFile创建PdfWriter
  • 在所有saveData调用中,调用addText ,对于Samples > 0 ,每次都会再次打开文档(第一次就可以了,但不能多次执行)。
  • 最后,在使用Samples == 0saveData调用中关闭文档。

因此,实质上你这样做:

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

将其与应如何做的进行比较:

// 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();

(从iText in Action中HelloWorld.java样本复制- 第2版

只有对于Samples == 1你才能得到它(构造函数中多余的document.open()被忽略,因为还没有编写者); 但是,对于较大的“ Samples,值,您可以使用存在的编写器多次打开文档,这可能会反复将PDF启动附加到输出流。

您很可能通过删除所有当前的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