FileNotFoundException error
Good afternoon,
First of all I would like to thank the administrators for accepting my registration to the forum.
My problem is that I'm running the code hereunder to generate a new file from extracted content, and for some reason I'm getting a FileNotFoundException error.
The strange thing is that if I manually replace this part (where the error is being generated):
finalDoc.save(finalPath);
with this part:
finalDoc.save("C:\\Users\\MyName\\Desktop\\test.pd f");
the file is created successfully.
This is strange since the output of the finalPath variable is the same as "C:\\Users\\MyName\\Desktop\\test.pdf" (i.e. of type String).
This is the API of the package:
Overview (Apache PDFBox 1.2.1 API)
Thanks for any help!
Code Java:
import org.apache.pdfbox.exceptions.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.util.PDFTextStripperByArea;
import java.awt.Rectangle;
import java.util.List;
/**
* This is an example on how to extract text from a specific area on
the PDF document.
*
* Usage: java org.apache.pdfbox.examples.util.ExtractTextByArea
<input-pdf>
*
* @author <a href="#">Ben Litchfield</a>
* @version $Revision: 1.2 $
*/
public class ExtractTextByArea
{
private ExtractTextByArea()
{
//utility class and should not be constructed.
}
/**
* This will print the documents text in a certain area.
*
* @param args The command line arguments.
*
* @throws Exception If there is an error parsing the document.
*/
public static void main( String[] args ) throws Exception
{
if( args.length != 1 )
{
usage();
}
else
{
PDDocument document = null;
try
{
document = PDDocument.load( args[0] );
if( document.isEncrypted() )
{
try
{
document.decrypt( "" );
}
catch( InvalidPasswordException e )
{
System.err.println( "Error: Document is encrypted with a password." );
System.exit( 1 );
}
}
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition( true );
Rectangle rect = new Rectangle( 335, 90, 30, 20 );
stripper.addRegion( "class1", rect );
List allPages = document.getDocumentCatalog().getAllPages();
PDPage firstPage = (PDPage)allPages.get( 0 );
stripper.extractRegions( firstPage );
PDDocument finalDoc = new PDDocument();
finalDoc.addPage(firstPage);
String path = "C:\\Users\\MyName\\Desktop\\";
String extension = (stripper.getTextForRegion("class1" ) + ".pdf");
String finalPath = path.concat(extension);
finalDoc.save(finalPath);
}
finally
{
if( document != null )
{
document.close();
}
}
}
}
/**
* This will print the usage for this document.
*/
private static void usage()
{
System.err.println( "Usage: java
org.apache.pdfbox.examples.util.ExtractTextByArea <input-pdf>" );
}
}
Re: FileNotFoundException error
Please copy the full text of the error message and paste it here.
Add a println to the code to print out the value of the finalPath variable.
Re: FileNotFoundException error
Quote:
Originally Posted by
Norm
Please copy the full text of the error message and paste it here.
Add a println to the code to print out the value of the finalPath variable.
Hi,
These are the println outputs:
extension: test.pdf
finalPath: C:\Users\MyName\Desktop\test.pdf
(PS: I've also tried the code with forward slashes, still to no avail: String path = "C:/Users/MyName/Desktop/"; )
This is the error being generated:
Quote:
run:
Exception in thread "main" java.io.FileNotFoundException: C:\Users\MyName\Desktop\test
.pdf (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:212)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:104)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocume nt.java:1138)
at org.apache.pdfbox.examples.util.ExtractTextByArea. main(ExtractTextByArea.java:91)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
To note that if I insert the path manually like this, it works: finalDoc.save("C:\\Users\\MyName\\Desktop\\test.pd f");
Thanks.
Re: FileNotFoundException error
Can you post a small program that compiles, executes and shows the problem?
Re: FileNotFoundException error
Hi,
Instead of making a small program, may I please rephrase my problem after I've discovered something else?
So the problem lies with this part:
Quote:
String path = "C:\\Users\\MyName\\Desktop\\";
String extension = (stripper.getTextForRegion("class1" ) + ".pdf");
String finalPath = path.concat(extension);
finalDoc.save(finalPath);
Now I've tried to replace the extension variable as follows:
Quote:
String extension = "test" + ".pdf";
This way the file was created successfully without errors.
Thus, I think we can conclude that the problem lies during the content being generated with this part:
Quote:
stripper.getTextForRegion("class1" )
I'm thinking that since the filename of the file to be created is assigned by the code above, it first needs to be stored somewhere, and then extracted. I don't know if I'm making myself understandable, and I don't actually know how this can be solved if it is really the problem.
Thanks.
Re: FileNotFoundException error
If the printed value of finalPath was the same value that you used manually, what does all the code you just posted have to do with the problem?
Quote:
Instead of making a small program
Without a program that shows the problem, what can be done?
Re: FileNotFoundException error
Quote:
Originally Posted by
Norm
If the printed value of finalPath was the same value that you used manually, what does all the code you just posted have to do with the problem?
If I insert the value manually, there is no problem because the content of the variable is already initialised (i.e. manually)
On the other hand, if the value has to be generated by stripper.getTextForRegion("class1"), then it is yet to be initialised and I think the code is finding a problem creating the file with a not-yet-initialised filename.
Quote:
Originally Posted by
Norm
Without a program that shows the problem, what can be done?
The thing is that a normal Java program would compile without problems. But my problem lies with a code taken from this 'pdfbox' package, so in order for compilation one needs to install this package. The program in my original post is not long and compilable, but one needs the package.
Re: FileNotFoundException error
What I don't understand was the value of finalPath that printed out when you executed the code.
If finalPath has the value you posted:
finalPath: C:\Users\MyName\Desktop\test.pdf
Did the code give the error when that was printed out?
You never copied the whole of the console's contents that showed the print out and the error message together.
Re: FileNotFoundException error
It did not print out because I had put the println after the code that was generating the error. Now I've changed the order to make the println initialise before the error kicks in. The bold part is the println, and the following is the error generated during the same run of the program.
Quote:
run:
C:\Users\MyName\Desktop\test.pdf
Exception in thread "main" java.io.FileNotFoundException: C:\Users\MyName\Desktop\test.pdf
(The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:212)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:104)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocume nt.java:1138)
at org.apache.pdfbox.examples.util.ExtractTextByArea. main(ExtractTextByArea.java:93)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Re: FileNotFoundException error
Now the error message is different.
It was: (The system cannot find the path specified)
now its: (The filename, directory name, or volume label syntax is incorrect)
why is that?
Re: FileNotFoundException error
I don't know, probably I was experimenting with different possibilities.
It's still a FileNotFoundException though.
Do you know what (The filename, directory name, or volume label syntax is incorrect) could mean?
Re: FileNotFoundException error
Hi,
I sort of solved the issue by saving the output of the finalPath variable to a text file, and then call it back from the text file in the last part of the program... i.e. finalDoc.save(finalPath retrieved from text file);
Thanks.