We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

FileNotFoundException error trying to import XML into a PDF

Hi, I’m trying to get started with Aspose.PDF in Java.

I am following this tutorial: https://www.youtube.com/watch?v=w_fLxNqNAZ0
But using this code: https://docs.aspose.com/pages/viewpage.action?pageId=7405624#ImportDataintoaPDFFile(facades)-ImportDatafromXMLintoaPDFFile(facades)

I am trying to open a PDF template file and import an XML file into it and output the new and filled PDF file.

This is my code:

package com.company;

import java.io.FileInputStream;
import com.aspose.pdf.*;

public class Main {

    public static void main(String[] args) {
	// write your code here
        // For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Java
        // Open document
        com.aspose.pdf.facades.Form form = new com.aspose.pdf.facades.Form();
        form.bindPdf("C:/Users/Coto/Desktop/importxml/mq_template.pdf");
        // Open XML file.
        java.io.FileInputStream xmlInputStream = new FileInputStream("C://Users//Coto//Desktop//importxml//C-385180_Monitoring_questions_20180822_HOR_Final.docx_XML.xml");
        // Import XML data
        form.importXml(xmlInputStream);
        // close file stream
        xmlInputStream.close();
        // Save updated PDF
        form.save("output.pdf");
        // dispose the form object
        form.dispose();
    }
}

However, I get this error: https://i.gyazo.com/1adebb3f42940c1a549fed7c5330ad2f.png

Is there anything wrong with my code?

@Coto16

Thanks for contacting support.

The exceptions which you are facing seem general Java IDE generated. You need to enclose your code snippet in Try block and use Catch to handle FileNotFoundException and IOException. You may please surround your code with Try/Catch block and in case you still face any issue related to Aspose.PDF, please share your sample PDF and XML files with us. We will test the scenario in our environment and address it accordingly.

@asad.ali

I tried what you told me.

This is my new code:

package com.company;

import java.io.FileNotFoundException;
import java.io.FileInputStream;
import com.aspose.pdf.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
	// write your code here
        // For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Java
        // Open document
        com.aspose.pdf.facades.Form form = new com.aspose.pdf.facades.Form();
        form.bindPdf("C:/Users/Coto/Desktop/importxml/mq_template.pdf");
        try {
            // Open XML file.
            java.io.FileInputStream xmlInputStream = new FileInputStream("C:/Users/Coto/Desktop/importxml/C-385180_Monitoring_questions_20180822_HOR_Final.docx_XML.xml");
            // Import XML data
            form.importXml(xmlInputStream);
            // close file stream
            xmlInputStream.close();
            // Save updated PDF
            form.save("C:/Users/Coto/Desktop/importxml/output.pdf");
            // dispose the form object
            form.dispose();
        }
        catch(FileNotFoundException ex) {
            System.out.println("File wasnt found");
        }
        catch(IOException ex) {
            System.out.println("Cant read");
        }
    }
}

And the code finishes successfully with exit code 0.I check and a new file “output.pdf” has been created, however, it’s as if it didn’t import the XML file at all. When I import the XML using Adobe Acrobat XI Pro, it works fine, but not using the above script…

My PDF template file: https://cdn.discordapp.com/attachments/299995740711682058/522817436622258176/mq_template.pdf

My XML file: https://cdn.discordapp.com/attachments/299995740711682058/522817444860133386/C-385180_Monitoring_questions_20180822_HOR_Final.docx_XML.xml

Normally, the forms should be filled with text, but none of this appears to happen - it just creates a new output.pdf file that looks more like mq_template.pdf.

@Coto16

Thanks for getting back to us.

We were able to replicate the issue in our environment using Aspose.PDF for Java 18.11 and logged it as PDFJAVA-38237 in our issue management system for the sake of correction. We will further look into details of the issue and keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.

Please inform me of any new information regarding the issue.

@Coto16

Thanks for your inquiry.

I regret to inform that issue has not been investigated yet due to low priority and large queue of pending issues logged prior to it. However, we will surely let you know once we make some significant progress towards issue resolution. Please spare us little time.

We are sorry for the inconvenience.