Printing of Documents - Separate into separate jobs

Ok so I have the below code for printing. The problem I have is that I have a document which needs to be separated into 2 or more print jobs. The reason is because it needs to print the first page on headed paper for each part of the document.

I’m wondering if it is possible to separate the printing of my doc into 2 or more jobs thus printing the headed paper on first page of each job. I can’t do this by calculating page numbers as they will be different everytime. I was thinking maybe its possible to detect a keyword and make an uno call to tell printer to create a new job for the second part of document. Any help is much appreciated. thanks

/**
 * Method printDoc - print the current document to the default printer
 */
public void printDoc()
{
	try
	{

	 PropertyValue[] printOpts = new PropertyValue[1];
	 printOpts[0] = new PropertyValue();
	 printOpts[0].Name = "Wait";
	 printOpts[0].Value = new Boolean( true );

	 System.out.println( "Printing now" );
	 officeDoc.print( printOpts );
	 System.out.println( "Printing finished" );			}
	catch (Throwable e)
	{
		System.out.println( "Printing failed" );
		e.printStackTrace();
	}
}


/**
 * Method printDoc - print the current document to a named printer,
 * specify the pages to print (e.g. "1, 3, 4-7, 9-")
 * @param printerName - the name of the printer to print to
 * @param pages - the number of pages to print
 */
public void printDoc(String printerName, String pages)
{
	try
	{
		System.out.println("OODocument - Attempting to print page(s) " + pages + " to " + printerName + " printer.");

	    PropertyValue[] printerDesc = new PropertyValue[1];
	    printerDesc[0] = new PropertyValue();
	    printerDesc[0].Name = "Name";
	    printerDesc[0].Value = printerName;

		System.out.println( "Setting printer to " + printerName );
	    officeDoc.setPrinter(printerDesc);


	    PropertyValue[] printOpts = new PropertyValue[2];
	    printOpts[0] = new PropertyValue();
	    printOpts[0].Name = "Pages";
	    printOpts[0].Value = pages;

		//set printing to be synchronous
		//see http://api.openoffice.org/docs/common/ref/com/sun/star/view/PrintOptions.html
	    printOpts[1] = new PropertyValue();
	    printOpts[1].Name = "Wait";
	    printOpts[1].Value = new Boolean( true );


		System.out.println( "Printing now" );
		long start = System.currentTimeMillis();
		officeDoc.print(printOpts);
		long end = System.currentTimeMillis();
		System.out.println( "Printed. Took " + (end-start) + "ms" );
	}
	catch (Throwable e)
	{
		e.printStackTrace();
	}
}

Answered my own question below

	public int searchPageNumber()
{
	XController xController = OODocument.getCurrentDocument().getXFrame().getController();

	XTextViewCursorSupplier supTextViewCursor =
				(XTextViewCursorSupplier) UnoRuntime.queryInterface(
					XTextViewCursorSupplier.class, xController);

	XTextViewCursor curTextView = supTextViewCursor.getViewCursor();
	
	// gets the page cursor and assigns the text view cursor to the page
	XPageCursor curPage =
				(XPageCursor) UnoRuntime.queryInterface(
					XPageCursor.class, curTextView);
	System.out.println("The current page number is " + curPage.getPage());

	// gets the model
	XModel model = xController.getModel();
	// assigns model to the document
    XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, model);
	// Xsearchable so we can search the text
	XSearchable xSearchable = (XSearchable) UnoRuntime.queryInterface(XSearchable.class, xTextDocument); 
    XSearchDescriptor xsd = (XSearchDescriptor) xSearchable.createSearchDescriptor(); 

    xsd.setSearchString("zzzzz");

    XInterface xfi = (XInterface) xSearchable.findFirst(xsd); 
    if (xfi != null) { 
        XTextRange xStart = (com.sun.star.text.XTextRange) UnoRuntime.queryInterface( 
                com.sun.star.text.XTextRange.class, xfi); 

        curTextView.gotoRange(xStart, false); 
    } 
    
	System.out.println("current page = " + curPage.getPage());
	return curPage.getPage();
}

AND THE JAVASCRIPT

var totalPages = soApplet.getNumberOfPages();
				//alert("totalPages=" + totalPages); 
				var x = soApplet.searchPageNumber();
  				//alert(x);       	    
    	    	//printer accepts in the format eg)"2-5" so quote added.      	    
    	    	var newVal = x-1; 
    	    	//alert("newVal ="+newVal);
    	    	var afterVal = x+1;
  				//alert("afterVal ="+afterVal);
  				var printPaper ='"'+"2-"+newVal+'"';
  				//alert("printPaper ="+printPaper);
    	    	if (newVal == 0)
    	    	{
    	    		//alert("newval is equal to 0 hence just print normally");
    	    		soApplet.printDocument(PLAIN_PAPER, "2-");
    	    	}
    	    	else if (newVal == 1)
    	    	{
    	    		//alert("newval is equal to 1");
    	    		soApplet.printDocument(LETTER_HEAD, x);
  					soApplet.printDocument(PLAIN_PAPER, afterVal+"-");
    	    	}
    	    	else
    	    	{
    	    		//alert("newval is NOT equal to 1");
       		 		soApplet.printDocument(PLAIN_PAPER,printPaper);	 									
      				soApplet.printDocument(LETTER_HEAD, x);
      				soApplet.printDocument(PLAIN_PAPER, afterVal+"-");
  				}