How to Merge Multiple DOCX Files into a Single Document using Go

Cloudmersive
3 min readNov 28, 2022

--

The Cloudmersive Document Conversion API endpoint offers a variety of unique, segmented services intended to help you convert, edit, and combine your numerous Office files (and other document types). With the help of our Merge Multiple DOCX API, you can easily combine up to 10 DOCX files with a single API call, dramatically simplifying the process of consolidating important business materials. Simply by copying from the complementary Go code examples provided below & registering a free account on our website (this provides a free-tier API Key with a limit of 800 API calls per month), you can easily & conveniently build this DOCX consolidation service into your application for the long run.

Structuring your call is a one-step process: simply include the below code examples in your file, and then provide the file path for each document you wish to include. After supplying your API key & calling the Document Conversion function, the API will return the encoding for your new DOCX file:

package main

import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)

func main() {

url := "https://api.cloudmersive.com/convert/merge/docx/multi"
method := "POST"

payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile1",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
file, errFile2 := os.Open("/path/to/file")
defer file.Close()
part2,
errFile2 := writer.CreateFormFile("inputFile2",filepath.Base("/path/to/file"))
_, errFile2 = io.Copy(part2, file)
if errFile2 != nil {
fmt.Println(errFile2)
return
}
file, errFile3 := os.Open("/path/to/file")
defer file.Close()
part3,
errFile3 := writer.CreateFormFile("inputFile3",filepath.Base("/path/to/file"))
_, errFile3 = io.Copy(part3, file)
if errFile3 != nil {
fmt.Println(errFile3)
return
}
file, errFile4 := os.Open("/path/to/file")
defer file.Close()
part4,
errFile4 := writer.CreateFormFile("inputFile4",filepath.Base("/path/to/file"))
_, errFile4 = io.Copy(part4, file)
if errFile4 != nil {
fmt.Println(errFile4)
return
}
file, errFile5 := os.Open("/path/to/file")
defer file.Close()
part5,
errFile5 := writer.CreateFormFile("inputFile5",filepath.Base("/path/to/file"))
_, errFile5 = io.Copy(part5, file)
if errFile5 != nil {
fmt.Println(errFile5)
return
}
file, errFile6 := os.Open("/path/to/file")
defer file.Close()
part6,
errFile6 := writer.CreateFormFile("inputFile6",filepath.Base("/path/to/file"))
_, errFile6 = io.Copy(part6, file)
if errFile6 != nil {
fmt.Println(errFile6)
return
}
file, errFile7 := os.Open("/path/to/file")
defer file.Close()
part7,
errFile7 := writer.CreateFormFile("inputFile7",filepath.Base("/path/to/file"))
_, errFile7 = io.Copy(part7, file)
if errFile7 != nil {
fmt.Println(errFile7)
return
}
file, errFile8 := os.Open("/path/to/file")
defer file.Close()
part8,
errFile8 := writer.CreateFormFile("inputFile8",filepath.Base("/path/to/file"))
_, errFile8 = io.Copy(part8, file)
if errFile8 != nil {
fmt.Println(errFile8)
return
}
file, errFile9 := os.Open("/path/to/file")
defer file.Close()
part9,
errFile9 := writer.CreateFormFile("inputFile9",filepath.Base("/path/to/file"))
_, errFile9 = io.Copy(part9, file)
if errFile9 != nil {
fmt.Println(errFile9)
return
}
file, errFile10 := os.Open("/path/to/file")
defer file.Close()
part10,
errFile10 := writer.CreateFormFile("inputFile10",filepath.Base("/path/to/file"))
_, errFile10 = io.Copy(part10, file)
if errFile10 != nil {
fmt.Println(errFile10)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}


client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)

if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")

req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

That’s all there is to it — no more code required. Easy as can be!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet