Java Integration |
Start page Previous page Next page |
If you prefer Java to LotusScript in the Notes/Domino environment, you can also use n2pdf directly in Java (as of JRE 1.4).
The DLL implements an interface for this purpose which can be addressed via Java Native Interface (JNI).
This makes it possible to use n2pdf in the Java programming language (from Notes/Domino or other Java development environments, such as Netbeans or Eclipse). This means you can also access the n2pdf functions from Java as well.
To simplify use in Java, we provide you with a complete "Java Wrapper Class" ("n2pdfJNI.jar"). All of the function calls and constants of n2pdf are defined for Java in this class.
But of course, you can not only use n2pdf from within Notes/Domino, but you can also use n2pdf in external Java applications or on J2EE servers – n2pdf is very flexible.
The following script corresponds to the logical structure of "Basic principle for LotusScript". The parentheses containing numbers (in red) serve to provide you with a direct comparison with the LotusScript functions.
import lotus.domino.*; import de.softvision.jni.N2pdf; ' (1)
public class CreatePDF {
int jobID = -1;
N2pdf n2pdf = new N2pdf(); // create n2pdf instance
AgentContext agentContext = null;
public CreatePDF( AgentContext agentContext ) { this.agentContext = agentContext; }
private void setHeaderOrFooter ( int isHeader ) { // build the view name String viewName = (isHeader==1?"(Header)":"(Footer)");
try { // get the current database Database db = agentContext.getCurrentDatabase(); if ( db == null ) { return; }
// get the view from the current database View view = db.getView ( viewName ); if ( view == null ) { return; }
// get the first document in the view Document doc = view.getFirstDocument(); if ( doc == null ) { return; }
if ( isHeader == 1 ) { n2pdf.N2PDFAddRTContent ( jobID, n2pdf.N2PDFVALUE_CONTENT_HEADER, n2pdf.N2PDFVALUE_HF_ALL_PAGES, db.getServer(), db.getFilePath(), doc.getUniversalID(), "Body" ); ' (6) Hier als RichText Header } else { n2pdf.N2PDFAddRTContent ( jobID, n2pdf.N2PDFVALUE_CONTENT_FOOTER, n2pdf.N2PDFVALUE_HF_ALL_PAGES, db.getServer(), db.getFilePath(), doc.getUniversalID(), "Body" ); }
} catch(Exception e) { e.printStackTrace(); } }
public void process () { try { int numOfDocs = 9; int currentDoc = 0; int pageBreak = 0; String tocText = "";
// get the current database Database db = agentContext.getCurrentDatabase(); if ( db == null ) { return;}
// get the view View view = db.getView("HelpFile"); if ( view == null ) { return;}
// get the first document from view Document doc = view.getFirstDocument(); if ( doc == null ) { return; }
// get the target file name String pdfFileName = n2pdf.N2PDFCreateTempFile(".pdf"); ' (10)
// initialize a new n2pdf job jobID = n2pdf.N2PDFInit ( 0 ); ' (2)
// n2pdf job initialized? ' (3) if ( jobID < 0 ) { return; }
// set the header for the PDF setHeaderOrFooter ( 1 );
// set the footer for the PDF setHeaderOrFooter ( 0 );
// auto-launch the PDF viewer n2pdf.N2PDFSetOption ( jobID, n2pdf.N2PDFOPTION_SYSTEM_LAUNCH_VIEWER, n2pdf.N2PDFVALUE_TRUE, "" ); ' (4)
// create a TOC for the PDF n2pdf.N2PDFSetOption ( jobID, n2pdf.N2PDFOPTION_TOC, n2pdf.N2PDFVALUE_TRUE, "" );
// convert doc links into PDF links n2pdf.N2PDFSetOption ( jobID, n2pdf.N2PDFOPTION_NOTES_LINK_DOC_MODE, n2pdf.N2PDFVALUE_NOTES_LINK_MODE_IMAGE_LINK, "" );
// enable PDF compression n2pdf.N2PDFSetOption ( jobID, n2pdf.N2PDFOPTION_PDF_COMPRESSION_MODE, n2pdf.N2PDFVALUE_COMPRESSION_DEFLATE, "" );
// set font settings for the default text template n2pdf.N2PDFSetOption ( jobID, n2pdf.N2PDFOPTION_PARAGRAPH_FONT_NAME, "Arial", n2pdf.N2PDFVALUE_DEFAULT_PARAGRAPH_NAME ); n2pdf.N2PDFSetOption ( jobID, n2pdf.N2PDFOPTION_PARAGRAPH_FONT_SIZE, "14", n2pdf.N2PDFVALUE_DEFAULT_PARAGRAPH_NAME ); n2pdf.N2PDFSetOption ( jobID, n2pdf.N2PDFOPTION_PARAGRAPH_FONT_COLOR, n2pdf.N2PDFVALUE_COLOR_PURPLE, n2pdf.N2PDFVALUE_DEFAULT_PARAGRAPH_NAME );
// skip thru all documents while (doc != null) { ' (5)
// count the number of documents currentDoc ++;
// if this is the last document, we do not need a page break if ( currentDoc == numOfDocs ) { pageBreak = 0; } else { pageBreak = n2pdf.N2PDFVALUE_PAGEBREAK_AFTER; }
// build the syntax for the chapter title tocText = "[TOC:" + doc.getItemValueString("ChapterLevel")+"]" + doc.getItemValueString("ChapterTitle")+(char)13;
// add the chapter title (plain text) to the PDF n2pdf.N2PDFAddContent ( jobID, n2pdf.N2PDFVALUE_CONTENT_BODY, n2pdf.N2PDFVALUE_CRLF_AFTER, tocText ); ' (6)
// add the RichText content to the PDF n2pdf.N2PDFAddRTContent ( jobID, n2pdf.N2PDFVALUE_CONTENT_BODY, pageBreak, db.getServer(), db.getFilePath(), doc.getUniversalID(), "Body" ); ' (8)
// get the next document doc = view.getNextDocument(doc); ' (5) }
// create the output PDF file n2pdf.N2PDFProcess ( jobID, pdfFileName, 0 ); ' (9)
} catch(Exception e) { e.printStackTrace(); } } }
|