Solved

Java Embedding TIFF image in RTF file

Posted on 2004-09-09
12
488 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
 
LVL 92

Expert Comment

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

Author Comment

by:mjmohan
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Have a look at RTFEditorKit class, haven't ever used it so can't tell you anything specific sorry.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now