Solved

pdf read

Posted on 2004-04-25
17
359 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
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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
In this blog we highlight approaches to managed security as a service.  We also look into ConnectWise’s value in aiding MSPs’ security management and indicate why critical alerting is a necessary integration.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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