Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Embedding TIFF image in RTF file

Posted on 2004-09-09
12
Medium Priority
?
557 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

636 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