am trying to add images to a rtf document. I am able to add images to the document but I can't append any images. This means that when the 2nd Image is added, the first image is removed. I think that whenever the code is executed a new rtf document is created.

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){}
    }

}

Read the API doc for the FileOutputStream class. Its constructor has an arg for appending to the file.

am class. Its constructor has an arg for appending

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

I can see the document size increase, however I can't see the images in it. Can only see the first image.
Any suggestions.

I don't know anything about the internals of an RTF file. Are you sure your methods are correct for changing the contents of an RTF file?

Edited 4 Years Ago by NormR1

am class. Its constructor has an arg for appending

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

I can see the document size increase, however I can't see the images in it. Can only see the first image.
Any suggestions.

It's working now.
looped all the files at a specific location and then added to the rtf document. Seems that iText does not support modifying of rtf document.

File evidenceDocLocation = new File(obj.getFileLocation().toString());

                File[] listOfScreenshots = evidenceDocLocation.listFiles();
                for (int i = 0; i < listOfScreenshots.length; i++) 
                {
                    if (listOfScreenshots[i].isFile() && !listOfScreenshots[i].isDirectory()) 
                    {

                        com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(listOfScreenshots[i].toString());
                        image.scaleAbsolute(400, 300);
                        doc.add(image); 
                        log.createLog(listOfScreenshots[i]);
                    }
                }
                doc.close();
This question has already been answered. Start a new discussion instead.