I get NPE
, but it's working on WIN PC.Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.indexOf(String)" because "com.spire.pdf.graphics.PdfFontBase.spr▌–().spr”" is null
I'm uploading .xps file and my converter code:
- Code: Select all
public class Converter {
public static String convert(String inputDirectoryPath) {
File inputDirectory = new File(inputDirectoryPath);
if (!inputDirectory.exists() || !inputDirectory.isDirectory()) {
throw new IllegalArgumentException("The specified input directory does not exist or is not a directory");
}
File[] xpsFiles = inputDirectory.listFiles((dir, name) -> name.toLowerCase().endsWith(".xps"));
if (xpsFiles == null) {
throw new IllegalArgumentException("The specified input directory does not exist or is not a directory");
}
if (xpsFiles.length == 0) {
throw new IllegalArgumentException("The specified input directory does not contain any XPS files");
}
String outputDirectoryPath = "%s/output-%s/".formatted(inputDirectory.getAbsolutePath(), getFormattedDate());
new File(outputDirectoryPath).mkdir();
System.out.println("Converting XPS files from folder " + inputDirectoryPath + " to PDF files to folder " + outputDirectoryPath);
Arrays.stream(xpsFiles).forEach(xpsFile -> convertXpsToPdf(outputDirectoryPath, xpsFile));
System.out.println("Done converting XPS files to PDF files");
return outputDirectoryPath;
}
private static void convertXpsToPdf(String outputDirectoryPath, File xpsFile) {
String absolutePath = xpsFile.getAbsolutePath();
String destinationFilePath = outputDirectoryPath + getFileName(absolutePath) + ".pdf";
System.out.println("Converting file '" + absolutePath + "' to PDF...");
PdfDocument pdfDocument = new PdfDocument();
pdfDocument.loadFromXPS(absolutePath);
pdfDocument.saveToFile(destinationFilePath, PDF);
pdfDocument.close();
}
private static String getFileName(String sourceFilePath) {
return new File(sourceFilePath).getName().replaceFirst("[.][^.]+$", "");
}
private static String getFormattedDate() {
return new SimpleDateFormat("dd-MM-yyyy").format(new Date());
}
}