/* === Page changing functions === */ /** Return property from dictionary of current page */ function pageProperty(propName) { if (isPageAvaliable()) return page().getDictionary().property(propName).ref(); else warn(tr("No page selected!")); } /** Return property from dictionary of current page, adding it with default value if property is not found */ function pagePropertyDef(propName,defValue) { if (isPageAvaliable()) return page().getDictionary().propertyDef(propName,defValue).ref(); else warn(tr("No page selected!")); } /** Moves given page with given difference. * @param page Page to move. * @param diff Difference to add to given page position. * @return New positon of given page. * * New page is moved to page + diff position. Therefore negative diff value * means moving backwards and positive forward. If position would not change * does nothing. *
* If page + diff < 1 resp. > pageCount, page is moved to the 1st position * resp. behind last position. *
* Current (visible) page is changed to moved position. */ function movePage(page, diff) { var currPos=document.getPagePosition(page); var storePos = currPos + diff; if(storePos<1) storePos=1; if(storePos == currPos) return currPos; document.removePage(currPos); document.insertPage(page, storePos); PageSpace.refresh(storePos); // reload go(); return storePos; } /** Inserts given page to given position to current document. * @param page Page instance. * @param pos Position where to store given page. * @return Inserted page position. * * Note that given page can be from different document. *
* If given pos < 1 resp. > page count, it will be stored before first * page, resp. behind last page. *
* Current (visible) page is changed to given pos. */ function insertPage(page, pos) { document.insertPage(page, pos); PageSpace.refresh(pos); // reload go(); return pos; } /** Removes page from given position. * @param pos Page position. * @return Removed page position. * * Checks whether given position is in range. If not, warning dialog is * displayed, otherwise delegate to document.removePage. *
* Current (visible) page is changed to given pos (or lowered to page count). */ function removePage(pos) { if(pos < 1 || pos > document.getPageCount()) { warn(pos+" "+tr("out of range")); return -1; } document.removePage(pos); var displayPos=(pos<=document.getPageCount())?pos:document.getPageCount(); PageSpace.refresh(displayPos); go(); return displayPos; } /** Calculates number of first n elements lower than value from given * array. * @param positions Array of numbers. * @param value Value which to compare. * @return Number of positions array members which are lower than given * value. */ function calcDiff(positions, value, n) { var diff=0; print(positions+" "+value+" "+n); for(j=0; j0 && positions[j]<=value) diff++; print(diff); return diff; } /** Merge given pages with current document. * @param pages Array of pages to merge with. * @param positions Array of positions for pages (see below). * * Inserts all pages from given array each to the position from given array. * positions members are positions of pages in original document where to * insert coresponding page. If positions array is shorter than pages, missing * members are calculated as successors of the highest positions member. * If pages array is shorther than positions array, positions's redundant * (those without page) are ignored. *
* Examples: *
 * // Current document has 3 pages (p1, p2, p3)
 * // We want to merge pp1, pp2, pp3 with current document and
 *
 * // 1.)
 * // pp1 should be inserted to the p1's position (currently 1st)
 * // pp2 should be inserted to the p2's position (currently 2nd)
 * // pp3 should be inserted to the p3's position (currently 3rd)
 * mergeWithPages([pp1,pp2,pp3], [1,2,3])
 * // or with skipped position parameters
 * mergeWithPages([pp1,pp2,pp3], [1,2])
 * // with same meaning as
 * mergeWithPages([pp1,pp2,pp3], [1,2])
 * // or
 * mergeWithPages([pp1,pp2,pp3], [1])
 * // or empty positions means from the begining of document
 * mergeWithPages([pp1,pp2,pp3]. [])
 *
 * // As a result:
 * // pp1, p1, pp2, p2, pp3, p3
 *
 * // 2.)
 * // pp1 should be inserted to the p3's position (currently 1st)
 * // pp2 should be inserted to the p2's position (currently 2nd)
 * // pp3 should be inserted to the p1's position (currently 3rd)
 * mergeWithPages([pp1,pp2,pp3], [3, 2, 1])
 *
 * // As a result:
 * // pp3, p1, pp2, p2, pp1, p3
 *
 * // 3.)
 * // merge pp1, pp2, pp3 behind current pages (join documents with
 * // current in front part. 
 * mergeWithPages([pp1,pp2,pp3], [document.getPageCount()+1])
 *
 * // As a result:
 * // p1, p2 ,p3, pp1, pp2, pp3
 *
 * // 4.)
 * // merge pp1, pp2, pp3 before current pages (join documents with
 * // current in back part. 
 * mergeWithPages([pp1,pp2,pp3], [1,1,1])
 *
 * // As a result:
 * // pp1, pp2 ,pp3, p1, p2, p3
 * 
*/ function mergeWithPages(pages, positions) { var maxPos=0; var pos; // stores all pages which have their position for(i=0;i