all files / core/ pdf-page.js

26.83% Statements 172/641
13.6% Branches 74/544
27.87% Functions 17/61
26.8% Lines 171/638
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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980       10×                               10×           29×                                     29×                                                                                           23×   23×   23×           27×   27×     27×                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
define(["require", "exports", "./pdf-primitives", "./utils", "./annotations/annotation-collection", "./graphics/pdf-graphics", "./base-stream", "./enumerator", "./graphics/pdf-template"], function (require, exports, pdf_primitives_1, utils_1, annotation_collection_1, pdf_graphics_1, base_stream_1, enumerator_1, pdf_template_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PdfPage = (function () {
        function PdfPage(crossReference, pageIndex, dictionary, reference) {
            this._isAnnotationParsed = false;
            this._isNew = false;
            this._isDuplicate = false;
            this._isLineAnnotation = false;
            this._pageIndex = pageIndex;
            this._pageDictionary = dictionary;
            this._crossReference = crossReference;
            this._ref = reference;
        }
        Object.defineProperty(PdfPage.prototype, "annotations", {
            get: function () {
                if (typeof this._annotations === 'undefined') {
                    if (this._pageDictionary && this._pageDictionary.has('Annots')) {
                        var annots = this._getProperty('Annots');
                        Eif (utils_1._isNullOrUndefined(annots) && Array.isArray(annots)) {
                            var widgets_1;
                            Eif (this._crossReference._document._catalog._catalogDictionary.has('AcroForm')) {
                                widgets_1 = this._crossReference._document.form._parseWidgetReferences();
                            }
                            Iif (widgets_1 && widgets_1.length > 0) {
                                var validAnnotations_1 = [];
                                annots.forEach(function (entry) {
                                    if (widgets_1.indexOf(entry) === -1) {
                                        validAnnotations_1.push(entry);
                                    }
                                });
                                this._annotations = new annotation_collection_1.PdfAnnotationCollection(validAnnotations_1, this._crossReference, this);
                            }
                            else {
                                this._annotations = new annotation_collection_1.PdfAnnotationCollection(annots, this._crossReference, this);
                            }
                        }
                    }
                    if (typeof this._annotations === 'undefined') {
                        this._annotations = new annotation_collection_1.PdfAnnotationCollection([], this._crossReference, this);
                    }
                    this._annotations._getAnnotations();
                }
                return this._annotations;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfPage.prototype, "size", {
            get: function () {
                if (typeof this._size === 'undefined' || typeof this._size.width === 'undefined' || typeof this._size.height === 'undefined') {
                    var mBox = utils_1._getInheritableProperty(this._pageDictionary, 'MediaBox', false, true, 'Parent', 'P');
                    var cBox = utils_1._getInheritableProperty(this._pageDictionary, 'CropBox', false, true, 'Parent', 'P');
                    var width = 0;
                    var height = 0;
                    var rotate = this._pageDictionary && this._pageDictionary.has('Rotate')
                        ? utils_1._getInheritableProperty(this._pageDictionary, 'Rotate', false, true, 'Parent')
                        : 0;
                    Iif (cBox && rotate !== null && typeof rotate !== 'undefined') {
                        width = cBox[2] - cBox[0];
                        height = cBox[3] - cBox[1];
                        var isValidCropBox = !(mBox && (mBox[2] - mBox[0]) < width);
                        if (!(((rotate === 0 || rotate === 180) && (width < height)) ||
                            ((rotate === 90 || rotate === 270) && (width > height) || isValidCropBox)) && (rotate === 0 && mBox)) {
                            width = mBox[2] - mBox[0];
                            height = mBox[3] !== 0 ? mBox[3] - mBox[1] : mBox[1];
                        }
                    }
                    else Eif (mBox) {
                        width = mBox[2] - mBox[0];
                        height = mBox[3] !== 0 ? mBox[3] - mBox[1] : mBox[1];
                    }
                    else {
                        this._pageDictionary.update('MediaBox', [0, 0, 612, 792]);
                        width = 612;
                        height = 792;
                    }
                    this._size = { width: Math.abs(width), height: Math.abs(height) };
                }
                return this._size;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfPage.prototype, "rotation", {
            get: function () {
                var angle = 0;
                Eif (typeof this._rotation === 'undefined') {
                    angle = utils_1._getInheritableProperty(this._pageDictionary, 'Rotate', false, true, 'Parent');
                    Iif (angle < 0) {
                        angle += 360;
                    }
                    this._rotation = (typeof angle !== 'undefined') ? ((angle / 90) % 4) : enumerator_1.PdfRotationAngle.angle0;
                }
                return this._rotation;
            },
            set: function (value) {
                if (!this._isNew) {
                    this._rotation = value;
                    var rotate = Math.floor(this._rotation) * 90;
                    if (rotate >= 360) {
                        rotate = rotate % 360;
                    }
                    this._pageDictionary.update('Rotate', rotate);
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfPage.prototype, "tabOrder", {
            get: function () {
                return this._obtainTabOrder();
            },
            set: function (value) {
                this._tabOrder = value;
                var tabs = '';
                if (this._tabOrder !== enumerator_1.PdfFormFieldsTabOrder.none) {
                    if (this._tabOrder === enumerator_1.PdfFormFieldsTabOrder.row) {
                        tabs = 'R';
                    }
                    else if (this._tabOrder === enumerator_1.PdfFormFieldsTabOrder.column) {
                        tabs = 'C';
                    }
                    else if (this._tabOrder === enumerator_1.PdfFormFieldsTabOrder.structure) {
                        tabs = 'S';
                    }
                }
                this._pageDictionary.update('Tabs', pdf_primitives_1._PdfName.get(tabs));
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfPage.prototype, "cropBox", {
            get: function () {
                if (typeof this._cBox === 'undefined') {
                    this._cBox = utils_1._getInheritableProperty(this._pageDictionary, 'CropBox', false, true, 'Parent', 'P');
                }
                if (typeof this._cBox === 'undefined') {
                    this._cBox = [0, 0, 0, 0];
                }
                return this._cBox;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfPage.prototype, "mediaBox", {
            get: function () {
                if (typeof this._mBox === 'undefined') {
                    this._mBox = utils_1._getInheritableProperty(this._pageDictionary, 'MediaBox', false, true, 'Parent', 'P');
                }
                Iif (typeof this._mBox === 'undefined') {
                    this._mBox = [0, 0, 0, 0];
                }
                return this._mBox;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfPage.prototype, "orientation", {
            get: function () {
                if (typeof this._orientation === 'undefined') {
                    if (typeof this.size !== 'undefined') {
                        var size = this.size;
                        if (size.width > size.height) {
                            this._orientation = enumerator_1.PdfPageOrientation.landscape;
                        }
                        else {
                            this._orientation = enumerator_1.PdfPageOrientation.portrait;
                        }
                    }
                }
                return this._orientation;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfPage.prototype, "_origin", {
            get: function () {
                Eif (typeof this._o === 'undefined' || (this._o[0] === 0 && this._o[1] === 0)) {
                    this._o = [this.mediaBox[0], this._mBox[1]];
                }
                return this._o;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfPage.prototype, "graphics", {
            get: function () {
                Eif (typeof this._g === 'undefined' || this._needInitializeGraphics) {
                    this._parseGraphics();
                }
                return this._g;
            },
            enumerable: true,
            configurable: true
        });
        PdfPage.prototype._addWidget = function (reference) {
            var annots;
            if (this._pageDictionary.has('Annots')) {
                var annotsRef = this._pageDictionary.getRaw('Annots');
                annots = this._getProperty('Annots');
                if (annotsRef && annotsRef instanceof pdf_primitives_1._PdfReference) {
                    delete this._pageDictionary._map.Annots;
                    this._pageDictionary.update('Annots', annots);
                }
            }
            if (annots && Array.isArray(annots)) {
                annots.push(reference);
            }
            else {
                this._pageDictionary.update('Annots', [reference]);
            }
            this._pageDictionary._updated = true;
        };
        PdfPage.prototype._getProperty = function (key, getArray) {
            Eif (getArray === void 0) { getArray = false; }
            var value = utils_1._getInheritableProperty(this._pageDictionary, key, getArray, false);
            Iif (!Array.isArray(value)) {
                return value;
            }
            Eif (value.length === 1 || !(value[0] instanceof pdf_primitives_1._PdfDictionary)) {
                return value[0];
            }
            return pdf_primitives_1._PdfDictionary.merge(this._crossReference, value);
        };
        PdfPage.prototype._parseGraphics = function () {
            this._loadContents();
            var saveStream = new base_stream_1._PdfContentStream([32, 113, 32, 10]);
            var saveReference = this._crossReference._getNextReference();
            this._crossReference._cacheMap.set(saveReference, saveStream);
            this._contents.splice(0, 0, saveReference);
            var restoreStream = new base_stream_1._PdfContentStream([32, 81, 32, 10]);
            var restoreReference = this._crossReference._getNextReference();
            this._crossReference._cacheMap.set(restoreReference, restoreStream);
            this._contents.push(restoreReference);
            var contentStream = new base_stream_1._PdfContentStream([]);
            var contentReference = this._crossReference._getNextReference();
            this._crossReference._cacheMap.set(contentReference, contentStream);
            this._contents.push(contentReference);
            this._pageDictionary.set('Contents', this._contents);
            this._pageDictionary._updated = true;
            this._initializeGraphics(contentStream);
        };
        PdfPage.prototype._loadContents = function () {
            var contents = this._pageDictionary.getRaw('Contents');
            var ref;
            Iif (contents !== null && typeof contents !== 'undefined' && contents instanceof pdf_primitives_1._PdfReference) {
                ref = contents;
                contents = this._crossReference._fetch(ref);
            }
            Iif (contents && contents instanceof base_stream_1._PdfBaseStream) {
                this._contents = [ref];
            }
            else Eif (contents && Array.isArray(contents)) {
                this._contents = contents;
            }
            else {
                this._contents = [];
            }
        };
        PdfPage.prototype._initializeGraphics = function (stream) {
            var isInvalidCase = false;
            var llx = 0;
            var lly = 0;
            var urx = 0;
            var ury = 0;
            var size = this.size;
            var mbox = this.mediaBox;
            Eif (mbox && mbox.length >= 4) {
                llx = mbox[0];
                lly = mbox[1];
                urx = mbox[2];
                ury = mbox[3];
            }
            var cbox;
            Iif (this._pageDictionary.has('CropBox')) {
                cbox = this.cropBox;
                if (cbox && cbox.length >= 4) {
                    var cx = cbox[0];
                    var cy = cbox[1];
                    var crx = cbox[2];
                    var cry = cbox[3];
                    var isValid = (cx < 0 || cy < 0 || crx < 0 || cry < 0) &&
                        (Math.floor(Math.abs(cy)) === Math.floor(Math.abs(size.height))) &&
                        (Math.floor(Math.abs(cx)) === Math.floor(Math.abs(size.width)));
                    if (isValid) {
                        this._g = new pdf_graphics_1.PdfGraphics({ width: Math.max(cx, crx), height: Math.max(cy, cry) }, stream, this._crossReference, this);
                    }
                    else {
                        this._g = new pdf_graphics_1.PdfGraphics(size, stream, this._crossReference, this);
                        this._g._cropBox = cbox;
                    }
                }
                else {
                    this._g = new pdf_graphics_1.PdfGraphics(size, stream, this._crossReference, this);
                }
            }
            else Iif ((llx < 0 || lly < 0 || urx < 0 || ury < 0) &&
                (Math.floor(Math.abs(lly)) === Math.floor(Math.abs(size.height))) &&
                (Math.floor(Math.abs(urx)) === Math.floor(Math.abs(size.width)))) {
                var width = Math.max(llx, urx);
                var height = Math.max(lly, ury);
                if (width <= 0 || height <= 0) {
                    isInvalidCase = true;
                    if (llx < 0) {
                        llx = -llx;
                    }
                    if (lly < 0) {
                        lly = -lly;
                    }
                    if (urx < 0) {
                        urx = -urx;
                    }
                    if (ury < 0) {
                        ury = -ury;
                    }
                    width = Math.max(llx, urx);
                    height = Math.max(lly, ury);
                }
                this._g = new pdf_graphics_1.PdfGraphics({ width: width, height: height }, stream, this._crossReference, this);
            }
            else {
                this._g = new pdf_graphics_1.PdfGraphics(size, stream, this._crossReference, this);
            }
            Iif (this._pageDictionary.has('MediaBox')) {
                this._g._mediaBoxUpperRightBound = isInvalidCase ? -lly : ury;
            }
            this._graphicsState = this._g.save();
            var origin = this._origin;
            Eif ((origin[0] >= 0 && origin[1] >= 0) || Math.sign(origin[0]) !== Math.sign(origin[1])) {
                this._g._initializeCoordinates();
            }
            else {
                this._g._initializeCoordinates(this);
            }
            Eif (!this._isNew) {
                var rotation = this.rotation;
                Iif (!Number.isNaN(rotation) && (rotation !== enumerator_1.PdfRotationAngle.angle0 || this._pageDictionary.has('Rotate'))) {
                    var rotate = void 0;
                    if (this._pageDictionary.has('Rotate')) {
                        rotate = this._pageDictionary.get('Rotate');
                    }
                    else {
                        rotate = rotation * 90;
                    }
                    var clip = this._g._clipBounds;
                    if (rotate === 90) {
                        this._g.translateTransform({ x: 0, y: size.height });
                        this._g.rotateTransform(-90);
                        this._g._clipBounds = [clip[0], clip[1], size.width, size.height];
                    }
                    else if (rotate === 180) {
                        this._g.translateTransform({ x: size.width, y: size.height });
                        this._g.rotateTransform(-180);
                    }
                    else if (rotate === 270) {
                        this._g.translateTransform({ x: size.width, y: 0 });
                        this._g.rotateTransform(-270);
                        this._g._clipBounds = [clip[0], clip[1], size.height, size.width];
                    }
                }
            }
            Iif (this._isNew && this._pageSettings && !this._isLineAnnotation) {
                var clipBounds = this._getActualBounds(this._pageSettings);
                this._g._clipTranslateMargins(clipBounds);
            }
            this._needInitializeGraphics = false;
        };
        PdfPage.prototype._getActualBounds = function (pageSettings) {
            var actualSize = pageSettings._getActualSize();
            return [pageSettings.margins.left, pageSettings.margins.top, actualSize[0], actualSize[1]];
        };
        PdfPage.prototype._fetchResources = function () {
            if (typeof this._resourceObject === 'undefined') {
                if (this._pageDictionary && this._pageDictionary.has('Resources')) {
                    var obj = this._pageDictionary.getRaw('Resources');
                    if (obj !== null && typeof obj !== 'undefined' && obj instanceof pdf_primitives_1._PdfReference) {
                        this._hasResourceReference = true;
                        this._resourceObject = this._crossReference._fetch(obj);
                    }
                    else if (obj && obj instanceof pdf_primitives_1._PdfDictionary) {
                        this._resourceObject = obj;
                    }
                }
                else {
                    this._resourceObject = new pdf_primitives_1._PdfDictionary(this._crossReference);
                    this._pageDictionary.update('Resources', this._resourceObject);
                }
            }
            return this._resourceObject;
        };
        PdfPage.prototype._getCropOrMediaBox = function () {
            var box;
            if (this._pageDictionary) {
                if (this._pageDictionary.has('CropBox')) {
                    box = this._pageDictionary.getArray('CropBox');
                }
                else if (this._pageDictionary.has('MediaBox')) {
                    box = this._pageDictionary.getArray('MediaBox');
                }
            }
            return box;
        };
        PdfPage.prototype._beginSave = function () {
            if (typeof this._graphicsState !== 'undefined') {
                this.graphics.restore(this._graphicsState);
                this._graphicsState = null;
                this._needInitializeGraphics = true;
            }
        };
        PdfPage.prototype._destroy = function () {
            this._pageDictionary = undefined;
            this._size = undefined;
            this._mBox = undefined;
            this._cBox = undefined;
            this._o = undefined;
            this._g = undefined;
            this._graphicsState = undefined;
            this._contents = undefined;
        };
        PdfPage.prototype._obtainTabOrder = function () {
            if (this._pageDictionary && this._pageDictionary.has('Tabs')) {
                var tabOrder = this._pageDictionary.get('Tabs');
                if (tabOrder === pdf_primitives_1._PdfName.get('R')) {
                    this._tabOrder = enumerator_1.PdfFormFieldsTabOrder.row;
                }
                else if (tabOrder === pdf_primitives_1._PdfName.get('C')) {
                    this._tabOrder = enumerator_1.PdfFormFieldsTabOrder.column;
                }
                else if (tabOrder === pdf_primitives_1._PdfName.get('S')) {
                    this._tabOrder = enumerator_1.PdfFormFieldsTabOrder.structure;
                }
                else if (tabOrder === pdf_primitives_1._PdfName.get('W')) {
                    this._tabOrder = enumerator_1.PdfFormFieldsTabOrder.widget;
                }
            }
            if (this._tabOrder === null || typeof this._tabOrder === 'undefined') {
                this._tabOrder = enumerator_1.PdfFormFieldsTabOrder.none;
            }
            return this._tabOrder;
        };
        PdfPage.prototype._removeAnnotation = function (reference) {
            if (this._pageDictionary && this._pageDictionary.has('Annots')) {
                var annots = this._getProperty('Annots');
                if (utils_1._isNullOrUndefined(annots) && Array.isArray(annots)) {
                    annots = annots.filter(function (item) { return item !== reference; });
                    this._pageDictionary.set('Annots', annots);
                    this._pageDictionary._updated = true;
                }
            }
        };
        Object.defineProperty(PdfPage.prototype, "_contentTemplate", {
            get: function () {
                this._fetchResources();
                var targetArray = this._combineContent();
                var targetStream = new base_stream_1._PdfContentStream(Array.from(targetArray));
                var template = new pdf_template_1.PdfTemplate(targetStream, this._crossReference);
                template._content.dictionary.set('Resources', this._resourceObject);
                if (this.cropBox[0] > 0 || this.cropBox[1] > 0) {
                    template._content.dictionary.set('BBox', this.cropBox);
                    template._size = { width: this.cropBox[0], height: this.cropBox[1] };
                }
                else if (this.mediaBox[0] > 0 || this.mediaBox[1] > 0) {
                    template._content.dictionary.set('BBox', this.mediaBox);
                    template._size = { width: this.mediaBox[0], height: this.mediaBox[1] };
                }
                else {
                    template._content.dictionary.set('BBox', [0, 0, this.size.width, this.size.height]);
                    template._size = { width: this.size.width, height: this.size.height };
                }
                return template;
            },
            enumerable: true,
            configurable: true
        });
        PdfPage.prototype._combineIntoSingleArray = function (arrays) {
            var totalLength = arrays.reduce(function (length, arr) { return length + arr.length; }, 0);
            var targetArray = new Uint8Array(totalLength);
            var offset = 0;
            arrays.forEach(function (sourceArray) {
                targetArray.set(sourceArray, offset);
                offset += sourceArray.length;
            });
            return targetArray;
        };
        PdfPage.prototype._combineContent = function () {
            var _this = this;
            var list = [];
            var array;
            this._loadContents();
            list.push(new Uint8Array([32, 113, 32, 10]));
            var contents = this._contents;
            contents.forEach(function (reference) {
                var base = _this._crossReference._fetch(reference);
                if (base) {
                    if (base instanceof base_stream_1._PdfContentStream) {
                        array = new Uint8Array(base._bytes);
                    }
                    else if (base instanceof base_stream_1._PdfBaseStream) {
                        array = base.getBytes();
                    }
                    if (array) {
                        list.push(array);
                        list.push(new Uint8Array([13, 10]));
                    }
                }
            });
            list.push(new Uint8Array([32, 81, 32, 10]));
            list.push(new Uint8Array([13, 10]));
            var targetArray = this._combineIntoSingleArray(list);
            return targetArray;
        };
        return PdfPage;
    }());
    exports.PdfPage = PdfPage;
    var PdfDestination = (function () {
        function PdfDestination(arg1, arg2, arg3) {
            this._location = { x: 0, y: 0 };
            this._destinationMode = enumerator_1.PdfDestinationMode.location;
            this._zoom = 0;
            this._isValid = true;
            this._destinationBounds = { x: 0, y: 0, width: 0, height: 0 };
            this._array = Array();
            if (typeof arg1 !== 'undefined' && arg1 !== null) {
                if (arg1.rotation === enumerator_1.PdfRotationAngle.angle180) {
                    this._location = { x: arg1.graphics._size.width, y: this._location.y };
                }
                else if (arg1.rotation === enumerator_1.PdfRotationAngle.angle90) {
                    this._location = { x: 0, y: 0 };
                }
                else if (arg1.rotation === enumerator_1.PdfRotationAngle.angle270) {
                    this._location = { x: arg1.graphics._size.width, y: 0 };
                }
                else {
                    this._location = { x: 0, y: this._location.y };
                }
                this._page = arg1;
                this._index = arg1._pageIndex;
            }
            if (arg2 !== null && typeof arg2 !== 'undefined') {
                this._location = { x: arg2.x, y: arg2.y };
                if ('width' in arg2 && 'height' in arg2 && typeof arg2.width === 'number' && typeof arg2.height === 'number') {
                    this._destinationBounds = arg2;
                }
            }
            if (arg3 !== null && typeof arg3 !== 'undefined') {
                if ('mode' in arg3 && arg3.mode !== null && typeof arg3.mode !== 'undefined') {
                    this.mode = arg3.mode;
                }
                if ('zoom' in arg3 && arg3.zoom !== null && typeof arg3.zoom !== 'undefined') {
                    this.zoom = arg3.zoom;
                }
            }
        }
        Object.defineProperty(PdfDestination.prototype, "zoom", {
            get: function () {
                return this._zoom;
            },
            set: function (value) {
                if (value !== this._zoom) {
                    this._zoom = value;
                    this._initializePrimitive();
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfDestination.prototype, "page", {
            get: function () {
                return this._page;
            },
            set: function (value) {
                if (value !== this._page) {
                    this._page = value;
                    this._initializePrimitive();
                    this._index = value._pageIndex;
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfDestination.prototype, "pageIndex", {
            get: function () {
                return this._index;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfDestination.prototype, "mode", {
            get: function () {
                return this._destinationMode;
            },
            set: function (value) {
                if (value !== this._destinationMode) {
                    this._destinationMode = value;
                    this._initializePrimitive();
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfDestination.prototype, "location", {
            get: function () {
                return this._location;
            },
            set: function (value) {
                if (value !== this._location) {
                    this._location = value;
                    this._initializePrimitive();
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfDestination.prototype, "destinationBounds", {
            get: function () {
                return this._destinationBounds;
            },
            set: function (value) {
                if (value !== this._destinationBounds) {
                    this._destinationBounds = value;
                    this._initializePrimitive();
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfDestination.prototype, "isValid", {
            get: function () {
                return this._isValid;
            },
            enumerable: true,
            configurable: true
        });
        PdfDestination.prototype._setValidation = function (value) {
            this._isValid = value;
        };
        PdfDestination.prototype._initializePrimitive = function () {
            this._array = [];
            var page = this._page;
            if (page && page._pageDictionary) {
                var element = page._pageDictionary;
                if (typeof element !== 'undefined' && element !== null) {
                    this._array.push(this._page._ref);
                }
                switch (this._destinationMode) {
                    case enumerator_1.PdfDestinationMode.location:
                        this._array.push(pdf_primitives_1._PdfName.get('XYZ'));
                        if (typeof page !== 'undefined' && page !== null) {
                            this._array.push(this._location.x);
                            this._array.push(this._page.graphics._size.height - this._location.y);
                        }
                        else {
                            this._array.push(0);
                            this._array.push(0);
                        }
                        this._array.push(this._zoom);
                        break;
                    case enumerator_1.PdfDestinationMode.fitToPage:
                        this._array.push(pdf_primitives_1._PdfName.get('Fit'));
                        break;
                    case enumerator_1.PdfDestinationMode.fitR:
                        this._array.push(pdf_primitives_1._PdfName.get('FitR'));
                        this._array.push(this._destinationBounds.x);
                        this._array.push(this._destinationBounds.y);
                        this._array.push(this._destinationBounds.width);
                        this._array.push(this._destinationBounds.height);
                        break;
                    case enumerator_1.PdfDestinationMode.fitH:
                        this._array.push(pdf_primitives_1._PdfName.get('FitH'));
                        this._array.push((typeof page !== 'undefined' && page !== null) ? page.size.height - this._location.y : 0);
                        break;
                }
                if (this._parent) {
                    this._parent._dictionary.set(this._isBookmark ? 'Dest' : 'D', this._array);
                    this._parent._dictionary._updated = true;
                }
            }
        };
        return PdfDestination;
    }());
    exports.PdfDestination = PdfDestination;
    var _PdfDestinationHelper = (function () {
        function _PdfDestinationHelper(dictionary, value) {
            if (dictionary && typeof value === 'string') {
                this._dictionary = dictionary;
                this._key = value;
            }
        }
        _PdfDestinationHelper.prototype._obtainDestination = function () {
            var destination;
            var page;
            var loadedDocument;
            if (!this._dictionary || (!this._dictionary.has(this._key) && !this._dictionary.has('D'))) {
                return undefined;
            }
            else if (this._dictionary && this._dictionary._crossReference && this._dictionary._crossReference._document) {
                loadedDocument = this._dictionary._crossReference._document;
            }
            if (this._dictionary.has('D')) {
                this._key = 'D';
            }
            var destinationArray = this._dictionary.getArray(this._key);
            if ((typeof destinationArray === 'string' || (destinationArray instanceof pdf_primitives_1._PdfName && typeof destinationArray.name === 'string')) && loadedDocument) {
                destinationArray = this._getDestination(destinationArray, loadedDocument);
            }
            var value;
            if (Array.isArray(destinationArray) && destinationArray.length > 0) {
                value = destinationArray[0];
            }
            var mode;
            var left;
            var top;
            var bottom;
            var right;
            var zoom;
            var index;
            var topValue;
            var leftValue;
            if (typeof value === 'number') {
                index = value;
            }
            else if (value instanceof pdf_primitives_1._PdfDictionary) {
                index = utils_1._getPageIndex(loadedDocument, value);
            }
            else if (value instanceof pdf_primitives_1._PdfReference) {
                var pageDictionary = loadedDocument._crossReference._fetch(value);
                if (pageDictionary && pageDictionary instanceof pdf_primitives_1._PdfDictionary) {
                    index = utils_1._getPageIndex(loadedDocument, pageDictionary);
                }
            }
            if (!page && typeof index === 'number' && (index >= 0 && index < loadedDocument.pageCount)) {
                page = loadedDocument.getPage(index);
            }
            if (Array.isArray(destinationArray) && destinationArray.length > 0) {
                mode = destinationArray[1];
            }
            if (mode && page && destinationArray) {
                switch (mode.name) {
                    case 'XYZ':
                        left = destinationArray[2];
                        top = destinationArray[3];
                        zoom = destinationArray[4];
                        topValue = typeof top === 'number' ? (page.size.height - top) : 0;
                        leftValue = typeof left === 'number' ? left : 0;
                        if (page.rotation !== enumerator_1.PdfRotationAngle.angle0) {
                            topValue = utils_1._checkRotation(page, top, left);
                        }
                        destination = new PdfDestination(page, { x: leftValue, y: topValue });
                        destination._index = page._pageIndex;
                        destination.zoom = (typeof zoom !== 'undefined' && zoom !== null) ? zoom : 0;
                        if (left === null || top === null || zoom === null || typeof left === 'undefined' ||
                            typeof top === 'undefined' || typeof zoom === 'undefined') {
                            destination._setValidation(false);
                        }
                        break;
                    case 'FitR':
                        if (destinationArray.length > 2) {
                            left = destinationArray[2];
                        }
                        if (destinationArray.length > 3) {
                            bottom = destinationArray[3];
                        }
                        if (destinationArray.length > 4) {
                            right = destinationArray[4];
                        }
                        if (destinationArray.length > 5) {
                            top = destinationArray[5];
                        }
                        left = (typeof left !== 'undefined' && left !== null) ? left : 0;
                        bottom = (typeof bottom !== 'undefined' && bottom !== null) ? bottom : 0;
                        right = (typeof right !== 'undefined' && right !== null) ? right : 0;
                        top = (typeof top !== 'undefined' && top !== null) ? top : 0;
                        destination = new PdfDestination(page, { x: left, y: bottom, width: right, height: top });
                        destination._index = page._pageIndex;
                        destination.mode = enumerator_1.PdfDestinationMode.fitR;
                        break;
                    case 'FitH':
                    case 'FitBH':
                        if (destinationArray.length > 2) {
                            top = destinationArray[2];
                        }
                        topValue = typeof top === 'number' ? (page.size.height - top) : 0;
                        destination = new PdfDestination(page, { x: 0, y: topValue });
                        destination._index = page._pageIndex;
                        destination.mode = enumerator_1.PdfDestinationMode.fitH;
                        if (top === null || typeof top === 'undefined') {
                            destination._setValidation(false);
                        }
                        break;
                    case 'Fit':
                        destination = new PdfDestination(page);
                        destination._index = page._pageIndex;
                        destination.mode = enumerator_1.PdfDestinationMode.fitToPage;
                        break;
                }
            }
            else if (Array.isArray(destinationArray)) {
                destination = new PdfDestination();
                if (destinationArray.length > 4) {
                    zoom = destinationArray[4];
                }
                if (destinationArray.length > 1) {
                    mode = destinationArray[1];
                }
                if (typeof zoom === 'number') {
                    destination.zoom = zoom;
                }
                if (mode) {
                    if (mode.name === 'Fit') {
                        destination.mode = enumerator_1.PdfDestinationMode.fitToPage;
                    }
                    else if (mode.name === 'XYZ') {
                        if (destinationArray.length > 2) {
                            left = destinationArray[2];
                        }
                        if (destinationArray.length > 3) {
                            topValue = destinationArray[3];
                        }
                        if ((typeof left === 'undefined' && left === null) || (typeof topValue === 'undefined' && topValue === null)
                            || (typeof zoom === 'undefined' && zoom === null)) {
                            destination._setValidation(false);
                        }
                    }
                }
                if (typeof index === 'number' && (index >= 0 && index < loadedDocument.pageCount)) {
                    destination._index = index;
                }
            }
            return destination;
        };
        _PdfDestinationHelper.prototype._getDestination = function (name, document) {
            var destinationArray;
            if (document) {
                destinationArray = this._getNamedDestination(document, name);
            }
            return destinationArray;
        };
        _PdfDestinationHelper.prototype._getNamedDestination = function (document, result) {
            var destination;
            var catalog = document._catalog;
            if (catalog && catalog._catalogDictionary) {
                if (result && typeof result === 'string') {
                    if (catalog._catalogDictionary.has('Names')) {
                        var names = catalog._catalogDictionary.get('Names');
                        if (names && names.has('Dests')) {
                            var kids = names.get('Dests');
                            if (kids) {
                                var ref = this._getNamedObjectFromTree(kids, result);
                                destination = this._extractDestination(ref, document);
                            }
                        }
                    }
                }
                else if (result && result instanceof pdf_primitives_1._PdfName) {
                    var destinations = catalog._catalogDictionary.get('Dests');
                    if (destinations) {
                        destination = destinations.get(result.name);
                    }
                }
            }
            return destination;
        };
        _PdfDestinationHelper.prototype._extractDestination = function (ref, document) {
            var dict;
            var destinationArray;
            if (ref && ref instanceof pdf_primitives_1._PdfReference) {
                dict = document._crossReference._fetch(ref);
            }
            if (dict) {
                if (dict instanceof pdf_primitives_1._PdfDictionary && dict.has('D')) {
                    destinationArray = dict.getRaw('D');
                }
                else if (Array.isArray(dict)) {
                    destinationArray = dict;
                }
            }
            return destinationArray ? destinationArray : ref;
        };
        _PdfDestinationHelper.prototype._getNamedObjectFromTree = function (kids, name) {
            var found = false;
            var currentDictionary = kids;
            var reference;
            while (!found && currentDictionary) {
                if (currentDictionary && currentDictionary.has('Kids')) {
                    currentDictionary = this._getProperKid(currentDictionary, name);
                }
                else if (currentDictionary && currentDictionary.has('Names')) {
                    reference = this._findName(currentDictionary, name);
                    found = true;
                }
            }
            return reference;
        };
        _PdfDestinationHelper.prototype._findName = function (current, name) {
            var names = current.get('Names');
            var halfLength = names.length / 2;
            var lowerIndex = 0;
            var topIndex = halfLength - 1;
            var half = 0;
            var found = false;
            var destinationReference;
            while (!found) {
                half = Math.floor((lowerIndex + topIndex) / 2);
                if (lowerIndex > topIndex) {
                    break;
                }
                var result = names[Number.parseInt(half.toString(), 10) * 2];
                if (result && result instanceof pdf_primitives_1._PdfReference) {
                    result = current._crossReference._fetch(result);
                }
                var cmp = this._stringCompare(name, result);
                if (cmp > 0) {
                    lowerIndex = half + 1;
                }
                else if (cmp < 0) {
                    topIndex = half - 1;
                }
                else {
                    found = true;
                    break;
                }
            }
            if (found) {
                destinationReference = names[half * 2 + 1];
            }
            return destinationReference;
        };
        _PdfDestinationHelper.prototype._getProperKid = function (kids, name) {
            var kidsArray;
            var kid;
            if (kids && kids.has('Kids')) {
                kidsArray = kids.getRaw('Kids');
            }
            if (kidsArray && Array.isArray(kidsArray) && kidsArray.length !== 0) {
                kidsArray = kids.getArray('Kids');
                for (var i = 0; i < kidsArray.length; i++) {
                    kid = kidsArray[Number.parseInt(i.toString(), 10)];
                    if (this._checkLimits(kid, name)) {
                        break;
                    }
                }
            }
            return kid;
        };
        _PdfDestinationHelper.prototype._checkLimits = function (kid, result) {
            var found = false;
            if (kid && kid.has('Limits')) {
                var limits = kid.get('Limits');
                var lowerLimit = limits[0];
                var higherLimit = limits[1];
                var lowCompare = this._stringCompare(lowerLimit, result);
                var highCompare = this._stringCompare(higherLimit, result);
                found = (lowCompare === 0 || highCompare === 0 || (lowCompare < 0 && highCompare > 0));
            }
            return found;
        };
        _PdfDestinationHelper.prototype._stringCompare = function (limits, result) {
            var byteArray = utils_1._stringToBytes(limits);
            var byteArray1 = utils_1._stringToBytes(result);
            var commonSize = Math.min(byteArray.length, byteArray1.length);
            var resultValue = 0;
            for (var i = 0; i < commonSize; i++) {
                var byte = byteArray[Number.parseInt(i.toString(), 10)];
                var byte1 = byteArray1[Number.parseInt(i.toString(), 10)];
                resultValue = byte - byte1;
                if (resultValue !== 0) {
                    break;
                }
            }
            if (resultValue === 0) {
                resultValue = byteArray.length - byteArray1.length;
            }
            return resultValue;
        };
        return _PdfDestinationHelper;
    }());
    exports._PdfDestinationHelper = _PdfDestinationHelper;
});