all files / pages/ pdf-section.js

71.6% Statements 179/250
35.63% Branches 57/160
70.21% Functions 33/47
71.6% Lines 179/250
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400                 3784×                           270×                           19×           65×           440×   440×                 440×                           10×                           30× 30× 30×   30× 30×         30×         30×         30×         30×   30× 30×           30×           30×           30×           30×   17×     15× 15× 15× 15× 15× 15×     15× 15× 15× 15× 15× 15×   15×   15× 15×   481× 1784× 466×     15× 15×   15× 15× 15× 15× 15×     50× 25× 25× 25× 25×     25× 25× 25× 25× 25× 25× 25× 25× 25× 25× 25× 25× 25× 25× 25×     25×     25×     25× 25× 25× 25× 25×   25×     25×     25× 25× 25× 25× 25×   25×     25×     25× 25× 25× 25× 25×   25×     25×     25× 25× 25× 25× 25×                                                                                                                                        
define(["require", "exports", "./pdf-page", "./page-added-event-arguments", "./../primitives/pdf-reference", "./../primitives/pdf-array", "./../primitives/pdf-number", "./../primitives/pdf-name", "./../primitives/pdf-dictionary", "./../primitives/pdf-dictionary", "./../input-output/pdf-dictionary-properties", "./pdf-section-collection", "./pdf-section-page-collection", "./../drawing/pdf-drawing", "./pdf-section-templates"], function (require, exports, pdf_page_1, page_added_event_arguments_1, pdf_reference_1, pdf_array_1, pdf_number_1, pdf_name_1, pdf_dictionary_1, pdf_dictionary_2, pdf_dictionary_properties_1, pdf_section_collection_1, pdf_section_page_collection_1, pdf_drawing_1, pdf_section_templates_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PdfSection = (function () {
        function PdfSection(document, pageSettings) {
            this.pageAdded = new page_added_event_arguments_1.PageAddedEventArgs();
            this.pdfPages = [];
            this.dictionaryProperties = new pdf_dictionary_properties_1.DictionaryProperties();
            this.pdfDocument = document;
            Eif (typeof pageSettings === 'undefined') {
                this.settings = document.pageSettings.clone();
                this.initialSettings = this.settings.clone();
            }
            else {
                this.settings = pageSettings.clone();
                this.initialSettings = this.settings.clone();
            }
            this.initialize();
        }
        Object.defineProperty(PdfSection.prototype, "parent", {
            get: function () {
                return this.sectionCollection;
            },
            set: function (value) {
                this.sectionCollection = value;
                this.section.items.setValue(this.dictionaryProperties.parent, new pdf_reference_1.PdfReferenceHolder(value));
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfSection.prototype, "parentDocument", {
            get: function () {
                return this.pdfDocument;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfSection.prototype, "pageSettings", {
            get: function () {
                return this.settings;
            },
            set: function (value) {
                if (value != null) {
                    this.settings = value;
                }
                else {
                    throw Error('Value can not be null.');
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfSection.prototype, "element", {
            get: function () {
                return this.section;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfSection.prototype, "count", {
            get: function () {
                return this.pagesReferences.count;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfSection.prototype, "template", {
            get: function () {
                if (this.pageTemplate == null) {
                    this.pageTemplate = new pdf_section_templates_1.PdfSectionTemplate();
                }
                return this.pageTemplate;
            },
            set: function (value) {
                this.pageTemplate = value;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfSection.prototype, "document", {
            get: function () {
                return this.sectionCollection.document;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfSection.prototype, "pages", {
            get: function () {
                if (this.pagesCollection == null || typeof this.pagesCollection === 'undefined') {
                    this.pagesCollection = new pdf_section_page_collection_1.PdfSectionPageCollection(this);
                }
                return this.pagesCollection;
            },
            enumerable: true,
            configurable: true
        });
        PdfSection.prototype.getPages = function () {
            return this.pdfPages;
        };
        PdfSection.prototype.pointToNativePdf = function (page, point) {
            var bounds = this.getActualBounds(page, true);
            point.x += bounds.x;
            point.y = this.pageSettings.height - (point.y);
            return point;
        };
        PdfSection.prototype.setPageSettings = function (settings) {
            this.settings = settings;
            this.state.orientation = settings.orientation;
            this.state.rotate = settings.rotate;
            this.state.size = settings.size;
            this.state.origin = settings.origin;
        };
        PdfSection.prototype.initialize = function () {
            this.pagesReferences = new pdf_array_1.PdfArray();
            this.section = new pdf_dictionary_1.PdfDictionary();
            this.state = new PageSettingsState(this.pdfDocument);
            this.section.sectionBeginSave = new pdf_dictionary_2.SaveSectionEventHandler(this, this.state);
            this.pageCount = new pdf_number_1.PdfNumber(0);
            this.section.items.setValue(this.dictionaryProperties.count, this.pageCount);
            this.section.items.setValue(this.dictionaryProperties.type, new pdf_name_1.PdfName(this.dictionaryProperties.pages));
            this.section.items.setValue(this.dictionaryProperties.kids, this.pagesReferences);
        };
        PdfSection.prototype.containsTemplates = function (document, page, foreground) {
            var documentHeaders = this.getDocumentTemplates(document, page, foreground);
            var sectionTemplates = this.getSectionTemplates(page, foreground);
            return (documentHeaders.length > 0 || sectionTemplates.length > 0);
        };
        PdfSection.prototype.getDocumentTemplates = function (document, page, foreground) {
            var templates = [];
            Iif (this.template.applyDocumentTopTemplate && document.template.getTop(page) != null) {
                if ((!(document.template.getTop(page).foreground || foreground)) || (document.template.getTop(page).foreground && foreground)) {
                    templates.push(document.template.getTop(page));
                }
            }
            Iif (this.template.applyDocumentBottomTemplate && document.template.getBottom(page) != null) {
                if ((!(document.template.getBottom(page).foreground || foreground)) || (document.template.getBottom(page).foreground && foreground)) {
                    templates.push(document.template.getBottom(page));
                }
            }
            Iif (this.template.applyDocumentLeftTemplate && document.template.getLeft(page) != null) {
                if ((!(document.template.getLeft(page).foreground || foreground)) || (document.template.getLeft(page).foreground && foreground)) {
                    templates.push(document.template.getLeft(page));
                }
            }
            Iif (this.template.applyDocumentRightTemplate && document.template.getRight(page) != null) {
                if ((!(document.template.getRight(page).foreground || foreground)) || (document.template.getRight(page).foreground && foreground)) {
                    templates.push(document.template.getRight(page));
                }
            }
            return templates;
        };
        PdfSection.prototype.getSectionTemplates = function (page, foreground) {
            var templates = [];
            Iif (this.template.getTop(page) != null) {
                var pageTemplate = this.template.getTop(page);
                if ((!(pageTemplate.foreground || foreground)) || (pageTemplate.foreground && foreground)) {
                    templates.push(pageTemplate);
                }
            }
            Iif (this.template.getBottom(page) != null) {
                var pageTemplate = this.template.getBottom(page);
                if ((!(pageTemplate.foreground || foreground)) || (pageTemplate.foreground && foreground)) {
                    templates.push(pageTemplate);
                }
            }
            Iif (this.template.getLeft(page) != null) {
                var pageTemplate = this.template.getLeft(page);
                if ((!(pageTemplate.foreground || foreground)) || (pageTemplate.foreground && foreground)) {
                    templates.push(pageTemplate);
                }
            }
            Iif (this.template.getRight(page) != null) {
                var pageTemplate = this.template.getRight(page);
                if ((!(pageTemplate.foreground || foreground)) || (pageTemplate.foreground && foreground)) {
                    templates.push(pageTemplate);
                }
            }
            return templates;
        };
        PdfSection.prototype.add = function (page) {
            if (typeof page === 'undefined') {
                var page_1 = new pdf_page_1.PdfPage();
                this.add(page_1);
                return page_1;
            }
            else {
                var r = this.checkPresence(page);
                this.pdfPages.push(page);
                this.pagesReferences.add(r);
                page.setSection(this);
                page.resetProgress();
                this.pageAddedMethod(page);
            }
        };
        PdfSection.prototype.checkPresence = function (page) {
            var rh = new pdf_reference_1.PdfReferenceHolder(page);
            var contains = false;
            var sc = this.parent;
            for (var index = 0; index < sc.section.length; index++) {
                var section = sc.section[index];
                contains = contains || section.contains(page);
            }
            return rh;
        };
        PdfSection.prototype.contains = function (page) {
            var index = this.indexOf(page);
            return (0 <= index);
        };
        PdfSection.prototype.indexOf = function (page) {
            for (var index = 0; index < this.pdfPages.length; index++) {
                if (this.pdfPages[index] === page) {
                    return this.pdfPages.indexOf(page);
                }
            }
            var r = new pdf_reference_1.PdfReferenceHolder(page);
            return this.pagesReferences.indexOf(r);
        };
        PdfSection.prototype.pageAddedMethod = function (page) {
            var args = new page_added_event_arguments_1.PageAddedEventArgs(page);
            this.onPageAdded(args);
            var parent = this.parent;
            parent.document.pages.onPageAdded(args);
            this.pageCount.intValue = this.count;
        };
        PdfSection.prototype.onPageAdded = function (args) {
        };
        PdfSection.prototype.getActualBounds = function (arg1, arg2, arg3) {
            if (arg1 instanceof pdf_page_1.PdfPage && typeof arg2 === 'boolean') {
                var result = void 0;
                var document_1 = this.parent.document;
                result = this.getActualBounds(document_1, arg1, arg2);
                return result;
            }
            else {
                arg1 = arg1;
                arg2 = arg2;
                arg3 = arg3;
                var bounds = new pdf_drawing_1.RectangleF(0, 0, 0, 0);
                bounds.height = (arg3) ? this.pageSettings.size.height : this.pageSettings.getActualSize().height;
                bounds.width = (arg3) ? this.pageSettings.size.width : this.pageSettings.getActualSize().width;
                var left = this.getLeftIndentWidth(arg1, arg2, arg3);
                var top_1 = this.getTopIndentHeight(arg1, arg2, arg3);
                var right = this.getRightIndentWidth(arg1, arg2, arg3);
                var bottom = this.getBottomIndentHeight(arg1, arg2, arg3);
                bounds.x += left;
                bounds.y += top_1;
                bounds.width -= (left + right);
                bounds.height -= (top_1 + bottom);
                return bounds;
            }
        };
        PdfSection.prototype.getLeftIndentWidth = function (document, page, includeMargins) {
            Iif (document == null) {
                throw new Error('ArgumentNullException:document');
            }
            Iif (page == null) {
                throw new Error('ArgumentNullException:page');
            }
            var value = (includeMargins) ? this.pageSettings.margins.left : 0;
            var templateWidth = (this.template.getLeft(page) != null) ? this.template.getLeft(page).width : 0;
            var docTemplateWidth = (document.template.getLeft(page) != null) ? document.template.getLeft(page).width : 0;
            value += (this.template.applyDocumentLeftTemplate) ? Math.max(templateWidth, docTemplateWidth) : templateWidth;
            return value;
        };
        PdfSection.prototype.getTopIndentHeight = function (document, page, includeMargins) {
            Iif (document == null) {
                throw new Error('ArgumentNullException:document');
            }
            Iif (page == null) {
                throw new Error('ArgumentNullException:page');
            }
            var value = (includeMargins) ? this.pageSettings.margins.top : 0;
            var templateHeight = (this.template.getTop(page) != null) ? this.template.getTop(page).height : 0;
            var docTemplateHeight = (document.template.getTop(page) != null) ? document.template.getTop(page).height : 0;
            value += (this.template.applyDocumentTopTemplate) ? Math.max(templateHeight, docTemplateHeight) : templateHeight;
            return value;
        };
        PdfSection.prototype.getRightIndentWidth = function (document, page, includeMargins) {
            Iif (document == null) {
                throw new Error('ArgumentNullException:document');
            }
            Iif (page == null) {
                throw new Error('ArgumentNullException:page');
            }
            var value = (includeMargins) ? this.pageSettings.margins.right : 0;
            var templateWidth = (this.template.getRight(page) != null) ? this.template.getRight(page).width : 0;
            var docTemplateWidth = (document.template.getRight(page) != null) ? document.template.getRight(page).width : 0;
            value += (this.template.applyDocumentRightTemplate) ? Math.max(templateWidth, docTemplateWidth) : templateWidth;
            return value;
        };
        PdfSection.prototype.getBottomIndentHeight = function (document, page, includeMargins) {
            Iif (document == null) {
                throw new Error('ArgumentNullException:document');
            }
            Iif (page == null) {
                throw new Error('ArgumentNullException:page');
            }
            var value = (includeMargins) ? this.pageSettings.margins.bottom : 0;
            var templateHeight = (this.template.getBottom(page) != null) ? this.template.getBottom(page).height : 0;
            var docTemplateHeight = (document.template.getBottom(page) != null) ? document.template.getBottom(page).height : 0;
            value += (this.template.applyDocumentBottomTemplate) ? Math.max(templateHeight, docTemplateHeight) : templateHeight;
            return value;
        };
        PdfSection.prototype.remove = function (page) {
            if (page == null) {
                throw Error('ArgumentNullException("page")');
            }
            var index = this.pdfPages.indexOf(page);
            this.pagesReferences.removeAt(index);
            var temproaryPages = [];
            for (var j = 0; j < index; j++) {
                temproaryPages.push(this.pdfPages[j]);
            }
            for (var j = index + 1; j < this.pdfPages.length; j++) {
                temproaryPages.push(this.pdfPages[j]);
            }
            this.pdfPages = temproaryPages;
        };
        PdfSection.prototype.applyPageSettings = function (container, parentSettings, state) {
            var bounds = new pdf_drawing_1.RectangleF(state.origin, state.size);
            container.items.setValue(this.dictionaryProperties.mediaBox, pdf_array_1.PdfArray.fromRectangle(bounds));
            var rotate = 0;
            rotate = pdf_section_collection_1.PdfSectionCollection.rotateFactor * state.rotate;
            var angle = new pdf_number_1.PdfNumber(rotate);
            container.items.setValue(this.dictionaryProperties.rotate, angle);
        };
        PdfSection.prototype.beginSave = function (state, writer) {
            var doc = writer.document;
            this.applyPageSettings(this.section, doc.pageSettings, state);
        };
        PdfSection.prototype.drawTemplates = function (page, layer, document, foreground) {
            var documentHeaders = this.getDocumentTemplates(document, page, foreground);
            var sectionHeaders = this.getSectionTemplates(page, foreground);
            this.drawTemplatesHelper(layer, document, documentHeaders);
            this.drawTemplatesHelper(layer, document, sectionHeaders);
        };
        PdfSection.prototype.drawTemplatesHelper = function (layer, document, templates) {
            if (templates != null && templates.length > 0) {
                var len = templates.length;
                for (var i = 0; i < len; i++) {
                    var template = templates[i];
                    template.draw(layer, document);
                }
            }
        };
        return PdfSection;
    }());
    exports.PdfSection = PdfSection;
    var PageSettingsState = (function () {
        function PageSettingsState(document) {
            this.pageOrientation = document.pageSettings.orientation;
            this.pageRotate = document.pageSettings.rotate;
            this.pageSize = document.pageSettings.size;
            this.pageOrigin = document.pageSettings.origin;
        }
        Object.defineProperty(PageSettingsState.prototype, "orientation", {
            get: function () {
                return this.pageOrientation;
            },
            set: function (value) {
                this.pageOrientation = value;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PageSettingsState.prototype, "rotate", {
            get: function () {
                return this.pageRotate;
            },
            set: function (value) {
                this.pageRotate = value;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PageSettingsState.prototype, "size", {
            get: function () {
                return this.pageSize;
            },
            set: function (value) {
                this.pageSize = value;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PageSettingsState.prototype, "origin", {
            get: function () {
                return this.pageOrigin;
            },
            set: function (value) {
                this.pageOrigin = value;
            },
            enumerable: true,
            configurable: true
        });
        return PageSettingsState;
    }());
    exports.PageSettingsState = PageSettingsState;
});