Sudden Exception in Convert with trial demo key - When already worked before

Product:
Apryse SDK

Product Version:
Demo

Please give a brief summary of your issue:
(Think of this as an email subject)

Exception On line “doc = new PDFDoc(filePath);” or “PDFDoc pdfdoc = new PDFDoc()”

Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)

Exception:
Message: Exception:
Message: [2025-01-28T08:36:27Z] The PDFNet Terminate function has been called. Please invoke PDFNet Initialize function before calling other functions.

 Conditional expression: !m_throw_error
 Version      : 10.12.1-9208511dea
 Platform     : Windows
 Architecture : AMD64
 Filename     : APIDataCollector.cpp
 Function     : trn::PDF::APIDataCollector::ThrowIfServerError
 Linenumber   : 381

 Conditional expression: !m_throw_error
 Filename   : APIDataCollector.cpp
 Function   : trn::PDF::APIDataCollector::ThrowIfServerError
 Linenumber : 381
 Error code : 0

The file exists and can be opened.
PDFNet.initialize was successfully run before.

Please provide a link to a minimal sample where the issue is reproducible:

public static boolean ConvertToPdfFromFile(String inFile, String outFile)
{
    boolean err = false;
    Testfile file = new Testfile(inFile, outFile);
        
    System.out.println("Before new pdfDoc convert");

    try (PDFDoc pdfdoc = new PDFDoc())
    {
    System.out.println("After new pdfDoc convert");
            //use built in converter
            ConvertPrinter.setMode(ConvertPrinter.e_convert_printer_prefer_builtin_converter);
            Convert.toPdf(pdfdoc, file.inputFile);
            pdfdoc.save(file.outputFile, SDFDoc.SaveMode.LINEARIZED, null);
            System.out.println("Converted file: " + file.inputFile);
            System.out.println("to: " + file.outputFile);
    }
    catch (PDFNetException e)
    {
            System.out.println("ERROR: on input file " + file.inputFile);
            System.out.println(e);
            err = true;
    }

    return err;
}
1 Like

Your provided sample code neither calls PDFNet.Initialize() nor PDFNet.Terminate(), so it is unclear how it relates to the issue at hand.

PDFNet.Initialize() must be the first PDFNet API call you make. If it was working before, but then stopped working, without any change in your code, then there must be a race-condition.

PDFNet.Initialize() should be called once for your process, as early as possible.

If none of the above helps resolve the issue, please modify one of our SDK samples to reproduce.

1 Like

Hi Ryan,

Of course I’m using PDFNet.initialize and PDFNet.terminate

I also wrote in the initial post: “PDFNet.initialize was successfully run before.” !!!

Otherwise it wouldn’t worked for the first place.

I’ve sent you only the problematic part of the code.
Please help me!!
I need this as soon as possible.
It used to work with a Demo key: “demo:1724309704321:7e45b27503000000002aa6b5387fc0ead78b0521bbe64eb2372790494f”
Then I used another key - but realized only after - it was for Linux.
So tried to use the Demo key again (the code is on Windows) - Didn’t work.
Downloaded another Demo key - also didn’t work.

What could be a reason for that?

The whole code is (I assumed it is not necessary):

public static void testPrint (String filePath) {
    PDFNet.initialize(/*PDFTronLicense.Key()*/);
    System.out.println("_______________________________________________");
    System.out.println("Sample 3 - Print several PDF documents...");
    
    try {
        // Path to the PDF file
        filePath = "D://apryse//testfiles_input//32268515.jpeg"; // Linux Slash - '/'
        String filePath2 = "D:\\apryse\\testfiles_output\\F388451.pdf"; // Windows slash - '\'
        
        File f = new File(filePath);
        System.out.println("file exists: " + f.exists());
        f = new File(filePath2);
        System.out.println("file2 exists: " + f.exists());
        
        try {
            FileReader fileReader = new FileReader(f.getAbsolutePath());
            fileReader.read();
            fileReader.close();
            System.out.println("The file can be read");
        } catch (Exception e) {
            System.out.println("Exception when checking if file could be read with message:" + e.getMessage());
        }

        // Load the PDF document
        PDFDoc doc = null;
        try {
            doc = new PDFDoc(filePath);
        }
        catch (Exception e) {
            String filePathConverted = filePath.replace(filePath.substring(filePath.indexOf(".")+1), "pdf");
            System.out.println("filePathConverted: " + filePathConverted);
            boolean err = ConvertToPdfFromFile(filePath, filePathConverted);
            if (err) {
                System.out.println("ConvertFile failed");
            }
            else {
                System.out.println("ConvertFile succeeded");
                doc = new PDFDoc(filePathConverted);
            }
        }
        doc.initSecurityHandler();

        System.out.println("doc file name: " + doc.getFileName());
        
        // Example 1: use the PDF.Print.startPrintJob interface
        // This is silent (no progress dialog) and blocks until print job is at spooler
        
        // Setup PrinterMode options
        PrinterMode printerMode = new PrinterMode();
        printerMode.setCollation(true);
        printerMode.setCopyCount(1);
        printerMode.setDPI(300); // regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
        printerMode.setDuplexing(PrinterMode.e_Duplex_Auto);
        printerMode.setScaleType(PrinterMode.e_ScaleType_FitToOutputPage);
        
        PageSet pagesToPrint = new PageSet(1, doc.getPageCount(), PageSet.e_all);
        
        // Print the document
        Print.startPrintJob(
            doc,                          // PDF document
            null, // Printer name (null for default)
            doc.getFileName(),            // PDF document name - // Job name (displayed in the print queue)
            "",                           
            pagesToPrint,                 // Page range (null for all pages)
            printerMode,                  // Print settings (null for default settings)
            null                          // Callback (null if not needed)
        );
        
        System.out.println("Print job sent successfully!");

        // Close the document
        doc.close();

    } catch (PDFNetException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        // Terminate PDFNet
        com.pdftron.pdf.PDFNet.terminate();
    }
}
1 Like

Are you calling PDFNet.Terminate() over and over again?

It should only be called once, when your application is terminating/closing.

1 Like

I’m calling testPrint only once - no loops here.
PDFNet.Terminate() is being called only once!!!
And it’s NOT working.

I really would like to get some help here!!

1 Like

Sorry to hear your frustration, however your code worked fine for me.

You earlier said

Exception On line “doc = new PDFDoc(filePath);” or “PDFDoc pdfdoc = new PDFDoc()”

But the exception reported was

The PDFNet Terminate function has been called. Please invoke PDFNet Initialize function before calling other functions.

Which implies the exception didn’t occur at the PDFDoc() constructor, but at the end when Terminate was called. Could you please clarify.

Does the issue happen if you run one of our SDK samples unmodified?

If not, then for me to investigate further please provide me a minimal, reproducible, sample. Ideally one of our SDK samples modified.

1 Like