Solved

pdf read

Posted on 2004-04-25
17
349 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

11 Experts available now in Live!

Get 1:1 Help Now