?
Solved

pdf read

Posted on 2004-04-25
17
Medium Priority
?
374 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
  • 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
Independent Software Vendors: 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 2000 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

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.

Question has a verified solution.

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

Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

807 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