c# - Open PDF file in a specific page using pdfbox -
i have program makes search, example sentence, in pdf files of folder. it's working perfect...
but add feature open in exact page of sentence. , through documentation of pdfbox , not find specific this.
i don't know if let pass by, if enlighten me in grateful
thank
i read question earlier week. @ time, didn't have answer you. stumbled on methods setstartpage() , setendpage() on pdfbox documentation pdftextstripper class , made me think of question , answer. it's been 4 months since asked question, maybe someone. know learned thing or 2 while writing it.
when search pdf file, can search range of pages. functions setstartpage() , setendpage() set range of pages searching. if set start , end page same page number, know page search term found on.
in code below, using windows forms application can adapt code fit application.
using system; using system.windows.forms; using org.apache.pdfbox.pdmodel; using org.apache.pdfbox.util; //the diagnostics namespace needed specify pdf open parameters. more on them later. using system.diagnostics; //specify string searching string searchterm = "golden"; //i using static file path string pdffilepath = @"f:\myfile.pdf"; //load document pddocument document = pddocument.load(pdffilepath); //get number of pages int numberofpages = document.getnumberofpages(); //create instance of text stripper text pdf document pdftextstripper stripper = new pdftextstripper(); //loop through pages. search page page (int pagenumber = 1; pagenumber <= numberofpages; pagenumber++) { //set start page stripper.setstartpage(pagenumber); //set end page stripper.setendpage(pagenumber); //get text page range set above. //in case searching 1 page. //i used tolower method make text lowercase string pdftext = stripper.gettext(document).tolower(); //just fun, display text on each page in messagebox. pdf file has 2 pages. might annoying if have more. messagebox.show(pdftext); //search pdftext search term if (pdftext.contains(searchterm)) { //just fun, display page number on found search term messagebox.show("found search term on page " + pagenumber); //create process. opening pdf document specific page number process myprocess = new process(); //i specified adobe acrobat program open myprocess.startinfo.filename = "acrobat.exe"; //see link below info on pdf document open parameters myprocess.startinfo.arguments = "/a \"page=" + pagenumber + "=openactions\"" + pdffilepath; //start process myprocess.start(); //break out of loop. found our search term , opened pdf file break; } } //close document opened. document.close();
check out adobe pdf document on setting opening parameters of pdf file: http://partners.adobe.com/public/developer/en/acrobat/pdfopenparameters.pdf
Comments
Post a Comment