You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,19 @@ func main() {
77
77
panic(err)
78
78
}
79
79
80
+
// read the replacement image
81
+
bs, err:= os.ReadFile("./test/cameraman.jpg")
82
+
if err != nil {
83
+
panic(err)
84
+
}
85
+
86
+
// replace the lenna image with the cameraman image
87
+
// image attributes, e.g. size, position, keep unchanged
88
+
err = doc.SetFile("word/media/image1.jpg", bs)
89
+
if err != nil {
90
+
panic(err)
91
+
}
92
+
80
93
// write out a new file
81
94
err = doc.WriteToFile("replaced.docx")
82
95
if err != nil {
@@ -98,6 +111,13 @@ Although I do not recommend to do that as the WordprocessingML spec is somewhat
98
111
99
112
But, for whatever reason there might be, you can do that.
100
113
114
+
#### Image replace
115
+
Image replacing is slightly different from text replacing. To replace an image, you need to know its path within the docx archive, rather than using a placeholder.
116
+
117
+
To find the path, use unzip or similar tools to extract the contents of a docx file, then locate the image to be replaced inside the `word/media/` folder. Assume the path is `word/media/image1.jpg`, then you can use the `SetFile()` to overwrite the old image with a new one. It should be noted that:
118
+
- The image format (encoding) should keep the same during the replacement.
119
+
- Since the metadata of the image is not changed, only the image file itself is replaced, the new image will appear in its original location, with its original size. In other words, the image attributes keep unchanged.
120
+
101
121
### ➤ Terminology
102
122
To not cause too much confusion, here is a list of terms which you might come across.
0 commit comments