rpgle - Access the page of a .pdf I load (using pdfbox) (and i'm using RPG) -
i trying modify .pdf load using pdfbox. using rpg , have found java code access page loaded .pdf
pdpage page = (pdpage)pddoc.getdocumentcatalog().getallpages().get(0)
the other code have seen is
list pages = doc.getdocumentcatalog().getallpages(); pdpage page = (pdpage)pages.get( );
the problem having translating code rpg.
the code have right load .pdf template create document catalog , create list , save (the xlenvvar i'm getting classpath).
/free xlsenvvar(); // load existing pdf mypath = jstring_new('/tmp/pdftemplate.pdf'); file = file_new(mypath); mydocument = pddocument_load(file); mycatalog = pddocumentcatalog_new(mydocument); mylist = getallpages(mycatalog); // done file, save myfile = jstring_new('/tmp/pdfbox-demo.pdf'); pddocument_save(mydocument:myfile); // close document object in memory free allocated space pddocument_close(mydocument); // done *inlr = *on; /end-free
get prototype
d get... d pr d extproc(*java: d 'java.util.list': d 'get')
original post above
i thought post pdfbox program there example of using pdfbox in rpg out here on interwebs. working example (it uses companies service programs , sql example shell if use have change those. explained service programs doing can use own companies service programs or write code service programs do). fell free take , post elsewhere leave "written by" block. hope helps working pdfbox.
h/include qcpysrc,hspecstd //***************************************************************** // written by: phillip simmons date: 05 20, 2013 * // special spencer (co-worker) * // , dhanuxp (code400 forums) , * // http://wiki.midrange.com/index.php/pdfbox * // intial example started with. * // * // purpose: load pdf template, modify template * // save modified document. using pdfbox * / //***************************************************************** /include qcpysrc,xcommandp //service program return path on ifs /include qcpysrc,getifspthp //service program return document name /include qcpysrc,getifsnamp //service program document adopt authority of //folder above /include qcpysrc,ifsadoptp //*************************************************************** // data structure sql statement * //*************************************************************** d rcd ds qualified d emplid like(emplid) d lname 90a varying d addr1 like(addr1) d addr2 like(addr2) d csz 50a varying d sds d programname 1 10 //**************************************************************** // prototypes pdfbox * // full documentation of pdfbox available @ * // http://pdfbox.apache.org/index.html * // api docs http://pdfbox.apache.org/apidocs/ * //**************************************************************** /copy qsysinc/qrpglesrc,jni *classpath d xlsenvvar pr extpgm('xlsenvvar') *----------------------------------------------------------------- * global constants d default_userspace_unit_dpi... d c 72 * page sizes in mm d page_size_a0 s like(pdrectangle) d page_size_a1 s like(pdrectangle) d page_size_a2 s like(pdrectangle) d page_size_a3 s like(pdrectangle) d page_size_a4 s like(pdrectangle) d page_size_a5 s like(pdrectangle) d page_size_a6 s like(pdrectangle) d page_size_a0_landscape... d s like(pdrectangle) d page_size_a1_landscape... d s like(pdrectangle) d page_size_a2_landscape... d s like(pdrectangle) d page_size_a3_landscape... d s like(pdrectangle) d page_size_a4_landscape... d s like(pdrectangle) d page_size_a5_landscape... d s like(pdrectangle) d page_size_a6_landscape... d s like(pdrectangle) d page_width_a0 c 841 d page_height_a0 c 1189 d page_width_a1 c 594 d page_height_a1 c 841 d page_width_a2 c 420 d page_height_a2 c 594 d page_width_a3 c 297 d page_height_a3 c 420 *210 orginal size d page_width_a4 c 216 *297 orginal size d page_height_a4 c 279 d page_width_a5 c 148 d page_height_a5 c 210 d page_width_a6 c 105 d page_height_a6 c 148 *american pages different size iso standard *----------------------------------------------------------------- * pdrectangle d pdrectangleclass... d c 'org.apache.pdfbox.pdmodel.common.pd- d rectangle' d pdrectangle s o class(*java:pdrectangleclass) * pdrectangle() d pdrectangle_new... d pr like(pdrectangle) d extproc(*java: d pdrectangleclass: d *constructor) d width like(jfloat) value d height like(jfloat) value *----------------------------------------------------------------- *pddocumentcatalog d documentcatalog... d s o class(*java : d pddocumentcatalogclass) d pddocumentcatalogclass... d c 'org.apache.pdfbox.pdmodel.pddocumen- d tcatalog' d getdocumentcatalog... d pr o class(*java : d pddocumentcatalogclass) d extproc(*java : d pddocumentclass: 'getdocumen+ d tcatalog' ) * d getallpages pr o class(*java : 'java.util.list' ) d extproc(*java : d pddocumentcatalogclass : 'get+ d allpages' ) *----------------------------------------------------------------- *java.lang.string d jstringclass... d c 'java.lang.string' d jstring_new pr like(jstring) d extproc(*java d :jstringclass d :*constructor) d create_from 1024a varying const *----------------------------------------------------------------- *pddocument d pddocumentclass... d c 'org.apache.pdfbox.pdmodel.pddocumen- d t' d pddocument s o class(*java:pddocumentclass) * pddocument() d pddocument_new... d pr like(pddocument) d extproc(*java: d pddocumentclass: d *constructor) * pddocument.addpage() d pddocument_addpage... d pr d extproc(*java: d pddocumentclass: d 'addpage') d ppage like(pdpage) *pddocument.load() d pddocument_load... d pr like(pddocument) d extproc(*java : pddocumentclass: d 'load' ) d static d path like(jstring) d pddocument_save... d pr extproc(*java : pddocumentclass: d 'save' ) d savepath like(jstring) * d importpage pr o class(*java : 'org.apache.pdfbox.p+ d dmodel.pdpage' ) d extproc(*java : 'org.apache.pdfbox+ d .pdmodel.pddocument' : 'importpage+ d ' ) d* parameter prototype declaration java type: pdpage d pdpage like(pdpage) * pddocument.close() d pddocument_close... d pr d extproc(*java: d pddocumentclass: d 'close') *----------------------------------------------------------------- * font classes d pdfontclass c 'org.apache.pdfbox.pdmodel.font.pdfo- d nt' d pdfont s o class(*java:pdfontclass) *----------------------------------------------------------------- * pdpage d pdpageclass c 'org.apache.pdfbox.pdmodel.pdpage' d pdpage s o class(*java:pdpageclass) d pdpage_get pr o class(*java : 'java.lang.object' ) d extproc(*java : 'java.util.list' : d 'get' ) d indexno 10i 0 value *----------------------------------------------------------------- * pdtype1font d pdtype1fontclass... d c 'org.apache.pdfbox.pdmodel.font.pdty- d pe1font' d pdtype1font s o class(*java:pdtype1fontclass) * pdtype1font() d pdtype1font_new... d pr like(pdtype1font) d extproc(*java: d pdtype1fontclass: d *constructor) d fontname like(jstring) options(*omit) * pdtype1font.getstandardfont d pdtype1font_getstandardfont... d pr like(pdtype1font) d extproc(*java: d pdtype1fontclass: d 'getstandardfont') d static d fontname like(jstring) *----------------------------------------------------------------- * pdpagecontentstream d pdpagecontentstreamclass... d c 'org.apache.pdfbox.pdmodel.edit.pdpa- d gecontentstream' d pdpagecontentstream... d s o class(*java: d pdpagecontentstreamclass) * pdpagecontentstream() d pdpagecontentstream_new... d pr like(pdpagecontentstream) d extproc(*java: d pdpagecontentstreamclass: d *constructor) d pdocument like(pddocument) d ppage like(pdpage) d appendcontent n value d compress n value * pdpagecontentstream.begintext() d pdpagecontentstream_begintext... d pr d extproc(*java: d pdpagecontentstreamclass: d 'begintext') * pdpagecontentstream.setfont() d pdpagecontentstream_setfont... d pr d extproc(*java: d pdpagecontentstreamclass: d 'setfont') d font like(pdfont) d fontsize like(jfloat) value * pdpagecontentstream.movetextpositionbyamount() d pdpagecontentstream_movetextpositionbyamount... d pr d extproc(*java: d pdpagecontentstreamclass: d 'movetextpositionbyamount') d x like(jfloat) value d y like(jfloat) value * pdpagecontentstream.drawstring() d pdpagecontentstream_drawstring... d pr d extproc(*java: d pdpagecontentstreamclass: d 'drawstring') d text like(jstring) * pdpagecontentstream.endtext() d pdpagecontentstream_endtext... d pr d extproc(*java: d pdpagecontentstreamclass: d 'endtext') * pdpagecontentstream.close() d pdpagecontentstream_close... d pr d extproc(*java: d pdpagecontentstreamclass: d 'close') *----------------------------------------------------------------- * java.awt.color d jcolorclass... d c 'java.awt.color' d jcolor s o class(*java: d jcolorclass) // new color(int, int, int) d jcolor_new_fromintrgb... d pr like(jcolor) d extproc(*java d :jcolorclass d :*constructor) d r like(jint) value d g like(jint) value d b like(jint) value * pdpagecontentstream.setnonstrokingcolor(java.awt.color color) d pdpagecontentstream_setnonstrokingcolor... d pr d extproc(*java: d pdpagecontentstreamclass: d 'setnonstrokingcolor') d color like(jcolor) *----------------------------------------------------------------- * utilities d mmtounits pr like(jfloat) d pmm like(jfloat) value d createpagesizes... d pr *----------------------------------------------------------------- *pdfbox variables d myfontname s like(jstring) d myfont s like(pdtype1font) d mypagecontent s like(pdpagecontentstream) d mycolor s like(jcolor) d sourcedoc s like(pddocument) d targetdoc s like(pddocument) d documentcat s like(documentcatalog) d pdf_path1 s like(jstring) d pdf_save s like(jstring) d mypage s like(pdpage) d newpage s like(pdpage) d myallpages s o class(*java : 'java.util.list' ) d importpageret s like(pdpage) *rpg variables d docname s 255a varying d pagenumber s 10i 0 d tdate s d d cdate s 10 varying d tempsave s 150 varying d tempload s 150 varying d directorys s 150 varying d directoryl s 150 varying d cdirectory s 150 varying d loadstring s 150 varying d checkstring s 150 varying d sqlstm s 1000a varying d q s 1a inz(x'7d') d cmds s 1500a varying d cmdl s 1500a varying d*-------------------------------------------------- d* procedure name: addtext d* purpose: d* returns: d* parameter: mytext d*-------------------------------------------------- d addtext pr d mytext 5000a varying const d indent 10i 0 const d offset 10i 0 const * d mystring s like(jstring) /free tdate = %date(); cdate = %char(tdate:*usa/); //the page of loaded pdf going want retrieve pagenumber = 0; //contains classpath xlsenvvar(); //getifspth , getifsnam service programs company uses //to path document. further down in pdf_path1(loadstring) //you can pass hardcoded string ex pdf_path1('/tmp/demodoc.pdf'); // requested template file docname = %trim(getifspth(programname:1)) + %trim(getifsnam(programname:1)); //directory file moved directoryl = %trim(getifspth(programname:2)); //getting path load pdf document checkstring = %trim(getifspth(programname:3)) + %trim(getifsnam(programname:3)); //check see if template exists if dlt //this needed because later when move document final //destination if document moved exists move //fail. cause modified template left behind , when //the program ran later earlier cpy obj fail , //load modified templete. callp(e) xcommand('del objlnk('+q+%trim(checkstring)+q+')'); //string pass xcommand() cmdl = 'cpy obj(' +q + %trim(docname) +q + ')' + ' todir(' +q + %trim(directoryl) +q + ')'; xcommand(cmdl); //create blank pdf document. page modified. targetdoc = pddocument_new(); //getting path load pdf document loadstring = %trim(getifspth(programname:3)) + %trim(getifsnam(programname:3)); //the string holds path pdf template pdf_path1 = jstring_new(loadstring); //load pdf document sourcedoc = pddocument_load(pdf_path1); //get document catalog documentcat = getdocumentcatalog(sourcedoc); //get list of pages myallpages = getallpages(documentcat); //get specific page want mypage = pdpage_get(myallpages:pagenumber); sqlstm = 'select emplid,lname, addr1' + ' addr2, csz' ' empmst'; exec sql prepare s1 :sqlstm; exec sql declare rcd cursor s1; exec sql open rcd; exec sql fetch rcd :rcd; dow sqlcod = 0; //import , copy new page. page used //in content stream. importpageret = importpage(targetdoc:mypage); // create content stream can add content page // modifying existing page append *on // compression *off mypagecontent = pdpagecontentstream_new(targetdoc:importpageret: *on:*off); //adding text preloaded pdf addtext(rcd.emplid :23 :240); addtext(rcd.lname :23 :231); addtext(rcd.addr1 :23 : 227); addtext(rcd.addr2 :23 : 223); addtext(rcd.csz :23 : 219); addtext(cdate :180 : 240); exec sql fetch rcd :rcd; enddo; //temparaly saving /tmp move mov command tempsave = %trim(getifspth(programname:3)) + %trim(getifsnam(programname:3)); //the string holds path pdf saved pdf_save = jstring_new(tempsave); //done file, save pddocument_save(targetdoc : pdf_save); // close document object in memory free allocated space pddocument_close(sourcedoc); pddocument_close(targetdoc); //create directory file moved cdirectory = %trim(getifspth(programname:4))+ %char(%subdt(%date():*years)); xcommand('crtdir dir(' +q + cdirectory +q +')'); //retrieve path file saved directorys = %trim(getifspth(programname:4)) + %char(%subdt(%date():*years)) + '/' + %trim(getifsnam(programname:4)); //move file correct directory cmds = 'mov obj(' +q + tempsave +q + ')' + ' toobj(' +q + %trim(directorys)+ q + ')'; xcommand(cmds); //adopt auth of folder ifsadopt(directorys); exec sql close rcd; *inlr = *on; /end-free * mmtounits p mmtounits b d pi like(jfloat) d pmm value like(jfloat) d funits s like(jfloat) /free funits = 1 / (25.4) * default_userspace_unit_dpi * pmm; return funits; /end-free p mmtounits e * creatpagesizes p createpagesizes... p b d pi /free page_size_a0 = pdrectangle_new(mmtounits(page_width_a0): mmtounits(page_height_a0)); page_size_a1 = pdrectangle_new(mmtounits(page_width_a1): mmtounits(page_height_a1)); page_size_a2 = pdrectangle_new(mmtounits(page_width_a2): mmtounits(page_height_a2)); page_size_a3 = pdrectangle_new(mmtounits(page_width_a3): mmtounits(page_height_a3)); page_size_a4 = pdrectangle_new(mmtounits(page_width_a4): mmtounits(page_height_a4)); page_size_a5 = pdrectangle_new(mmtounits(page_width_a5): mmtounits(page_height_a5)); page_size_a6 = pdrectangle_new(mmtounits(page_width_a6): mmtounits(page_height_a6)); page_size_a0_landscape = pdrectangle_new(mmtounits(page_height_a0): mmtounits(page_width_a0)); page_size_a1_landscape = pdrectangle_new(mmtounits(page_height_a1): mmtounits(page_width_a1)); page_size_a2_landscape = pdrectangle_new(mmtounits(page_height_a2): mmtounits(page_width_a2)); page_size_a3_landscape = pdrectangle_new(mmtounits(page_height_a3): mmtounits(page_width_a3)); page_size_a4_landscape = pdrectangle_new(mmtounits(page_height_a4): mmtounits(page_width_a4)); page_size_a5_landscape = pdrectangle_new(mmtounits(page_height_a5): mmtounits(page_width_a5)); page_size_a6_landscape = pdrectangle_new(mmtounits(page_height_a6): mmtounits(page_width_a6)); return; /end-free p e * * p*-------------------------------------------------- p* procedure name: addtext p* purpose: write text pdpage p* returns: p* parameter: mytext p*-------------------------------------------------- p addtext b d addtext pi d mytext 5000a varying const d indent 10i 0 const d offset 10i 0 const * d mystring s like(jstring) /free // set font type myfontname = jstring_new('times-roman'); myfont = pdtype1font_getstandardfont(myfontname); pdpagecontentstream_setfont(mypagecontent:myfont:10); // set color of font add // isn't needed if writing blank page since // adding text loaded page need set color of text. mycolor = jcolor_new_fromintrgb(0:0:0); pdpagecontentstream_setnonstrokingcolor(mypagecontent:mycolor); // prepare write text content stream pdpagecontentstream_begintext(mypagecontent); // move cursor possition,indent x mm left of page // offset x mm bottom of page. pdpagecontentstream_movetextpositionbyamount(mypagecontent: mmtounits(indent):mmtounits(offset)); // create text string want write mystring = jstring_new(mytext); // write string content stream pdpagecontentstream_drawstring(mypagecontent:mystring); // done writing text content stream pdpagecontentstream_endtext(mypagecontent); //we done content stream pdpagecontentstream_close(mypagecontent); /end-free p addtext e
thought add couple of comments after reading code. have make new folder each year. crtdir doing. if directory exists fine contents in created directory safe, crtdir doesn't overwrite directory. reason loading template 1 location /tmp , saving /tmp before moving again when working pdfbox jstring_new(' ') maxing out @ 30 characters (including / , spaces , . ). mystring = jstring_new('/tmp/2013/myreportnamethatistoolong.pdf') string 39 characters long , string become truncated. olong.pdf lost when make string. can make string rpg code , pass string jstring_new() , longer 30 characters , works.
you want list.get() return org.apache.pdfbox.pdmodel.pdpage
rpg typed, can't cast same way java can. instead, create prototype return object want manipulate. along lines, may end using get() retrieve several different kind of list elements. rpg doesn't overloading either. i'd create separate get() prototype each object i'd handle. i'd have getpage() , getthread() , on. each 1 'java.util.list:get' return different object type.
the rpg list @ www.midrange.com source rpg.
Comments
Post a Comment