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.

Mon Aug 25, 2014 11:48 am

Hi

I have done extensive research but could not find any suitable answer to my problem on this forum or any other forums.

Will Spire.doc for .Net support loading and saving a word document from and to Byte Array? From the application that we are currently designing, the user should be able to open a previously saved document from Byte Array, edit/modify the content and save/write it back as Byte Array. If this is possible, would you be kind enough to give us a code sample?

Regards
SSP

ssponnada
 
Posts: 8
Joined: Wed Jul 17, 2013 3:14 pm

Tue Aug 26, 2014 2:35 am

Dear ssponnada,

Thanks for your inquiry.

For your requirement, please refer to the following code:
Code: Select all
//a saved document from byte array
 byte[] fromArray = File.ReadAllBytes("Starbound.doc");
 using (MemoryStream ms1 = new MemoryStream(fromArray))
 {
     Document doc = new Document(ms1);
     //here you can edit/modify the document
     //for instance,replace "on" with "upon"
     doc.Replace("on", "upon", false, true);
     using (MemoryStream ms2 = new MemoryStream())
     {
         doc.SaveToStream(ms2, FileFormat.Doc);
         //save to byte array
         byte[] toArray = ms2.ToArray();
     }
 }

Please feel free to contact us if you have any problems.

Best regards,
Burning
E-iceblue support team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Tue Aug 26, 2014 11:55 am

Dear Burning

Many thanks for your response. Unfortunately, this did not work for us.

Just to give you a bit more clarity, we are looking to open a pre-existing document, edit the content and when saved, write it back as byte array to the same database/table/column.

Grateful for any help.

Regards
SSP

ssponnada
 
Posts: 8
Joined: Wed Jul 17, 2013 3:14 pm

Wed Aug 27, 2014 3:37 am

Dear ssponnada,

Thanks for your inquiry.

I guess you want to read a byte array (a word document) from database, edit the content and write it as byte array to database when saved.
If so, please refer to the following code:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

using Spire.Doc.Documents;
using Spire.Doc;


namespace SALESSUPPORT_3401
{
    class Program
    {
        private static string connectionString=@"Data Source=PGIIBG4EDKUP9ZT\SQLEXPRESS;Initial Catalog=SalesSupport;Integrated Security=SSPI";

        static void Main(string[] args)
        {
            //Get data from database
            byte[] original = GetFileData(1);
            byte[] result;
            using (MemoryStream ms1 = new MemoryStream(original))
            {
                //Load data with MemoryStream
                Document doc = new Document(ms1);
                //Now you can edit the document
                //For example, replace "on" with "upon"
                doc.Replace("on", "upon", false, true);
                //Save doc as byte[]
                using (MemoryStream ms2 = new MemoryStream())
                {
                    doc.SaveToStream(ms2, FileFormat.Doc);
                    result = ms2.ToArray();
                }
            }
            //Update file data to database
            if (UpdateFileData(result, 1))
            {
                Console.WriteLine("OK");
            }
            else
            {
                Console.WriteLine("Wrong");
            }
            //Check out if the document is correct
            using (MemoryStream ms3 = new MemoryStream(result))
            {
                Document doc = new Document(ms3, FileFormat.Doc);
                doc.SaveToFile("Result.doc", FileFormat.Doc);
                Process.Start("Result.doc");
            }
        }

        //Convert file to byte[]
        static byte[] FileToBytes(string fileName)
        {
            FileStream fs = File.OpenRead(fileName);
            byte[] fileData = new byte[fs.Length];
            fs.Read(fileData, 0, (int)fs.Length);
            return fileData;
        }

        //Save data to database
        static bool UpdateFileData(byte[] fileData,int id)
        {
            string sql = "UPDATE Table3401 SET FileByteArray=@FileByteArray WHERE ID=@ID";
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.Add(new SqlParameter("@FileByteArray", SqlDbType.Image));
                    cmd.Parameters["@FileByteArray"].Value = fileData;
                    cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
                    cmd.Parameters["@ID"].Value = id;
                    return cmd.ExecuteNonQuery() > 0 ? true : false;
                }
            }
        }

        //Get data from database
        static byte[] GetFileData(int id)
        {
            string sql = "SELECT FileByteArray FROM Table3401 WHERE ID=@ID";
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
                    cmd.Parameters["@ID"].Value = id;
                    object result = cmd.ExecuteScalar();
                    if (null == result)
                    {
                        return new byte[0];
                    }
                    return (byte[])result;
                }
            }
        }
    }
}

Please feel free to contact us if you have any problems.

