watermark.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. ;(function () {
  2. const watermarker_name = 'WEOffice_watermarkerGroup'
  3. let waterMakerGroup = []
  4. /**
  5. * 创建水印文字
  6. */
  7. function _createTextMark(
  8. container,
  9. content,
  10. fontfamily,
  11. fontcolor,
  12. fontsize,
  13. ratation,
  14. left,
  15. top,
  16. height,
  17. width
  18. ) {
  19. let txt = container.Shapes.AddTextEffect(
  20. 0,
  21. content,
  22. fontfamily,
  23. fontsize,
  24. false,
  25. false,
  26. left,
  27. top
  28. )
  29. txt.Fill.Solid()
  30. txt.Fill.ForeColor.RGB = fontcolor
  31. txt.Line.Visible = false
  32. txt.Fill.Transparency = 0.5
  33. txt.LockAspectRatio = true
  34. if (!!height) txt.Height = height
  35. if (!!width) txt.Width = width
  36. txt.WrapFormat.AllowOverlap = true
  37. txt.Rotation = ratation
  38. txt.WrapFormat.Side = 3
  39. txt.WrapFormat.Type = 3
  40. return txt
  41. }
  42. function SetWaterMarkTest(
  43. indoc,
  44. index,
  45. content = '清样',
  46. fontfamily = '仿宋',
  47. fontcolor = 12566463,
  48. fontsize = 12,
  49. ratation = 315,
  50. viewIndex
  51. ) {
  52. let app = wps.WpsApplication()
  53. let doc = indoc || app.ActiveDocument
  54. app.ActiveWindow.ActivePane.View.SeekView = viewIndex || index
  55. let pageSetup = doc.Sections.Item(1).PageSetup
  56. let maxwidth = pageSetup.PageWidth
  57. let maxheight = pageSetup.PageHeight
  58. let left = 0,
  59. initLeft = -10,
  60. top = 0
  61. let markcontainer = doc.Sections.Item(1).Headers.Item(index)
  62. //添加水印文字
  63. let first = _createTextMark(
  64. markcontainer,
  65. content,
  66. fontfamily,
  67. fontcolor,
  68. fontsize,
  69. ratation,
  70. left,
  71. top
  72. )
  73. let unitWidth = Math.abs(Math.cos(ratation) * first.Width),
  74. unitHeight = Math.abs(Math.sin(ratation) * first.Width)
  75. //每页放置16个最多
  76. const maxCount = 4
  77. const margin = { vertical: 0, horizontal: 0 }
  78. if (maxwidth > unitWidth * maxCount)
  79. margin.horizontal = (maxwidth - unitWidth * maxCount) / (maxCount - 1)
  80. if (maxheight > unitWidth * maxCount)
  81. margin.vertical = (maxheight - unitHeight * maxCount) / (maxCount - 1)
  82. first.Delete()
  83. top = unitHeight
  84. left = initLeft
  85. let arr = []
  86. while (top < maxheight && left < maxwidth) {
  87. const txt = _createTextMark(
  88. markcontainer,
  89. content,
  90. fontfamily,
  91. fontcolor,
  92. fontsize,
  93. ratation,
  94. left,
  95. top,
  96. null,
  97. null
  98. )
  99. arr.push(txt.Name)
  100. left = left + unitWidth + margin.horizontal
  101. if (top < maxheight && left > maxwidth) {
  102. top += unitHeight + margin.vertical
  103. left = initLeft
  104. }
  105. }
  106. var waterMark = markcontainer.Shapes.Range(arr).Group()
  107. waterMark.Name = watermarker_name
  108. waterMark.Select()
  109. wps.Application.Selection.Copy()
  110. if (doc.Sections.Count > 1) {
  111. for (var i = 2; i <= doc.Sections.Count; i++) {
  112. h = doc.Sections.Item(i).Headers.Item(index)
  113. if (h.Shapes.Item(watermarker_name) == null) {
  114. wps.Application.Selection.SetRange(h.Range.Start, h.Range.Start)
  115. wps.Application.Selection.Paste()
  116. }
  117. }
  118. }
  119. waterMakerGroup[index] = waterMark
  120. app.ActiveWindow.ActivePane.View.SeekView = 0
  121. }
  122. function getGroup(indoc, headerindex) {
  123. let doc = indoc || wps.WpsApplication().ActiveDocument
  124. let header = doc.Sections.Item(headerindex).Headers.Item(headerindex)
  125. return header.Shapes.Item(watermarker_name)
  126. }
  127. function SetWaterMark(
  128. doc,
  129. content = '清样',
  130. fontfamily = '仿宋',
  131. fontsize = 12,
  132. fontcolor = 12566463,
  133. ratation = 315
  134. ) {
  135. loginfo('设置水印')
  136. //首页不同
  137. var DifferentFirstPageHeaderFooter =
  138. !!doc.PageSetup.DifferentFirstPageHeaderFooter
  139. //奇偶页不同
  140. var OddAndEvenPagesHeaderFooter =
  141. !!doc.PageSetup.OddAndEvenPagesHeaderFooter
  142. if (!DifferentFirstPageHeaderFooter && !OddAndEvenPagesHeaderFooter) {
  143. SetWaterMarkTest(
  144. doc,
  145. 2,
  146. content,
  147. fontfamily,
  148. fontcolor,
  149. fontsize,
  150. ratation,
  151. 9
  152. )
  153. } else if (OddAndEvenPagesHeaderFooter && !DifferentFirstPageHeaderFooter) {
  154. SetWaterMarkTest(
  155. doc,
  156. 1,
  157. content,
  158. fontfamily,
  159. fontcolor,
  160. fontsize,
  161. ratation
  162. )
  163. SetWaterMarkTest(
  164. doc,
  165. 3,
  166. content,
  167. fontfamily,
  168. fontcolor,
  169. fontsize,
  170. ratation
  171. )
  172. } else {
  173. SetWaterMarkTest(
  174. doc,
  175. 2,
  176. content,
  177. fontfamily,
  178. fontcolor,
  179. fontsize,
  180. ratation
  181. )
  182. var pagecount = doc.ActiveWindow.Panes.Item(1).Pages.Count
  183. if (DifferentFirstPageHeaderFooter && pagecount > 2) {
  184. SetWaterMarkTest(
  185. doc,
  186. 1,
  187. content,
  188. fontfamily,
  189. fontcolor,
  190. fontsize,
  191. ratation
  192. )
  193. }
  194. if (OddAndEvenPagesHeaderFooter && pagecount > 1) {
  195. SetWaterMarkTest(
  196. doc,
  197. 3,
  198. content,
  199. fontfamily,
  200. fontcolor,
  201. fontsize,
  202. ratation
  203. )
  204. }
  205. }
  206. wps.Application.Selection.SetRange(0, 0)
  207. wps.Application.Selection.Text = ' '
  208. wps.Application.Selection.Copy()
  209. wps.Application.Selection.Delete()
  210. loginfo('设置水印完成')
  211. }
  212. /**
  213. * 清理水印
  214. */
  215. function ClearWaterMark(indoc) {
  216. //if (waterMakerGroup) waterMakerGroup.Delete()
  217. //双保险
  218. // ;[1, 2, 3].forEach((v) => {
  219. // let max = indoc.Sections.Count
  220. // for (let i = 1; i <= max; i++) {
  221. // let group = indoc.Sections.Item(i)
  222. // .Headers.Item(v)
  223. // .Shapes.Item(watermarker_name)
  224. // if (group) {
  225. // console.log('删除水印')
  226. // group.Delete()
  227. // } else {
  228. // console.log('未找到水印')
  229. // }
  230. // }
  231. // })
  232. // wps.Application.ActiveWindow.ActivePane.View.SeekView = 0
  233. waterMakerGroup.forEach((v) => v.Delete())
  234. }
  235. window.WaterMark = {
  236. SetWaterMark,
  237. SetWaterMarkTest,
  238. ClearWaterMark,
  239. }
  240. })(window)