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 12, 2022 8:32 pm

Hello guys and girls,
I have an issue, and I hope you can help me.
I made spring boot app, where I have one word template, where I put some data from app. I used this two lines to save document:
//this line works
document.saveToFile("C:\\\\Users\\\\Korisnik\\FilledTemplat.docx");

//this line does not work
document.saveToFile("FilledTemplat.docx", FileFormat.Docx);

What bothers me is the fact that there is not any notification when I saved that file, and I would like to add either save as dialog, or at least notification that it's saved to downloads (notification that shows at the bottom of screen in chrome).

Thanks in advance :)

CrazyTolstoy
 
Posts: 3
Joined: Tue Apr 12, 2022 8:22 pm

Wed Apr 13, 2022 10:32 am

Hi,

Thanks for your inquiry.
The code document.saveToFile("FilledTemplat.docx", FileFormat.Docx); would save the document in default path of your application. Our Spire.Doc is the SDK used in backend, so it doesn't provide the dialog which allows user to select path. We provide the following method to save file to stream, you could download the stream by browser (HttpServletResponse).
Code: Select all
        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
        document.saveToStream(outputStream,FileFormat.Docx);


Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Wed Apr 13, 2022 9:06 pm

Thanks for your fast response <3

Since I'm new to spring boot and java, could you be a bit more specific on using bytearrayoutputstream with spire?
I tried to use code you gave me, but it does nothing :/

Best regards!

CrazyTolstoy
 
Posts: 3
Joined: Tue Apr 12, 2022 8:22 pm

Thu Apr 14, 2022 8:05 am

Hi,

Thank you for your reply,
Actually, writing the stream in browser by HttpServletResponse is not part of the functionality of our product. Here I provided the sample code for your reference. I suggest that you find more comprehensive knowledge about HttpServletResponse online.
Code: Select all
@RequestMapping("/download")
    public void download(HttpServletResponse response) {
        try {
            Document document=new Document();
            Section section=document.addSection();
            section.addParagraph().appendText("This is a test");
            ByteArrayOutputStream stream=new ByteArrayOutputStream();
            document.saveToStream(stream, FileFormat.Docx);

            byte[] bytes = stream.toByteArray();
            String fileName = "res.docx";
            response.reset();
            response.setContentType("application/msword;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename= "+ fileName);
            response.getOutputStream().write(bytes);
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Fri Apr 15, 2022 6:37 pm

Thank you very much, you really helped me! <3

CrazyTolstoy
 
Posts: 3
Joined: Tue Apr 12, 2022 8:22 pm

Mon Apr 18, 2022 3:43 am

Hi,

You're welcome.
If you need assitsance in the future, please feel free to contact us.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.Doc