You are here

You are here

Joining Multiple Documents into a Single Document

The SpringCM API exposes methods that will let you combine up to 200 documents into a single PDF document.  The source documents do not have to be PDF's, but they must be of a document type that SpringCM creates a PDF rendition for.  The call to combine documents is an asynchronous operation that is initiated by the DocumentMerge method.  Its progress is tracked with the DocumentMergeCheck method.  When the merge completes successfully, the new document can be downloaded with the DocumentMergeDownload method.  The final document cannot be directly stored in SpringCM, it must be downloaded.  

Do not confuse the DocumentMerge method, which combines documents together, with the DocumentXMLMerge method, which creates a new document based on an XML datasource and merge template.

Vertical Tabs

c#
string[] documentIds = "<Array of document id's to combine>";
string localPathAndFileName = "<Local file system path and file name for downloaded file.  File name should have the .pdf extension>";
 
DocumentMergeResult mergeResult = springCMService.DocumentMerge(token, documentIds);
 
//Loop until the merge request is ready or stopped for an error condition
while (!mergeResult.IsReady && !mergeResult.StoppedProcessing)
{
	Thread.Sleep(3000);
 
	//The API supports checking multiple merge requests at once
	//Here we only work with one request and response
	DocumentMergeResult[] mergeResults = springCMService.DocumentMergeCheck(token, new DocumentMergeResult[] {mergeResult});
	mergeResult = mergeResults[0];
}
 
//If the merge is successful, we can download the document
//otherwise we check the error message
if(mergeResult.IsReady)
{
	int index = 0;
	using (var fs = new FileStream(localPathAndFileName, FileMode.OpenOrCreate))
	{
		while (index <= mergeResult.MaxIndex)
		{
			byte[] bytes = springCMService.DocumentMergeDownload(token, mergeResult, index);
			fs.Write(bytes, 0, bytes.Length);
			index++;
		}
	}
}
else
{
	Console.WriteLine("Merge could not finish for the following reason: {0}",mergeResult.StopReason);
}
java
String[] documentIds = "<Array of document id's to combine>";
String localPathAndFileName = "<Local file system path and file name for downloaded file.  File name should have the .pdf extension>";
 
DocumentMergeResult mergeResult = springCMService.documentMerge(token, documentIds);
 
//Loop until the merge request is ready or stopped for an error condition
while (!mergeResult.isIsReady() && !mergeResult.isStoppedProcessing())
{
	Thread.sleep(3000);
 
	//The API supports checking multiple merge requests at once
	//Here we only work with one request and response
	DocumentMergeResult[] mergeResults = springCMService.documentMergeCheck(token, new DocumentMergeResult[] {mergeResult});
	mergeResult = mergeResults[0];
}
 
//If the merge is successful, we can download the document
//otherwise we check the error message
if(mergeResult.isIsReady())
{
	int index = 0;
	FileOutputStream fileOutputStream = null;
	try
	{
		fileOutputStream = new FileOutputStream(localPathAndFileName);	   
		while (index <= mergeResult.getMaxIndex())
		{
			byte[] bytes = springCMService.documentMergeDownload(token, mergeResult, index);
			fileOutputStream.write(bytes);
			index++;
		}
	}
	finally
	{
		fileOutputStream.close();
	}
}
else
{
	System.out.format("Merge could not finish for the following reason: %s\n",mergeResult.getStopReason());
}