How to convert Microsoft Word Document to PDF with Java using Aspose.Words REST API

String appSID = "***********************************************";
String appKey = "****************************************";
String fileName = "chinainformation.doc";
//build URI to convert MS Word file to other format using storage
try
{
String strURI = "http://api.aspose.com/v1.1/words/" + fileName + "?format=pdf";

//Use the following line to use API version 1.1
//String strURI = "http://api.aspose.com/v1.1/words/" + fileName + "?format=pdf";

//sign URI
String signedURI = Sign(strURI, appSID, appKey);
InputStream responseStream = ProcessCommand(signedURI, "GET");

boolean response = SaveStreamToFile("c:\\verbingProjects\\MyFile.pdf", responseStream);
responseStream.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}


i get following error::

Forbidden
java.io.IOException: Server returned HTTP response code: 403 for URL: http://api.aspose.com/v1.1/words/chinainformation.doc?format=pdf&appSID=*****************************&signature=****************************************
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1675)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1673)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1671)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1244)
at com.aspose.cloud.common.Utils.ProcessCommand(Utils.java:297)
at aposeword.AposeWord.main(AposeWord.java:38)
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://api.aspose.com/v1.1/words/chinainformation.doc?format=pdf&appSID=*****************************signature=****************************************
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at java.net.HttpURLConnection.getResponseMessage(HttpURLConnection.java:534)
at com.aspose.cloud.common.Utils.ProcessCommand(Utils.java:295)
... 1 more

Hi Hashmat,


Thanks for your inquiry. Please attach your input Word document here for testing. We will investigate the issue on our end and provide you more information.

Best regards,

Hi Hashmat,

Your App SID looks incorrect in the URI. Correct App SID format is '7******-1***-4***-a***-80*********' but I cannot see any hyphens in your App SID. Please use correct App SID and let us know if it works for you.

Also, please remove some part of App SID and signature from your post and always replace some part of App SID and signature with (*) or any other character when sharing in a public post.

Best Regards,

@hashmatkhattak

Please note the latest API Version V4.0 uses the JSON Web Token authentication method instead of OAuthe2.0 and URL signing. Please check the updated Aspose.Words Cloud SDK for Java for more details.

WordsApi wordsApi = new WordsApi(new ApiClient());
        ApiClient client = wordsApi.getApiClient();
        client.setAppKey(AppKey).setAppSid(AppSID);

        try {
            String fileName = "test_multi_pages.docx";
            String format = "pdf";

            // Upload original document to Cloud Storage
            wordsApi.uploadFile(new UploadFileRequest(new File("src/main/resources/" + fileName), "", Constants.MYStorage));

            ConvertDocumentRequest request = new ConvertDocumentRequest(new File("src/main/resources/" + fileName), format,
                    Constants.MYStorage, null, null, null);
            File result = wordsApi.convertDocument(request);

        } catch (
                Exception e) {
            System.out.println(e.getMessage());
        }