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

}

Recommended Answers

All 6 Replies

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?

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 hope so, because it is adding images to the document.

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();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.