Solved

pdf read

Posted on 2004-04-25
17
357 Views
Last Modified: 2010-04-01
Hi Experts,

I am reading pdf using bits .....sometime bit is different it can be 1024 / 2048 etc ...How to find out this attribute before processing file ?

Thank you.
0
Comment
Question by:fpoyavo
[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
  • 10
  • 6
17 Comments
 
LVL 1

Author Comment

by:fpoyavo
ID: 10913282
I have found this code ... can it be useful ?

<code>
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;

public class Print {

public Print()
{}

public static void main(String[] args)
{
// PrintDoc is an implementation of Doc
Doc pdoc = new PrintDoc("file.ps");

// Gets the format of the document
DocFlavor flavor = pdoc.getDocFlavor();

// Creates a new attribute set
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

// Locates default printer to print the specified document format
// with the specified attribute set
PrintService service = PrintServiceLookup.lookupDefaultPrintService();

if (service != null) {
DocPrintJob job = service.createPrintJob();

try {
job.print(pdoc, aset);
}
catch (PrintException pe) { }
}
}
}

class PrintDoc implements Doc {
private String filename;
private InputStream stream;

public PrintDoc(String fname)
{
this.filename = fname;
}
public DocFlavor getDocFlavor() {
return DocFlavor.INPUT_STREAM.AUTOSENSE;
}

// No attributes attached to this Doc - mainly useful for MultiDoc
public DocAttributeSet getAttributes() {
return null;
}

// Since the data is to be supplied as an InputStream delegate
// to getStreamForBytes().
public Object getPrintData() throws IOException {
return getStreamForBytes();
}

// Not possible to return as text
public Reader getReaderForText() throws UnsupportedEncodingException, IOException {
return null;
}

// Return the print data as an InputStream.
// Always return the same instance.
public InputStream getStreamForBytes() throws IOException {
synchronized(this) {
if (stream == null) {
stream = new FileInputStream(filename);
}
return stream;
}
}
}
</code>
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10913343
I have :

byte[] buf = new byte[2048];
                int nread = 0;
                 while (-1!=(nread=in.read(buf)))
                 {
                   outf.write(buf, 0, nread);
                 }

May be there is some way to find out that file has to be read as 1024 ????
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10913518
it does not really make a diff for you to read it in 1028 chunks or 2048 chunks...
both shd be working....

you can use, nread.available() method to find out the available bytes to read...
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 1

Author Comment

by:fpoyavo
ID: 10913647
Kudleep,

Please more details. :) I know you are busy but please.

Thank you.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10913696
you can read your stream in different ways...

one can be
//read one byte at a time

byte buf = new byte[2048];
                int nread = 0;
                 while (-1!=(buf=in.read()))
                 {
                   outf.write(buf);
                 }
//slow but reliable

you can read all at one time
byte[] buf = new byte[in.available()];
                int nread = in.available();
                 outf.write(buf, in.read(buf), nread);
                 }

//fast but some times jumps if the stream is not ready by the time we execute in.available();

or you can read in it chucks of 1 K

byte[] buf = new byte[2048];
                int nread = 0;
                 while (-1!=(nread=in.read(buf)))
                 {
                   outf.write(buf, 0, nread);
                 }

so its up to you..
I usually use the first one becoz its most reliable

0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10913776
Hi Kudeep,

This is great :

byte[] buf = new byte[in.available()];
                int nread = in.available();
                 outf.write(buf, in.read(buf), nread);
                 }

How I can do delay 1 sec here before ...because it looks like it is too fast ?
And ...shouldit be while here ? or } just a typo ?

Thank you very much.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10913786
don't need a why here because we are reading everything available on the stream in one go....:-)
putting a 1 sec delay ruines the whole gain that you get in the performance so I do not suggest that....
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10913814
iT SEEMS that stream is not ready by the time we execute in.available :( ???? What is next ?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10913819
Have you tried other two variations that I have written???
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10914306
Yep.

The first one gave me : Incompatible type for declaration. Can't convert byte[] to byte.
in byte buf = new byte[2048];

The second one gives error opening pdf from jsp and if I do it directly.

The third one the I used and process fine pdfs that have 2048 when I have byte[2048] but those having 1024 message "damaged file" is displayed.
WheN I change it to byte[1024] it process fine pdfs with 1024 but has problem with those having 2048.

Please help.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10914308
Is there way to find out if file has 1024 or  2048 ?
0
 
LVL 23

Accepted Solution

by:
rama_krishna580 earned 500 total points
ID: 10914564
try this,.,


res.setContentType("application/pdf");
ServletOutputStream out = res.getOutputStream();

//The filename is passed in as a parameter;
String sFileName = (String)req.getParameter("filename");

// Override the filename for testing.
sFileName="myReport.pdf";

try {
File file = new File(sFileName);

byte[] byteContents = new byte[(int)file.length()];

FileInputStream in = new FileInputStream(sFileName);

// Read the file.
int retcd = in.read(byteContents);

// Read past end of file.
// retcd = in.read(byteContents);

in.close();

out.write(byteContents);
out.flush();
out.close();


res.setContentType("application/pdf");
//res.setStatus(res.SC_OK);
return;
}
catch (Exception e)
{
System.out.println(">>>> srvletDownloadPDF exception: " + e);
//res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.toString());
return;
}

R.K
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10914682
Rama,

I got this : Undefined variable or class name: file
                byte[] byteContents = new byte[(int)file.length()];
pointing to file.length ????
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10914742
Ok...It writes file but due to errors it cannot be opened.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10914881
All the time I was thinking that you are getting the file from a URL..!! and not from disk....
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10924445
You are right I am getting it from URL but I used your technique Kudleep (thank you for this) and works fine for URL or regular file.
Rama was close because the only thing I have missed was close files and trying to open them again later in my logic.

Thank you bro.  
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10924561
:-)
got that, I was wondering...... Thanks for clarifying
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
struts spring hibernate example 12 137
jdbc error in jsp application 20 74
login form jsp example 2 56
ejb on wildfly 5 48
This article was initially published on Monitis Blog, you can read it here . When it comes to deciding which approach to website performance monitoring is best for your business, unfortunately, like so many options in life . . . it depends. In t…
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

696 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