You are here

You are here

Download Document - PDF

Downloading a document as a PDF from SpringCM is nearly identical to downloading the document in its native format.  There are 2 notable changes that are made to get the PDF rendition of a document, assuming that it is a file type that SpringCM will generate a PDF for (Office documents, text documents, etc.)  The 2 changes are:

  1. The DownloadFormat enum parameter passed to the DocumentDownload method is changed from Native to PDF.
  2. The PDFFileSize property used on the SCMDocument is used instead of the FileSize parameter.

 

Vertical Tabs

c#
string documentId = "<Document Id retrieved by other method calls>";
string localPathAndFileName = "<Local file system path and file name for downloaded file.>";
 
//First get the document, we need to do this to know the full file size 
//We can pass false for the last parameter since we won't need metadata 
SCMDocument document = springCMService.DocumentGetById(token, documentId, false);
 
//Create an in memory stream to write the downloaded document 
using (FileStream stream = new FileStream(localPathAndFileName, FileMode.Create))
{
 
	//Initialize the download variables 
	//position keeps track of where we are in downloading the file 
	//chunk size is how many bytes we ask SpringCM for at a time, in this 
	//case 1MB. 
	int position = 0;
	int chunkSize = 1048576;
 
	//The position increases until we have all the bytes 
	//so loop while there is still more to download 
	while (position < document.PDFFileSize)
	{
		//The final chunk will likely be smaller than the chunksize 
		//so we check if the next 1MB chunk would take us past the 
		//bytes left to be downloaded. If this is the case, 
		//we change the chunksize to the final set of the bytes 
		//We have to check everytime in this loop to see when we get 
		//to the end. 
		if (position + chunkSize >= document.PDFFileSize)
		{
			chunkSize = (int) (document.PDFFileSize - position);
		}
 
		//Get the current chunk, and then write it to the stream 
		byte[] currentChunk = springCMService.DocumentDownload(token, document.Id, position, chunkSize, DownloadFormat.PDF);
		stream.Write(currentChunk, 0, chunkSize);
 
		//Move the position to be ready for the next chunk 
		position += chunkSize;
	}
}
java
String documentId = "<Document Id retrieved by other method calls>";
String localPathAndFileName = "<Local file system path and file name for downloaded file.>";
 
//First get the document, we need to do this to know the full file size
//We can pass false for the last parameter since we won't need metadata
SCMDocument document = springCMService.documentGetById(token, documentId, false);
 
//Initialize the download variables
//position keeps track of where we are in downloading the file
//chunk size is how many bytes we ask SpringCM for at a time, in this
//case 1MB.  
int position = 0;
int chunkSize = 1048576;
 
//The position increases until we have all the bytes
//so loop while there is still more to download
ByteBuffer downloadedDocument = ByteBuffer.allocate(document.getPDFFileSize().intValue());
 
while (position < document.getPDFFileSize())
{
	//The final chunk will likely be smaller than the chunksize
	//so we check if the next 1MB chunk would take us past the
	//bytes left to be downloaded.  If this is the case,
	//we change the chunksize to the final set of the bytes
	//We have to check everytime in this loop to see when we get
	//to the end.
	if (position + chunkSize >= document.getPDFFileSize())
	{
		chunkSize = (int)(document.getPDFFileSize() - position);
	}
 
	//Get the current chunk, and then write it to the stream
	byte[] currentChunk = springCMService.documentDownload(token, document.getId(), position, chunkSize,DownloadFormat.PDF);
	downloadedDocument.put(currentChunk, 0, chunkSize);
 
	//Move the position to be ready for the next chunk
	position += chunkSize;
 
}
 
FileOutputStream fileOutputStream = new FileOutputStream(localPathAndFileName);	   
fileOutputStream.write(downloadedDocument.array());
fileOutputStream.close();