This discussion is archived
1 2 Previous Next 21 Replies Latest reply: Mar 2, 2013 5:55 AM by 796618 RSS

FileNotFoundException error

796618 Newbie
Currently Being Moderated
Good evening,

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.pdf");

the file is created successfully.

This is strange since 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:
http://pdfbox.apache.org/apidocs/overview-summary.html

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="mailto:ben@benlitchfield.com">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>" );
    }

}
  • 1. Re: FileNotFoundException error
    gimbal2 Guru
    Currently Being Moderated
    alley wrote:
    This is strange since the finalPath variable is the same as
    "C:\\Users\\MyName\\Desktop\\test.pdf" (i.e. of type String).
    No it is not, because if it were the code would work. That part you proved yourself.
  • 2. Re: FileNotFoundException error
    Tolls Journeyer
    Currently Being Moderated
    alley wrote:
    String path = "C:\\Users\\MyName\\Desktop\\";
    String extension = (stripper.getTextForRegion(
    "class1" ) + ".pdf");
    String finalPath = path.concat(extension);
    finalDoc.save(finalPath);
    Log the value of 'extension' and/or the value of 'finalPath'.
    It won't be what you think it is.
  • 3. Re: FileNotFoundException error
    796618 Newbie
    Currently Being Moderated
    Hi jimbal & Tolls,

    Thanks for your replies. I've logged the 'extension' and 'finalPath' outputs as follows:

    extension: test.pdf
    finalPath: C:\Users\MyName\Desktop\test.pdf

    This seems to be right for me, but somehow am still getting the FileNotFouldException error.

    If I insert the path manually it works!!

    Cheers.
  • 4. Re: FileNotFoundException error
    796618 Newbie
    Currently Being Moderated
    Another thing... I don't know why the error concerns a 'file not found' when it seems obvious that the file is inexsistent since it is yet to be created!
  • 5. Re: FileNotFoundException error
    EJP Guru
    Currently Being Moderated
    It"s the directory that isn't found. Change all those backslashes to single forward slashes and make your life easier.
  • 6. Re: FileNotFoundException error
    796618 Newbie
    Currently Being Moderated
    Hi EJP,

    I already tried that, to no avail.

    The output with the single forward slash is correct: C:/Users/MyName/Desktop/test.pdf

    But the same errors keeps popping up.
  • 7. Re: FileNotFoundException error
    796618 Newbie
    Currently Being Moderated
    Hi,

    I think I can say that the problem lies with this part:

    String extension = (stripper.getTextForRegion("class1" ) + ".pdf");

    In fact, if I replace the 'extension' variable as follows, the file creates successfully: String extension = "test" + ".pdf";
    I believe this is because the 'extension' variable this way is initialised with a value.

    On the other hand, with the former method, the value of the String is not yet initialised during file creation, hence it cannot be created and saved.

    What I need is that the value is first stored somewhere, and then extracted. Does anyone has any idea how can I go around this please?

    Thanks.
  • 8. Re: FileNotFoundException error
    rp0428 Guru
    Currently Being Moderated
    >
    String extension = (stripper.getTextForRegion("class1" ) + ".pdf");
    >
    Why do you have left and right parens around the value?
    >
    On the other hand, with the former method, the value of the String is not yet initialised during file creation, hence it cannot be created and saved.
    >
    What does that mean? The 'extension' variable is initialized when the assignment is done.

    Please edit your post to eliminate the invalid line breaks in your code and add 'System.out.println' statements to print out the values of the variables in question. And then cut & paste the output.
  • 9. Re: FileNotFoundException error
    EJP Guru
    Currently Being Moderated
    In fact, if I replace the 'extension' variable as follows, the file creates successfully: String extension = "test" + ".pdf";
    I believe this is because the 'extension' variable this way is initialised with a value.
    It is because you aren't calling the stripper.getTextForRegion("class1") method, which therefore must be returning null or something invalid, which is therefore forming an incorrect filename, which is causing this exception.
    Does anyone has any idea how can I go around this please?
    In your code? that you haven't posted? Not really, no.
  • 10. Re: FileNotFoundException error
    796618 Newbie
    Currently Being Moderated
    Hi rp0428, EJP

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

    I will post the code when I'm back home since it's not available at my workplace.

    Thanks.
  • 11. Re: FileNotFoundException error
    796618 Newbie
    Currently Being Moderated
    Hi again,

    This is the final code. The bold part is the added code to create a temporary file that helped me solve my problem, but I don't think it's really the way to do it.
    /*
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    package org.apache.pdfbox.examples.util;
    
    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.io.*;
    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 &lt;input-pdf&gt;
     *
     * @author <a href="mailto:ben@benlitchfield.com">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 filename = (stripper.getTextForRegion("class1"));
                    String extension = ".pdf";
                  
                    StringBuilder builder = new StringBuilder(path);
                    builder.append(filename);
                    builder.insert(30, extension);
                   
                  
                  *String p = "C:\\Users\\MyName\\Desktop\\temp.txt";*
                  *PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(p, true)));*
                  *out.write(builder.toString());*              
                  *out.close();*
                  
                  *File namefile = new File(p);*
                  *FileReader namereader = new FileReader(namefile);*
                  *BufferedReader in = new BufferedReader(namereader);*
                  *String str = in.readLine();*                         
                  *in.close();*
    
                  *finalDoc.save(str);*
                  *namefile.delete();*
                 
                }
    
                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>" );
        }
    
    }
    Thanks.
  • 12. Re: FileNotFoundException error
    EJP Guru
    Currently Being Moderated
    String path = "C:\\Users\\MyName\\Desktop\\";
    String filename = (stripper.getTextForRegion("class1"));
    String extension = ".pdf";

    StringBuilder builder = new StringBuilder(path);
    builder.append(filename);
    builder.insert(30, extension);
    The problem is clearly here, and applying bandaids to stop the bleeding isn't as good as preventing the wound. The correct way to build a filename is with File, not StringBuilder, and I'm very suspicious of that insert(). Try this:
    // 1. There is never a need to use a backslash in a Java filename. Use forward slashes
    String path = "C:/Users/MyName/Desktop";
    String filename = stripper.getTextForRegion("class1")+".pdf";
    File file = new File(path, filename);
    finalDoc.save(file.getPath());
  • 13. Re: FileNotFoundException error
    796618 Newbie
    Currently Being Moderated
    Hi EJP,

    I tried your code but unfortunately I'm still getting the FileNotFoundException error:

    run:
    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.java:212)
         at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
         at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1138)
         at org.apache.pdfbox.examples.util.ExtractTextByArea.main(ExtractTextByArea.java:102)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)


    I think the problem is created by the code stripper.getTextForRegion("class1") which returns the String test in the final path of the file. If this String is saved to a text file, and then retrieved, it works fine. But if the code is called directly without saving it, then it generates the error even though it is being assigned to a String. Very strange...

    Thanks.
  • 14. Re: FileNotFoundException error
    rp0428 Guru
    Currently Being Moderated
    You still haven't done the simplest of things.
    >
    add 'System.out.println' statements to print out the values of the variables in question. And then cut & paste the output.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points