Best regards,
Burning
E-iceblue support team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Wed Aug 27, 2014 10:45 am

HI

Thank you again for your help. Unfortunately, the method you suggested will programatically replace the updated content of a document and write it back to the database. What I am after is something in the line of "BeforeSaveEvent" or "AfterSaveEvent".

Right now, we can open the document in MS Word 2003 from Byte Array and save it in a temporary location in a local folder. When the document is closed, MS Word will obviously ask to Save before closing or lose changes. I now need that document to be written back to the SQL table in Byte Array immediately before or after saving the document in order to save all changes made to the document.

As always, your time and efforts are much appreciated.

Regards
SSP

ssponnada
 
Posts: 8
Joined: Wed Jul 17, 2013 3:14 pm

Thu Aug 28, 2014 8:48 am

Dear ssponnada,

Thanks for your inquiry.

I'm sorry that Spire.Doc doesn't support this feature.

Please feel free to contact us if you have any problems.

Best regards,
Burning
E-iceblue support team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Thu Aug 28, 2014 1:51 pm

No worries. Thanks again for your efforts.

Regards
SSP

ssponnada
 
Posts: 8
Joined: Wed Jul 17, 2013 3:14 pm

Fri Aug 29, 2014 1:41 am

You're welcome.
Please feel free to contact us if you have any problems.

Best regards,
Burning
E-iceblue Support Team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Fri Nov 11, 2016 3:13 pm

Dear All,

I have implemented the code here above provided by "Burning" on Tue Aug 26, 2014 2:35 am, but impossible to get rid of the error "Can't open storage on LockBytes" when loading the data (a Word document as an OLE object) with MemoryStream (Document doc = new Document(ms1);).

Thanks in advance for your assistance.
Here is my code:

Code: Select all
using System.IO;
using System.Data;
using System.Data.OleDb;
using Spire.Doc;

namespace ConsoleApplication2
{
    class Program
    {
        private static string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\paroz\Documents\_DEV\APL database\APL_Std new report VP.accdb";

        static void Main(string[] args)
        {
            //Get data from database
            byte[] original = GetFileData(6);
            using (MemoryStream ms1 = new MemoryStream(original))
            {
                //Load data with MemoryStream
                Document doc = new Document(ms1);
            }
        }

        //Get data from database
        static byte[] GetFileData(int id)
        {
            string sql = "SELECT CoPolarFormulas FROM APL_List WHERE AP_ID = @ID";

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    cmd.Parameters.Add(new OleDbParameter("@ID", SqlDbType.Int));
                    cmd.Parameters["@ID"].Value = id;
                    object result = cmd.ExecuteScalar();
                    if (null == result)
                    {
                        return new byte[0];
                    }
                    return (byte[])result;
                }
            }
        }
    }
}

vparoz
 
Posts: 2
Joined: Fri Nov 11, 2016 10:36 am

Mon Nov 14, 2016 3:08 am

Dear vparoz,

Thanks for the information.
If you are using older version of Spire.Doc, we first suggest upgrading to the latest Spire.Doc Pack(hot fix) Version:5.8.13. After trying this version, if the problem persists, please provide us the sample file so that we can test it on our side.

Thanks,
Betsy
E-iceblue support team
User avatar

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

Mon Nov 14, 2016 12:32 pm

Dear Betsy,

I am using the latest version 5.8.13 on a 64-bit Windows 7.
Please find here attached a RAR archive containing:
- the Program.cs file
- the MS Access database (test.accdb) containing the Word document (stored as an OLE object) that I want to load

Thanks in advance for your support.

Kind regards,
Vincent

vparoz
 
Posts: 2
Joined: Fri Nov 11, 2016 10:36 am

Tue Nov 15, 2016 6:22 am

Dear Vincent,

Thanks for the files.
I have noticed the issue and posted it to our Dev team. We will inform you once there is any progress.
Sorry for inconvenience.

Sincerely,
Betsy
E-iceblue support team
User avatar

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

Wed Nov 16, 2016 9:19 am

Dear Vincent,

By investigation, we found that the stored bytes in Access database are not word file bytes, it is OLE object bytes contains other bytes. You need to get the word file byte from OLE object byte and then load it by our product.
Here is a guide for your kind reference.
https://bytes.com/topic/net/answers/740 ... e-object-c

Sincerely,
Betsy
E-iceblue support team
User avatar

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

Fri Nov 18, 2016 9:12 am

Dear Vincent,

How is the issue now ? Could you please give us some feedback at your convenience ?

Sincerely,
Betsy
E-iceblue support team
User avatar

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

Return to Spire.Doc