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

}

Dani AI

Generated

Short summary: the symptom (file grows with append=true but only the first image displays) happens because RtfWriter2 (com.lowagie) writes a complete RTF file - header, control words and closing braces - and simply appending raw bytes corrupts the RTF structure. was correct that the FileOutputStream append flag only increases file size; found the practical fix by creating a single Document/ writer session and adding every image before closing. RtfWriter2 is not designed for in-place modification of an already-written RTF.

Recommended workflow:

  1. Collect and sort the images to include.
  2. Create the Document and RtfWriter2 once, open the Document, then add each image (scale or anchor as needed).
  3. Close the Document after all images are added.
  4. Write to a temporary file and rename on success to avoid leaving a partially-written or corrupt file.

Troubleshooting and cautions: do not swallow exceptions — log or rethrow them so write errors are visible. If you must "append" later, read the current image list and rebuild the RTF from scratch rather than concatenating bytes. Inspect the .rtf in a text editor for repeated "{\rtf" headers or mismatched brace pairs to confirm corruption. Prefer standard image formats (JPEG/PNG), perform consistent scaling, and keep document creation/closing deterministic.

If true RTF editing is required, use a library that supports document modification (commercial options such as Aspose.Words can edit RTF), or consider producing DOCX instead (Apache POI / docx4j) for more robust programmatic editing. The simplest, most reliable approach for this thread is the single-session Document writer pattern that used.

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.