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.

Mon Dec 14, 2020 7:00 am

I downloaded FreeSpire.PDF for Java. I opened printPdfDocument with eclipse in Sample 19 folder. I added required Jar. I use JDK 15.
Code:
Code: Select all
import com.spire.pdf.*;
import java.awt.print.*;
import java.util.Base64;

public class printPdfDocument {
   public static void main(String[] args) {
      try {
      var text = /* Text of type Base64String. I cant add it because of the character limit. I verified the value I used in c #. No error. */
                printSpirePdfDocument(text);
       } catch (Exception e) {
              e.printStackTrace();
           }
   }
   
   public static void printSpirePdfDocument(String data){
      try {
         byte[] dataByte = Base64.getEncoder().encode(data.getBytes());
           PdfDocument loDoc = new PdfDocument(dataByte);
           PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
           PageFormat loPageFormat  = loPrinterJob.defaultPage();
           Paper loPaper = loPageFormat.getPaper();
           //Remove the default printing margins
           loPaper.setImageableArea(0,0,loPageFormat.getWidth(),loPageFormat.getHeight());
           //Set the number of copies
           loPrinterJob.setCopies(1);
           loPageFormat.setPaper(loPaper);
           loPrinterJob.setPrintable(loDoc,loPageFormat);
           loPrinterJob.print();
           } catch (Exception e) {
              e.printStackTrace();
           }
   }
}


Error Stack Trace:
Code: Select all
java.lang.RuntimeException: Invalid/Unknown/Unsupported format
   at Spire.Pdf/com.spire.pdf.packages.sprgja.spr  (Unknown Source)
   at Spire.Pdf/com.spire.pdf.packages.sprgja.spr  (Unknown Source)
   at Spire.Pdf/com.spire.pdf.packages.sprbKa.<init>(Unknown Source)
   at Spire.Pdf/com.spire.pdf.packages.sprSJa.<init>(Unknown Source)
   at Spire.Pdf/com.spire.pdf.widget.PdfDocumentWidget.spr (Unknown Source)
   at Spire.Pdf/com.spire.pdf.widget.PdfDocumentWidget.<init>(Unknown Source)
   at Spire.Pdf/com.spire.pdf.widget.PdfDocumentWidget.<init>(Unknown Source)
   at Spire.Pdf/com.spire.pdf.PdfDocument.<init>(Unknown Source)
   at printPdfDocument.printSpirePdfDocument(printPdfDocument.java:18)
   at printPdfDocument.main(printPdfDocument.java:9)


How can create pdf Document with use byte? Is it related JDk types?

alixxxxxx
 
Posts: 4
Joined: Tue Dec 08, 2020 1:27 pm

Mon Dec 14, 2020 9:22 am

Hello,

Thanks for your inquiry.
Please note that the method "Base64.getEncoder().encode" is used to encode the base64 string, not to decode it. Please modify your code like this:
Code: Select all
        //byte[] dataByte = Base64.getEncoder().encode(data.getBytes());
        byte[] dataByte = Base64.getDecoder().decode(data);
        ...

If you have further questions, please feel free to let us know.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Dec 15, 2020 6:47 pm

Sorry for that. I should have been more careful.

Thank you.

alixxxxxx
 
Posts: 4
Joined: Tue Dec 08, 2020 1:27 pm

Wed Dec 16, 2020 6:49 am

It does not matter. If you have further questions, please feel free to let us know.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF