123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- 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) => {
- 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;
|