« Selection and Navigation Drawing and colors »
Page management
How to add, remove, count and manage the pages of your document. By Ludwig Zeller
Adding pages
#includepath "~/Documents/;%USERPROFILE%Documents"; #include "basiljs/bundle/basil.js"; function draw() { // add one page at the end of the document and jump to it b.addPage(); b.text("Example 1", b.width / 2, b.height / 2, 100, 100); } b.go();
Removing pages
// removes current page b.removePage(); // fill up again... b.addPage(); // removes page 1 b.removePage(1);
Using references to pages
#includepath "~/Documents/;%USERPROFILE%Documents"; #include "basiljs/bundle/basil.js"; function draw() { var myPage = b.addPage(); var myTextFrame = b.text("hello world", 20, 20, 100, 20); // add a text frame on that page b.page(1); // leave that page b.page(myTextFrame); // go to the page where this PageItem sits // removes myPage right away... you won't see it ever b.removePage(myPage); } b.go();
- addPage() and page() return a reference to the new or determined page that you can store in a variable.
- You can pass a PageItem such as a TextFrame to page() in order to jump to the page that contains the given PageItem
Populating larger documents and adding between pages
#includepath "~/Documents/;%USERPROFILE%Documents"; #include "basiljs/bundle/basil.js"; function draw() { // run this on an empty document with only one page! // add pages until 20 for( var i = b.pageCount(); i < 20; i++) { b.addPage(); b.text("Example 2-" + i, b.width / 2, b.height / 2, 100, 100); } // set location of insertion b.addPage(b.AT_END); // default b.text("Example AT_END", b.width / 2, b.height / 2, 100, 100); b.addPage(b.AT_BEGINNING); b.text("Example AT_BEGINNING", b.width / 2, b.height / 2, 100, 100); // adds a page before page 15 b.page(10); // set current page b.addPage(b.BEFORE); // this refers to the current page b.text("Example BEFORE 10 becomes the new 10", b.width / 2, b.height / 2, 100, 100); // guess what! b.page(15); b.addPage(b.AFTER); b.text("Example AFTER 15 becomes 16", b.width / 2, b.height / 2, 100, 100); b.println(b.pageCount()); } b.go();
- This example rises the page count to 20
- You can use b.AT_END, b.AT_BEGINNING, b.BEFORE and b.AFTER to define where the new page should be inserted