Solved

Java Embedding TIFF image in RTF file

Posted on 2004-09-09
12
512 Views
Last Modified: 2008-03-06
Hi,
    I am trying to write a code to combine RTF file and TIFF file into an output RTF file.  That means, output RTF file should have RTF file contents at the top followed by TIFF image. The code is:

import java.io.*;

public class RTFTest
{
      public RTFTest(String rtfFile, String tifFile)
      {
            try
            {
                  BufferedInputStream bis = new BufferedInputStream (new FileInputStream (new File(rtfFile)));
                  int iAvailable = bis.available();
                  byte[] b = new byte[iAvailable];
                  bis.read (b, 0, iAvailable);
                  bis.close();

                  BufferedInputStream bis2 = new BufferedInputStream (new FileInputStream (new File(tifFile)));
                  int iAvailable2 = bis2.available();
                  byte[] b2 = new byte[iAvailable2];
                  bis2.read (b2, 0, iAvailable2);
                  bis2.close();

                  BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (new File("output.rtf")));
                  bos.write (b, 0, iAvailable);
                  bos.write (b2, 0, iAvailable2);
                  bos.flush();
                  bos.close();
            }
            catch(Exception ex)
            {
                  ex.printStackTrace();
            }
      }

      public static void main(String args[])
      {
            new RTFTest("./input.rtf", "./test.tif");
      }
}

But, when I open the output.tif, I could only see RTF contents.  Not sure what happened to the TIFF contents written onto it.  Please help me with this.

 Regards,
0
Comment
Question by:mjmohan
  • 6
  • 4
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12023318
that code will not necessarily read the entire file into the buffer.
You also don't need to read the entire file into buffer.

              byte[] b = new byte[1024];
              int nread = 0;

              BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (new File("output.rtf")));

              BufferedInputStream bis = new BufferedInputStream (new FileInputStream (new File(rtfFile)));
              while (-1!=(nread=bis.read(buf)))
              {
                  bos.write(buf, 0, nread);
              }
              bis.close();

              // do same for other file

              bos.flush();
              bos.close();
0
 
LVL 2

Author Comment

by:mjmohan
ID: 12023415
Hi Objects,
    I tried the above, as you mentioned.  Though the process is different, output is same. When I open output.rtf, I still cannot see Image.  Any inputs???
    Modified code:

      byte[] buf = new byte[1024];
      int nread = 0;

      BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (new File("output.rtf")));

      BufferedInputStream bis = new BufferedInputStream (new FileInputStream (new File(rtfFile)));
      while (-1!=(nread=bis.read(buf)))
      {
        bos.write(buf, 0, nread);
      }
      bis.close();

      bis = new BufferedInputStream (new FileInputStream (new File(tifFile)));
      while (-1!=(nread=bis.read(buf)))
      {
        bos.write(buf, 0, nread);
      }
      bis.close();

      bos.flush();
      bos.close();

 Regards,
0
 
LVL 92

Expert Comment

by:objects
ID: 12023434
> When I open output.rtf, I still cannot see Image.

Are you sure all you need to do is append the tiff to the end of the rtf file?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 92

Expert Comment

by:objects
ID: 12023517
you can check the file sizes to see if the append worked or not.
0
 
LVL 2

Author Comment

by:mjmohan
ID: 12023533
Objects,
  input.rtf size is 4 kb.
  test.tif size is 70 kb.
  output.rtf size is 74 kb.

 Till here, it is fine and I can understand that the contents are appended to output. But, I could not see any image when I open the file. I can only see RTF contents.
0
 
LVL 92

Expert Comment

by:objects
ID: 12023551
But should it work that way? I would think you need to do more than just append the two.
0
 
LVL 2

Author Comment

by:mjmohan
ID: 12023560
Objects,
   Can you please explain more?  Is anything else missing? Any additional steps to run? Please help.

 Regards,
0
 
LVL 92

Expert Comment

by:objects
ID: 12023574
Sorry I'm not completely familiar with the RTF format. Why do you think you can just append the tiff?
0
 
LVL 2

Author Comment

by:mjmohan
ID: 12023587
I need to do this for multiple files. I will be constantly polling on a directory and combine any RTF and TIFF files in that. Manual process may not be a valid option for this.
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 12023613
Have a look at RTFEditorKit class, haven't ever used it so can't tell you anything specific sorry.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 84
Java - Why doesn't this JFrame work 3 60
HashTable highest marks enumeration alternative 9 39
Java: The Public Class Main 4 17
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question