12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { floatToHex } from './utility';
- import { xmlParser, getElementsByTagName } from './dom';
- type WatermarkAnnotType = {
- obj_type: string;
- obj_attr: WatermarkType;
- };
- export const parseWatermarkFromXml = (
- xmlString: string,
- ): WatermarkAnnotType => {
- if (!xmlString) return { obj_type: 'watermark', obj_attr: {} };
- const xmlDoc = xmlParser(xmlString);
- const elements = xmlDoc.firstElementChild || xmlDoc.firstChild;
- const data: WatermarkType = {};
- const element = getElementsByTagName(elements as ChildNode, 'watermarks');
- if (element && element[1]) {
- const array: Element[] = Array.prototype.slice.call(element[1].childNodes);
- array.forEach((ele, index) => {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- const attr: any = ele.attributes;
- switch (ele.tagName) {
- case 'Font':
- data.type = 'text';
- data.text = (array[index + 1].textContent as string).trim();
- break;
- case 'Opacity': {
- data.opacity = attr.value.value;
- break;
- }
- case 'Rotation': {
- data.rotation = parseFloat(attr.value.value);
- break;
- }
- case 'Scale': {
- data.scale = parseFloat(attr.value.value);
- break;
- }
- case 'Color': {
- const r = attr.r.value;
- const g = attr.g.value;
- const b = attr.b.value;
- data.textcolor = floatToHex(r, g, b);
- break;
- }
- case 'PageRange': {
- data.pages = (array[index + 1].textContent as string).trim();
- break;
- }
- default:
- break;
- }
- });
- }
- return {
- obj_type: 'watermark',
- obj_attr: {
- vertalign: 'center',
- horizalign: 'center',
- xoffset: 0,
- yoffset: 0,
- isfront: 'yes',
- ...data,
- },
- };
- };
- export default parseWatermarkFromXml;
|