I'm using PdfReader to get the number of pages of a .pdf file. I tested my application on 13 pdfs today, and the 12 firsts were working fine, and the last one is blocking my application. I don't understand why, I can open the file with a FileInputStream and it works, and I can open it with Adobe so I don't think the file have an issue.
Here is how I create my PdfReader :
// This line is block my application for the 13th file :
PdfReader pdf = new PdfReader(filename);
int pageCount = pdf.getNumberOfPages();
Edit :
Some of theses pdf files are files I zipped in a Zip file, and I unZipped them. The file who is causing troubles is one of them, but others zipped/unZipped files are working fine
I solved my issue by adding a dependency in my pom.xml :
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
<dependency>
I was using Maven's itextpdf library which was in 2.1.7
Related
I've a resource folder(files) in my java swing project which contains a text file (users.txt), I'm reading and writing data into that text file.
When i export it as jar file, reading from that file is fine, but writing in the file is problem.
I'm reading from file this way
InputStream in1 = getClass().getResourceAsStream("/files/users.txt");
BufferedReader reader1 = new BufferedReader(new InputStreamReader(in1));
Reading is perfectly working fine in jar file
and writing in the same file below code(which is problem for jar file)
File file = new File("src/files/users.txt");
FileWriter fw = new FileWriter(file,true);
fw.write(data+"\n");
fw.close();
please help me out how can i write into the file in resource folder using jar file.
Thanks!
As the comments already say, files in the JARs are considered read-only.
You have to create a file at some user or installation directory based location (or a location chosen by the user).
If you have pre-configured data you have at least 2 options:
Ship the file along the JAR file using some kind of packaging tool (can also be a ZIP file). For ZIP files have a look at the Maven Assembly Plugin, if you're using Maven.
Extract the file from the JAR.
For the latter use case I've written some utility classes:
public void extractResource(String resourcePathString, Path targetDirPath) throws IOException, URISyntaxException {
URI jarURI = JarFiles.getJarURI(SomeClassInTheJar.class);
try (FileSystem jarFS = JarFiles.newJarFileSystem(jarURI)) {
Path resourcePath = jarFS.getPath(resourcePathString);
CopyFileVisitor.copy(resourcePath, targetDirPath);
}
}
With the help of CopyFileVisitor you can easily recursively extract/ add directories from/ to JAR or ZIP files, as CopyFileVisitor uses PathUtils and thus works across file systems.
JarFiles.getJarURI gets the JAR URI of a class.
For more information have a look at the tutorial: https://www.softsmithy.org/softsmithy-lib/lib/2.1.1/docs/tutorial/nio-file/index.html#ExtractJarResourceSample
The library is Open Source. You can get it from Maven Central:
<dependency>
<groupId>org.softsmithy.lib</groupId>
<artifactId>softsmithy-lib-core</artifactId>
<version>2.1.1</version>
</dependency>
I have some problems with Maven, Excel and poi package.
I access to an excel file thanks to the code :
Workbook workbook = WorkbookFactory.create(new File("src/main/resources/file.xlsx"));
Sheet sheet = workbook.getSheet(sheetName);
This code works correctly and I can read data inside later in my code.
Instead of a "new File(..)", I have to use this code below to access resources in dev mode and once the jar is built.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
String path = classLoader.getResource(fileName).toURI().getPath();
The given path is in "target/classes" and Maven do a "copy" of this file into folder "myproject/target/classes" of the current project(perfect so).
However, the xslx file copied by Maven is corrupted and neither by using Excel software, I can't access to its content. The original file size is 500Kb, the copied file size is more than 1Mb. (All other files img,txt.. are well copied excepted the xslx files)
I done lots of searches, I could find some answers like :
FileInputStream vs ClassPathResource vs getResourceAsStream and file integrity
. I tried all solutions I could find but impossible to solve mine and I always get the same error :
InvalidOperationException: Could not open the specified zip entry source stream
Or
java.io.FileNotFoundException: file.xlsx
From the same way of classLoader, I can access to my json, txt and image files.
Someone has answer on this issue ?
Why Maven doubles the size of the xlsx files and why they are corrupted ?
Any solution to solve that ?
I need help
I'm creating a pdf file in my java program.
After having it created to a specified Path, I'm opening it with the user's standard application.
File myFile = new File("F:/test.pdf");
Desktop.getDesktop().open(myFile);
My standard application for pdf files is for example Adobe Reader - so Adobe Reader opens up and displays the file. - So far so good.
Is there any way to delete this "test.pdf" after I close the file/my Adobe Reader?
Check the following link:
Java: Check if file is already open
Run an infinite loop after you open the loop, as mentioned in the above thread, verify and close the file accordingly.
Thanks,
JK
You can create the file in the temp directory, so you will not have to worry about removing it.
To create a file in the temp directory you should use the File.createTempFile method.
I'm trying to process docx file with Apache POI. Just simply read and then write file (just for now). Here is my simple code:
FileInputStream fileInputStream = new FileInputStream(inputFile);
XWPFDocument document = new XWPFDocument(OPCPackage.open(fileInputStream));
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
document.write(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
fileInputStream.close();
Problem is that input file has small image in header. Because of that after processing input file with POI and opening output file in Microsoft Word I get corrupted file error :
Microsoft Office cannot open this file because some parts are missing or invalid.
Location: Part: /word/settings.xml, Line: 2, Column: 0
Everything works in OO Writer, but not in office.
The question is : what is wrong? Does apache POI not process files with image in header? Do you know any way to work around the problem?
I NEED to use Apache POI, I don't take into consideration other tools. Also I use POI 3.8
The problem is not with the image header but with the Apache POI jar version. Use the latest jars.
poi-3.10-FINAL.jar
poi-ooxml-3.10-FINAL.jar
poi-ooxml-schemas-3.10-FINAL.jar
ooxml-schemas-1.1.jar
Having the above jars solved the issue for me.
I have to attach a pdf file in a pptx slide during runtime.
Tried the following:
Attached a pdf file in the pptx slide (Insert -> Object -> Adobe Acrobat Document).
Accessed the oleobject using the following code :
OleObjectBinaryPart oleObjectBinaryPart = new OleObjectBinaryPart(new PartName("/ppt/embeddings/oleObject1.bin"));
Updating the oleObjectBinaryPart using the following code:
oleObjectBinaryPart.setBinaryData(reportBlob.getBinaryStream());
Updating the pptx with the new oleobject:
pptMlPackage.getParts().getParts().put(new PartName("/ppt/embeddings/oleObject1.bin"), oleObjectBinaryPart);
pptMlPackage.save(new File("C:/test_report/pptx_out.pptx"));
After executing this code the pptx_out.pptx file got generated without any errors. But while trying to open the embedded pdf in powerpoint 2010 I'm getting following error:
The server application, source file, or item can't be found, or returned an unknown error. You may need to reinstall the sever application.
Is it a problem with the oleobject when updating?
You can't just attach the PDF as a binary blob; it has to be in the correct OLE format.
See further this discussion.