Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sun Mar 28, 2021 3:44 pm

The sample code

PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("sample.pdf");

doesn't work in spring boot application. Does anyone know how to workaround it? Does class PdfDocument have method(s) to load pdf from InputStream or byte[]? Is there Java API doc for com.spire.pdf.PdfDocument?

Thanks!

szhou100
 
Posts: 2
Joined: Sun Mar 28, 2021 1:19 pm

Mon Mar 29, 2021 9:01 am

Hello,

Thanks for your inquiry.
I created a spring boot application and tested your case with the latest Spire.PDF for Java Version:4.1.2, but did not encounter any issue. Did you get an error when loading the file? If so, I’m afraid it may be related to your PDF file. Please provide us with your error message and the file “sample.pdf” for further investigation.

As for loading PDF from InputStream and byte[], please refer to the following code. And here is the online Java API doc of Spire.PDF for Java: https://www.e-iceblue.com/Tutorials/API ... -Java.html.
Code: Select all
//1. Load from InputStream
PdfDocument doc = new PdfDocument();
doc.loadFromStream(inputStream);

//2. Load from byte[]
PdfDocument doc = new PdfDocument();
doc.loadFromBytes(byteArray);


Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Mon Mar 29, 2021 2:42 pm

Good morning!

Spire.PDF for Java Version:4.1.2 kills the loadFromFile() issue(I used to use 2.2).

But the dialog box doesn't popup and loPrinterJob.printDialog() return false. I run my app either within eclipse or via gradlew bootRun in command console. Any idea? Thanks.

PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("/data/approval-pdf.pdf");
PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
PageFormat loPageFormat = loPrinterJob.defaultPage();
Paper loPaper = loPageFormat.getPaper();
loPaper.setImageableArea(0, 0, loPageFormat.getWidth(), loPageFormat.getHeight());
loPageFormat.setPaper(loPaper);
loPrinterJob.setPrintable(pdf, loPageFormat);

// display the print dialog
if (loPrinterJob.printDialog()) {
try {
loPrinterJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}

szhou100
 
Posts: 2
Joined: Sun Mar 28, 2021 1:19 pm

Tue Mar 30, 2021 9:56 am

Hello,

Thanks for your feedback.
When testing your code in the web project, I did notice that the dialog box doesn't pop up. However, I tested the same code in a normal java project and found that it works well.
Meanwhile, I tested another open source PDF tool (code is as follows), and also used PrinterJob to print a PDF document, I found that the dialog box doesn't popup either. I'm afraid this is because the PrinterJob API does not support showing the dialog box in Web projects. Hope you can understand.
Code: Select all
        org.apache.pdfbox.pdmodel.PDDocument document = org.apache.pdfbox.pdmodel.PDDocument.load(new File("test.pdf"));
        PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
        loPrinterJob.setPageable(new org.apache.pdfbox.printing.PDFPageable(document));
        Boolean b=loPrinterJob.printDialog();
        try {
            loPrinterJob.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        }

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.PDF