Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: FileNotFoundException error

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!


     
    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>" );
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FileNotFoundException error

    Quote Originally Posted by Norm View Post
    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:

    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: FileNotFoundException error

    Can you post a small program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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:

    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:

    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?

    Instead of making a small program
    Without a program that shows the problem, what can be done?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FileNotFoundException error

    Quote Originally Posted by Norm View Post
    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 View Post
    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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Unhandled Exception Type FileNotFoundException
    By collision934 in forum Exceptions
    Replies: 11
    Last Post: February 18th, 2013, 05:32 PM
  2. Unhandled Exception Type FileNotFoundException
    By collision934 in forum Exceptions
    Replies: 0
    Last Post: February 15th, 2013, 04:01 PM
  3. [SOLVED] FileNotFoundException on ftp connection
    By hdty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2013, 07:39 PM
  4. [SOLVED] FileNotFoundException via Scanner
    By mtn_student in forum Exceptions
    Replies: 6
    Last Post: August 31st, 2011, 04:14 PM
  5. FileNotFoundException!?
    By Shaddam in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 29th, 2010, 06:46 PM

Tags for this Thread