We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Receiving Word Document as Base64-encoded string, attempting to open as WordDocument

Hello,

I have a distributed PDF Web Service that takes in a Word Document as Base 64 encoded string.  I want to convert that to a PDF.

My code:

WordDocument document = new WordDocument(new MemoryStream(Convert.FromBase64String(value)));
logger.Info("Created Word Document");
DocToPDFConverter conv = new DocToPDFConverter();
//Converts the word document to pdf
pdf = conv.ConvertToPDF(document);
stream = new MemoryStream();
pdf.Save(stream);
stream.Position = 0;
pdf.Close();
document.Close();
//Returns the stream data as Base 64 string
string returnData = Convert.ToBase64String(stream.ToArray());

For some reason, I get an error on the construction of the Word Document:

2017-06-22 14:24:53,440 [1] ERROR GeneratePdf_WCFService.Service1 - System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: value
   at System.IO.MemoryStream.set_Position(Int64 value)
   at Syncfusion.Compression.Zip.ZipArchive.Open(Stream stream, Boolean closeStream)
   at Syncfusion.DocIO.DLS.Convertors.DocxParser.Read(Stream docStream, WordDocument document)
   at Syncfusion.DocIO.DLS.WordDocument.OpenDocx(Stream stream)
   at Syncfusion.DocIO.DLS.WordDocument.OpenInternal(Stream stream, FormatType formatType, String password)
   at GeneratePdf_WCFService.Service1.GeneratePdf(String value, String type, Str

Anyone ever seen this before?  Any ideas or suggestions?

Thanks,

Philip


5 Replies

VR Vijay Ramachandran Syncfusion Team June 23, 2017 11:26 AM UTC

Hi Philip,

Thank you for contacting Syncfusion support.

We are unable to reproduce the reported ArgumentOutOfRangeException issue with the given code snippet. So, kindly provide us input Word document or sample to reproduce the same issue at our end. Thereby we will analyze further and provide you a prompt solution at the earliest.

Let us know if you have any concern.

Regards,
Vijay R
 



PT Philip Tenn June 24, 2017 02:56 PM UTC

I resolved this.  It was bad code on my end.  I was doing an additional UTF8 encoding of the content that corrupted the Docx file prior to sending the Base64 encoded string to the distributed PDF Conversion Service. 

BAD:                    

    byte[] bytes = Encoding.UTF8.GetBytes(webClient.DownloadString(blobInfo.BlobUri));                    

    base64 = Convert.ToBase64String(bytes);

FIXED:                    

    byte[] bytes = webClient.DownloadData(blobInfo.BlobUri);                    

    base64 = Convert.ToBase64String(bytes);




VR Vijay Ramachandran Syncfusion Team June 26, 2017 04:44 AM UTC

Hi Philip,

Thank you for your update.

We are happy to hear that your problem has been resolved. Please let us know if you have any other questions. We will be happy to assist you as always.

Regards,
Vijay R
 



NK Nusrat Kabir July 22, 2019 11:35 AM UTC

How can I display doc file using the same format as I have used to display pdf?
public ActionResult ShowFile(int id)
      {
          var pictureInDb = _context.FileDetails.FirstOrDefault(c => c.FileDetailId == id); 
      
          byte[] array;
          array = pictureInDb.image;
          var strBase64 = Convert.ToBase64String(array);
          var viewModel = new FileBinderWithStuff();
          if (pictureInDb.Extension == ".jpg")
          {
               viewModel = new FileBinderWithStuff
              {
                  Url = string.Format("data:image/jpg;base64,{0}", strBase64),
                  Extension = pictureInDb.Extension
              };
          }
         
          else
          {
              
               viewModel = new FileBinderWithStuff
              {
                  Url = string.Format("data:Application/pdf;base64,{0}", strBase64),
                  Extension = pictureInDb.Extension
              };
 
          }
         
          return PartialView("_ShowFile", viewModel);
          
      }


PR Poorani Rajendran Syncfusion Team July 24, 2019 06:58 AM UTC

Hi Nusrat,

Thank you for contacting Syncfusion support.

Regarding How can I display doc file using the same format as I have used to display pdf?

Could you please confirm us whether you need to display DOC format file in viewer or need to generate DOC format Word document. From the given details, we have not found the complete details of your end requirement. So, could you please share us the more details on your requirement, which will be more helpful to check at our end and will provide you the appropriate solution at the earliest.

If you wish to generate DOC Word document programmatically, we suggest you to refer the below documentation link to know more about generating Word document using Essential DocIO.
https://help.syncfusion.com/file-formats/docio/word-file-formats#word-binary-97-2003-format

Please let me know if you have any other questions.


Regards,
Poorani Rajendran


Loader.
Up arrow icon