Getting a stream for converted XOD document

204 views
Skip to first unread message

Anuj Agrawal

unread,
Sep 14, 2016, 5:11:31 PM9/14/16
to PDFTron PDFNet SDK
Hello,

Here's what we're trying to achieve and would appreciate any help:
Use case: Read a document from Amazon S3, convert it to XOD and save it as a separate copy on Amazon S3 server.
Can you please help me to figure out how to get a stream to be uploaded to S3 server from a converted XOD document:

GetObjectRequest sourceRequest = new GetObjectRequest
{BucketName = "BucketName",Key = "Key1"};
using (GetObjectResponse sourceResponse = client.GetObject(sourceRequest))
{
using (Stream sourceResponseStream = sourceResponse.ResponseStream)
{
PDFNet.Initialize();
if (!pdftron.PDF.Convert.Printer.IsInstalled())
{pdftron.PDF.Convert.Printer.Install();}

PDFDoc sourceDocument = new PDFDoc(sourceResponseStream);
pdftron.PDF.Convert.XODOutputOptions convertOptions = new pdftron.PDF.Convert.XODOutputOptions();
pdftron.Filters.Filter filter = pdftron.PDF.Convert.ToXod(sourceDocument, convertOptions);
PutObjectRequest destinationRequest = new PutObjectRequest
{BucketName = "BucketName",Key = "Key2"};
destinationRequest.ContentType = "application/xod";
destinationRequest.InputStream = ???

Ryan

unread,
Sep 15, 2016, 7:08:26 PM9/15/16
to pdfne...@googlegroups.com
This is a limitation of S3. AWS expects the content-length when uploading (I think because buckets can have upload size limits).


So you will have to stream into memory, and then once done, you can upload, by including the correct content length. It is not possible to know the size of the XOD beforehand.

As for general streaming upload, see the following C# code, from the WebViewerStreamingTest sample project in the .Net 4 SDK download.

In the particular case were you want to stream immediately to the user who wants to view the file, you could do both; stream to the client trying to view, and then once done, upload to S3 for future usage.

pdftron.PDF.Convert.XODOutputOptions options = new pdftron.PDF.Convert.XODOutputOptions();
options
.SetOutputThumbnails(false);
// point to the source file on disk that is to be converted, and begin the conversion
Debug.WriteLine("Prepare conversion...");
pdftron
.Filters.Filter filter = pdftron.PDF.Convert.ToXod(file, options);
// now ready to stream the document as it is converted
pdftron
.Filters.FilterReader fReader = new pdftron.Filters.FilterReader(filter);
byte[] buffer = new byte[64 * 1024]; //64 KB chunks
int bytesRead = 0;
bytesRead
= fReader.Read(buffer);
Response.BufferOutput = false;
int totalBytesSent = 0;
Debug.WriteLine("Commence streaming...");
while (bytesRead > 0) {
   
// write bytes to the response stream
   
Response.OutputStream.Write(buffer, 0, bytesRead);
   
// write to output how many bytes have been sent
    totalBytesSent
+= bytesRead;
   
Debug.WriteLine("Server sent total " + totalBytesSent + " bytes.");
   
// read next bytes
    bytesRead
= fReader.Read(buffer);
}
Debug.WriteLine("Done.");    
HttpContext.Current.ApplicationInstance.CompleteRequest();  // ensure all bytes have been sent and stop execution




Reply all
Reply to author
Forward
0 new messages