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.

Sat Aug 14, 2021 10:55 am

I am developing an spring boot web application (Java) that automates document generation. One issue that I am facing is I have a a word document template in the database and when a user wishes to generate a document, I want to create a new document using that stored template. So Is there a way to generate a word document from a template whose bytes are stored in the database. Also storing I wish to store this document back to the database by getting it bytes.

Any help would be appreciated.

Tabish58004502
 
Posts: 2
Joined: Fri Aug 13, 2021 10:50 pm

Mon Aug 16, 2021 9:00 am

Hello,

Thank you for your inquiry.
Our Spire.Doc provides loadFromStream and saveToStream methods which allow loading file bytes from database and saving file bytes to the database. Please refer to the following code.
Code: Select all
    //Bytes array from the database
    public static byte[] toDoc(byte[] buff){
        //Converts bytes into a stream
        InputStream inputStream = new ByteArrayInputStream(buff);
        Document doc = new Document();
        //Load stream file
        doc.loadFromStream(inputStream, FileFormat.Docx);

        //...

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //Save as a stream file
        doc.saveToStream(bos,FileFormat.Docx);

        //Returns an array of bytes for the stream file
        return  bos.toByteArray();
    }

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Aug 19, 2021 8:18 am

Hi,

Hope you are doing well!
Did the code we provided work for you? Any feedback will be greatly appreciated.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Aug 19, 2021 9:23 pm

Hi

Thank you for the reply.

I manage to make it work. Had to adjust few things but at the end it

Thanks again.

Tabish58004502
 
Posts: 2
Joined: Fri Aug 13, 2021 10:50 pm

Fri Aug 20, 2021 1:54 am

Hi,

You're welcome.
Glad to hear that. If you have other questions about using Spire.Doc in the future, please feel free to contact us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc

cron