"Appdending one document to another document throws ""insufficient data written"" error."

We are receiving an error trying to append a .doc file onto another document. These same files work when using PDF.

The URI is:
http://api.saaspose.com/v1.0/words/Quotation Details.296f4470-2c63-409b-9f55-408b9b5166f5.cc9ec59e-289b-4ab6-ae9d-771b1f44f284.doc/appendDocument

The other document being appended is:
Quotation Summary.9f001034-2f1f-4a20-b0ae-28b99b704915.3cabdcb4-9b8a-4a0b-b41a-96cf1a796bb1.doc

We are using SDK com.saaspose.words.Document, append method.

Error:
21:25:24,646 ERROR [Utils] Error processing command
java.io.IOException: insufficient data written
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:2826)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1152)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
at java.net.HttpURLConnection.getResponseMessage(HttpURLConnection.java:445)
at com.saaspose.common.Utils.processCommand(Utils.java:226)
at com.saaspose.words.Document.appendDocument(Document.java:322)
This was created with a trial account, [Tate4.Alfstad4@fpx.com](mailto:Tate4.Alfstad4@fpx.com).

Please advice.

Hi Gloria,

I was unable to reproduce exactly the same error but there was a bug in AppendDocument method and it was not merging the documents from root folder. This method has now been updated; please get the latest version of com.saaspose.words.Document from GitHub - aspose-words-cloud/aspose-words-cloud-java: A Java library for communicating with the Aspose.Words Cloud API and the try again.

I have used the following code to merge documents:

  1. For root folder
SaasposeApp.setAppKey("89");
SaasposeApp.setAppSID("77");
// specify product URI
Product.setBaseProductUri("[http://api.saaspose.com/v1.0"](http://api.saaspose.com/v1.0));

try {
String mainDocument = "C:\\MainDocument.doc";
String appendDocument1 = "C:\\AppendDocument1.doc";
String appendDocument2 = "C:\\AppendDocument2.doc";

com.saaspose.storage.Folder folder = new Folder();
folder.UploadFile(mainDocument, "");
folder.UploadFile(appendDocument1, "");
folder.UploadFile(appendDocument2, "");

String[] appendDocs = {"AppendDocument1.doc","AppendDocument2.doc"};
String[] importFormatModes = { "KeepSourceFormatting", "UseDestinationStyles" };
com.saaspose.words.Document document =new com.saaspose.words.Document(new File(mainDocument).getName());
document.AppendDocument(appendDocs, importFormatModes, "");

InputStream responseStream = folder.GetFile(new File(mainDocument).getName());
Folder.SaveStreamToFile("C:\\MergedDocument.doc", responseStream);
responseStream.close();

System.out.println("Done");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  1. If documents are not at the root folder (or in specific folders/sub-folders)
SaasposeApp.setAppKey("89");
SaasposeApp.setAppSID("77");
// specify product URI
Product.setBaseProductUri("[http://api.saaspose.com/v1.0"](http://api.saaspose.com/v1.0));

try {
String mainDocument = "C:\\MainDocument.doc";
String appendDocument1 = "C:\\AppendDocument1.doc";
String appendDocument2 = "C:\\AppendDocument2.doc";

com.saaspose.storage.Folder folder = new Folder();
folder.UploadFile(mainDocument, "Folder1");
folder.UploadFile(appendDocument1, "Folder1");
folder.UploadFile(appendDocument2, "Folder1");

String[] appendDocs = {"AppendDocument1.doc","AppendDocument2.doc"};
String[] importFormatModes = { "KeepSourceFormatting", "UseDestinationStyles" };
com.saaspose.words.Document document =new com.saaspose.words.Document(new File(mainDocument).getName());
document.AppendDocument(appendDocs, importFormatModes, "Folder1");

InputStream responseStream = folder.GetFile("Folder1/" + new File(mainDocument).getName());
Folder.SaveStreamToFile("C:\\MergedDocument.doc", responseStream);
responseStream.close();

System.out.println("Done");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Please try this code and let us know if it works for you; in case it does not work, please share your complete code and sample documents for further analysis.

Best Regards,
Muhammad Ijaz
Support Developer, Saaspose Sialkot Team
http://www.saaspose.com

Same results. Please try to recreate with attached files.

Hi Gloria,

Sorry, I was still unable to reproduce the issue. I get the attached output and following is my complete code:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.saaspose.common.Product;
import com.saaspose.common.SaasposeApp;
import com.saaspose.storage.Folder;

public class MergeDocuments {
public static void main(String[] args) throws IOException {
SaasposeApp.setAppKey("89**********");
SaasposeApp.setAppSID("77************");
// specify product URI
Product.setBaseProductUri("[http://api.saaspose.com/v1.0"](http://api.saaspose.com/v1.0));

try {
String mainDocument = "d:\\CoverPage.doc";
String appendDocument = "d:\\OpportunitySummary.doc";

com.saaspose.storage.Folder folder = new Folder();
folder.UploadFile(mainDocument, "");
folder.UploadFile(appendDocument, "");

String[] appendDocs = {"OpportunitySummary.doc"};
String[] importFormatModes = { "KeepSourceFormatting" };
com.saaspose.words.Document document =new com.saaspose.words.Document(new File(mainDocument).getName());
document.AppendDocument(appendDocs, importFormatModes, "");

InputStream responseStream = folder.GetFile(new File(mainDocument).getName());
Folder.SaveStreamToFile("d:\\MergedDocument.doc", responseStream);
responseStream.close();

System.out.println("Done");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

Can you please share your App SID and App Key with complete code for further analysis?

Best Regards,
Muhammad Ijaz
Support Developer, Saaspose Sialkot Team
http://www.saaspose.com

Hi Gloria,

Was this issue resolved at your end? As our support team was unable to reproduce this issue, if it's still happening at your end then could you please respond to the following question from our team?

"Can you please share your App SID and App Key with complete code for further analysis? "

We'll further investigate it and guide you accordingly.

Thanks & Regards
Shahzad Latif
Saaspose Support Team.

I have not been able to reproduce. You can close the support request. Thanks.