SmartJRtf - Java RTF Server Application Tutorial

  • About RTF Server API
  • What archive to download?
  • Typing the URL of your document
  • Run samples
  • Jar Files
  • PDF Generation
  • Silent printing RTF
  • Conversion of RTF to other formats
  • Examples

  • About RTF Server API

    The server APIs lets you silent print of an existing RTF. It lets you convert your RTF to other file formats such as PDF, TIFF, PostScript documents and image formats such as PNG, GIF, JPG/JPEG, BMP, etc. When converting the RTF to image formats it lets you output each page as resized (smaller or larger) ones. For example, to show a thumbnail image of a page you can create a small image of a page. For clear view of the page, you can create an enlarged picture of a page.

    Your RTF can be a local file or a remote file or a server page that dynamically generates a PDF.

    Back to Top
    What archive to download?

    Visit our website and download the server archive that might look like product_productVersion_server.zip. After downloading unzip this ZIP archive to a local folder (let us say c:/rtf/).

    Back to Top
    Typing the URL of your document

    Input an URL and does not matter if the document is located in local file system, in a remote web server or a server program that dynamically generate one. An URL with appropriate URL syntax is all that required for printing or viewing it.

    Examples of typing a local file URL:

  • Window: file:///c:/rtf/sample.rtf or file:/c:/rtf/sample.rtf
  • Mac OS: file:Macintosh HD/rtf/sample.rtf
  • Unix/Linux: file:///tmp/rtf/sample.rtf
  • Examples of typing remote URL:

  • HTTP: http://www.activetree.com/smartjeditor/rtf/sample.rtf
  • HTTPS: https://www.myserver.com/rtf/sample.rtf
  • FTP: ftp://ftp.myserver.com/rtf/sample.rtf
  • Back to Top
    Run samples

    There is an executable script file (.bat and .sh) for each of the demo JAVA sample in the "examples" directory. For Rtf2PdfDemo.java sample there is a Rtf2PdfDemo.bat (for Windows) and a Rtf2PdfDemo.sh (for Unix/Linux) in the installation/archiveName/bin/server/ directory.

    For running these *.bat or *.sh script files open a command shell, go to the directory where this script are located, then simply type one of the script file name and press enter. It will show you the "Usage" help for further trying the same but with the command line arguments.

    There is a sample RTF file SampleDocument.rtf in the "installation/ArchiveName/bin" directory.

    Back to Top
    Jar Files

    When including RTF viewer Swing component to your application or running it as an independent Java program you have to include the following jars in the class path. All jars mentioned (including PDF generation jars mentioned next) might be already available in the downloaded archive. If not found in the downloaded archive download it from our website and include them in the class path.

  • Required jars:

  • sjrtf_license.jar
    smartjcommon.jar
    sjrtf_server.jar
  • Optional jars:

  • sjrtf_demo.jar - This jar is created out of those sample .java files available for you in the "examples" directory of the downloaded archive. If the demo programs are used without modification include this jar in the class path.

    Back to Top
    PDF Generation

    This API is currently supporting PDF conversion of an RTF file using our PDF generation API. You therefore must include the following PDF creation jars in the class path (if PDF is enabled for your application).

  • PDF Jars

  • sjpdfgen.jar

    Back to Top
    Silent printing RTF

    RtfPrinter - is the class you use for silent printing of an RTF document. You primarily require the RTF source and a PageFormat instance. If you do not pass a PageFormat it will use the default PageFormat.

    Print using API provided print methods

    - Data source - one of the argument to these methods is byte[], File, String (file or URL), or URL.
    - PageFormat - Paper represented by a PageFormat to print the output. You can choose to use setChoosePaperSourceByPageSize() to true so it will automatically find a matched paper size for each page of your document.
    - Printer to print - the PrintService or DocPrintJobrepresenting a printer to print.
    - Print requiest attribute set - is a set of printing relared arrtibutes you can create and add into the PrintRequestAttributeSet instance. When printing these attributes (if supported) will be used by the underlying printer driver for printing.

  • public void print(byte[] data, PageFormat mediaPageFormat, DocPrintJob printerToPrint)
  • public void print(byte[] data, PageFormat mediaPageFormat, DocPrintJob printerToPrint, PrintRequestAttributeSet printReqAttrSet)
  • public void print(byte[] data, PageFormat mediaPageFormat, PrintService printerToPrint)
  • public void print(byte[] data, PageFormat mediaPageFormat, PrintService printerToPrint, PrintRequestAttributeSet printReqAttrSet)
  • public void print(File fileName, PageFormat mediaPageFormat, PrintService printerToPrint)
  • public void print(String fileName, PageFormat mediaPageFormat, DocPrintJob printerToPrint)
  • public void print(String fileName, PageFormat mediaPageFormat, PrintService printerToPrint)
  • public void print(String fileName, PageFormat mediaPageFormat, PrintService printerToPrint, PrintRequestAttributeSet printReqAttrSet)
  • public void print(URL url, PageFormat mediaPageFormat, DocPrintJob printerToPrint)
  • public void print(URL url, PageFormat mediaPageFormat, DocPrintJob printerToPrint, PrintRequestAttributeSet printReqAttrSet)
  • public void print(URL url, PageFormat mediaPageFormat, PrintService printerToPrint)
  • public void print(URL url, PageFormat mediaPageFormat, PrintService printerToPrint, PrintRequestAttributeSet printReqAttrSet)
  • Print using getPageable() methods
    For getting a java.awt.print.Pageable out of the document source use the following methods. Call to Pageable.getPrintable(index) is an instance of AbstractPage). You might iterate through the getPrintable(index) pages so you can selectively print pages and eliminate those pages you may not print at all. Use the code segment at the end of this section to print your Pageable.

  • public Pageable getPageable(byte[] data) - Returns a Pageable with all pages of the document using byte[] input.
  • public Pageable getPageable(File fileName) - Returns a Pageable with all pages of the document using File input.
  • public Pageable getPageable(URL url) - Returns a Pageable with all pages of the document using URL input.
  • public Pageable getPageable(Object source, PageFormat mediaPageFormat) - Returns a Pageable with all pages of the document. The source can be a String (representing local file, a local or web URL), File, URL or byte[] content of your document source. It will use the PageFormat instance for printing the output.
  • public Pageable getPageable(Object source, PageFormat mediaPageFormat, PrintService printerToPrint) - Returns a Pageable with all pages of the document. The source can be a String (representing local file, a local or web URL), File, URL or byte[] content of your document source. It will use the PageFormat instance for printing the output. The only different in this method compared to the prior one is that it has PrintService argument used for calculating printer margins to use if you choose to set the printer margin using setUsePrinterMargins() to true. API will therefore respect the printer margins represented by this PrintService of the printer where you may print it.
  • When you use getPageable() and get a java.awt.print.Pageable you can use the following code segment to print to a selected printer.

      java.net.URL url = null; //populate it - you can also use String, File or byte[] instead.
      PrintService userSelectedPrinter = null; //populate it - this represents a printer.
      //PageFormat - change it if require some other paper size.
      PageFormat printerSupportedPageFormat = AtCommonUtil.getSelectedPageFormat(MediaSizeName.NA_LETTER);
      //Now print.
      try {
        RtfPrinter rtfPrinter = new RtfPrinter();
        //Print using a tray number.
        Pageable pageable = rtfPrinter.getPageable(url, printerSupportedPageFormat, userSelectedPrinter);
        DocPrintJob pj = userSelectedPrinter.createPrintJob();
        HashDocAttributeSet docAttrs = new HashDocAttributeSet();
        //NOTE - Without "docAttrs" above IBM JRE might throw Null pointer exception
        SimpleDoc doc = new SimpleDoc(pageable, docFlavor, docAttrs);
        HashPrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        pj.print(doc, printRequestAttributeSet);
        rtfPrinter.close();
      }catch(Throwable t) {
        //May be do some error processing here.
      }
      

    Back to Top
    Conversion of RTF to other formats

    Your RTF can be converted to these formats:

    Document formats

  • PDF
  • TIFF
  • PostScript (PS)
  • Image formats

  • PNG
  • GIF
  • BMP
  • JPG/JPEG
  • Back to Top
    Examples

    There are some RTF server examples in the downloaded server archive in its "examples" directory. We suggest you to take a look in these Java sample files and if necessary create your own custom server programs.

  • RtfSilentPrintDemo - Takes an RTF input and prints to the default printer
  • Rtf2PdfDemo - Using an RTF input file it produce a PDF output file
  • Rtf2PostScriptDemo - With an RTF as input it produce a PostScript output file
  • Rtf2ResizedTiffDemo - Converts an RTF to a TIFF document
  • Rtf2ResizedBMPDemo - Converts RTF and creates BMP image for each page
  • Rtf2ResizedGIFDemo - Produces a GIF image for each page of the RTF
  • Rtf2ResizedJPEGDemo - Produces a JPEG image for each page of the RTF
  • Rtf2ResizedPNGDemo - Produces a PNG image for each page of the RTF
  • Back to Top

    Copyright © Activetree, Inc. All rights reserved.
    Web: http://www.activetree.com
    Email: sales@activetree.com
    Tel: +1 408-791-8036 Fax: +1 408-716-8450