[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 561
  • Last Modified:

Java Embedding TIFF image in RTF file

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
mjmohan
Asked:
mjmohan
  • 6
  • 4
1 Solution
 
objectsCommented:
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
 
mjmohanAuthor Commented:
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
 
objectsCommented:
> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
you can check the file sizes to see if the append worked or not.
0
 
mjmohanAuthor Commented:
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
 
objectsCommented:
But should it work that way? I would think you need to do more than just append the two.
0
 
mjmohanAuthor Commented:
Objects,
   Can you please explain more?  Is anything else missing? Any additional steps to run? Please help.

 Regards,
0
 
objectsCommented:
Sorry I'm not completely familiar with the RTF format. Why do you think you can just append the tiff?
0
 
mjmohanAuthor Commented:
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
 
objectsCommented:
Have a look at RTFEditorKit class, haven't ever used it so can't tell you anything specific sorry.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now