Solved

Java Embedding TIFF image in RTF file

Posted on 2004-09-09
12
500 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Message handling in Service Layer 3 57
GUI builder for Eclipse? 8 27
jar file executable 12 40
Arrays.asList  VS  ArrayList 4 45
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

910 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

24 Experts available now in Live!

Get 1:1 Help Now