2012-05-05 15 views
0

イメージをRTFドキュメントに追加しようとしています。ドキュメントに画像を追加することはできますが、画像を追加することはできません。つまり、2番目の画像が追加されると、最初の画像が削除されます。私は、コードが実行されるたびに新しいRTF文書が作成されると思います。イメージをRTFドキュメントに追加する

public class InsertToWord { 

    com.lowagie.text.Document doc = null; 
    FileOutputStream evidenceDocument; 
    String fileName = "evidenceDocument.rtf"; 
    settings obj = null; 

    InsertToWord() { 
     obj = new settings(); 
     doc = new com.lowagie.text.Document(); 

    } 

    public void insertImage(File saveLocation) { 

     try { 
      evidenceDocument = new FileOutputStream(obj.getFileLocation() + fileName); 
      RtfWriter2.getInstance(doc, evidenceDocument); 
      doc.open(); 
      com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(saveLocation.toString()); 
      image.scaleAbsolute(400, 300); 
      doc.add(image); 
      doc.close(); 
     } catch (Exception e) { 
     } 
    } 
} 

答えて

1

insertImage()メソッドでは、実際に新しいファイルを作成していて、古いファイルを上書きしています。あなたがメソッドのパラメータとしてでたFileOutputStreamを渡した後、一斉に行を削除することができ

evidenceDocument = new FileOutputStream(obj.getFileLocation()+fileName); 

public void insertImage(FileOutputStream evidenceDocument , File saveLocation) 
+0

どうすればよいですか? –

+0

@AnujSharma答えを更新しました。これがあなたの質問を満たしていれば、答えを受け入れたものとして忘れずにマークしてください。 :) –

+0

いいえ、動作していません。まだ新しい文書が作成されています。 –

0

このコードはこの行は、新しいファイルを作成している

1つはRTFフォーマットとその細かい作業に画像を追加するために使用しています:

public void actionPerformed(ActionEvent arg0) { 

     JFileChooser fileChooser = new JFileChooser(); 
     int option = fileChooser.showOpenDialog(null); 
     File file = fileChooser.getSelectedFile(); 

     if (option == JFileChooser.APPROVE_OPTION) { 

      try { 

       BufferedImage image = ImageIO.read(file); 
       image = Scalr.resize(image, 200); 
       document = (StyledDocument) textPane.getDocument(); 
       javax.swing.text.Style style = document.addStyle("StyleName", null); 
       StyleConstants.setIcon(style, new ImageIcon(image)); 
       document.insertString(document.getLength(), "ignored text", style); 


      } 

      catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 

     if (option == JFileChooser.CANCEL_OPTION) { 

      fileChooser.setVisible(false); 

     } 

    }// End of Method 

テックスtPane変数はJTextPaneです。

関連する問題