Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Apr 11, 2017 10:53 pm

Hi

I am getting an error when getting a docx attached to a Note in Dynamics CRM. The error is

An exception of type 'System.Runtime.InteropServices.ExternalException' occurred in Spire.Doc.dll but was not handled in user code
Additional information: Can't open storage on LockBytes.

Here is the code:

CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);
IOrganizationService service = crmConn.OrganizationServiceProxy;
var entNote = service.Retrieve("annotation", id, new ColumnSet(true));
if (entNote.Attributes.Contains("filename") && entNote.GetAttributeValue<string>("filename").ToLower().Contains("termsheet") && entNote.GetAttributeValue<EntityReference>("objectid").LogicalName == "esd_project")
{

var strBase64String = Convert.ToBase64String(new UnicodeEncoding().GetBytes(entNote.GetAttributeValue<string>("documentbody")));
byte[] fileContent = Convert.FromBase64String(strBase64String);

using (MemoryStream ms1 = new MemoryStream(fileContent))
{
var a = ms1.CanRead;
Document doc = new Document(ms1);
}

Please help what is wrong. I have added Sprie DLL's using Nuget in Visual Studio and the version is 5.8.76. I am using trial version and if issue is resolved would look into purchasing the product.

crysh51
 
Posts: 5
Joined: Tue Apr 11, 2017 10:37 pm

Wed Apr 12, 2017 3:51 am

Dear crysh51,

Thanks for your inquiry.
Please provide us with your strBase64String for testing on my side.

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Apr 12, 2017 5:31 pm

I have attached a text file in zip with the strbase64string.

crysh51
 
Posts: 5
Joined: Tue Apr 11, 2017 10:37 pm

Thu Apr 13, 2017 2:17 am

Dear crysh51,

Thanks for your information.
I have noticed the issue and posted it to our Dev team. Once there is any progress, we will inform you immediately.
Sorry for inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Apr 13, 2017 2:54 am

Great. Any ETA for solution?

crysh51
 
Posts: 5
Joined: Tue Apr 11, 2017 10:37 pm

Thu Apr 13, 2017 4:12 am

Dear crysh51,

After investigation, we found that Spire.Doc doesn't support loading this data at present. Could you please check the data format? It is not docx you mentioned.

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Apr 13, 2017 5:58 am

I don't know how CRM stores documents but as per MSDN article : https://msdn.microsoft.com/en-us/library/gg328429.aspx
we can download the notes attachment - docx/txt/pdf......

I need to convert the docx to pdf' inmemory' and may be you can help in providing code sample of doing the same. I am able to download docx to c:\folder and convert to pdf and upload to sharepoint but need to do that without using filesystem.

Hope you can help me and earn the business. Thanks!

crysh51
 
Posts: 5
Joined: Tue Apr 11, 2017 10:37 pm

Thu Apr 13, 2017 8:36 am

Dear crysh51,

Thanks for the information.
I suppose the issue is caused by two lines.
Code: Select all
var strBase64String = Convert.ToBase64String(new UnicodeEncoding().GetBytes(entNote.GetAttributeValue<string>("documentbody")));
byte[] fileContent = Convert.FromBase64String(strBase64String);

First please make sure if the data in 'entNote.GetAttributeValue<string>("documentbody")' needs to be converted to UnicodeEncoding. And in the MSDN article, it only uses Base64String conversion once(FromBase64String method). So I suggest changing the two lines to this line.
Code: Select all
byte[] fileContent = Convert.FromBase64String(entNote["documentbody"].ToString());

Hope this helps. If the code cannot solve the issue, please share us with the fileContent data.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Apr 13, 2017 11:29 am

In CRM, there are 2 ways of using objects Late Bind and Early Bind and they both give us same data. I am using late bing and in the code sample by MS, they are showing Early BInd. That is not the problem.

Anyways as I requested if you can provide code sample of inmemory conversion of byte[] arrary of docx to pdf, i will take care of the rest.

crysh51
 
Posts: 5
Joined: Tue Apr 11, 2017 10:37 pm

Fri Apr 14, 2017 1:58 am

Dear crysh51,

Thanks for the information.
Sorry that I forgot to provide that code, please refer to it as below.
Code: Select all
            byte[] bytes = System.IO.File.ReadAllBytes(@"F:\Sample.docx");
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                Document document = new Document();
                document.LoadFromStream(ms, Spire.Doc.FileFormat.Docx);
                document.SaveToFile("10287.pdf", FileFormat.PDF);
                //or save to pdf stream
                //document.SaveToFile(pdfStream, FileFormat.PDF);
            }

If you still have issues, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc

cron