2013-05-27 77 views
1

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

感谢大家能或者希望帮助:)

+1

目前尚不清楚究竟是如何使用您发布的代码片段右侧添加一个在createFile() 。请把它们放在一起。 – mkl

+0

同样在这里:我不明白这个问题。它需要澄清。 –

+0

@mkl ..我编辑我的问题,希望它更容易理解 – Igr

回答

3

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

  • DataStore构造函数创建一个新的Document,并调用其open方法(这还为时尚早,因为还没有作家)。
  • 某段时间后,在第一个saveData调用中,您可以拨打createFile,这会创建PdfWriter
  • 在所有saveData调用addText被调用哪个为Samples > 0每次再次打开文档(这是第一次可以,但不应该多次)。
  • 最后,在saveData呼叫Samples == 0您关闭文档。

因此,在本质上你这样做:

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 — 2nd EditionHelloWorld.java样本复制)

只适用于Samples == 1你对它有权利(由于没有编写器,构造函数中多余的document.open()被忽略);对于Samples,的较大值,虽然您可以多次使用作者在场的文档打开文档,这可能会将PDF重复一遍又一遍地重复输出到输出流。

很可能,你可以通过删除所有当前document.open()电话(包括addText()if(Samples > 0))解决该问题后PdfWriter.getInstance(document, file).

+0

谢谢,这正是我对“document.open()”这个问题的愚蠢定位。现在它正常工作:) – Igr