Solved

pdf read

Posted on 2004-04-25
17
350 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lamba in java 8 9 239
getting param value 7 51
sortaSum challenge java 17 171
memory representation of int[][][] a=new int[2][][]; 10 59
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now