StarOffice 8 Server - PDF Converter
この本のみを検索
PDF 文書ファイルをダウンロードする (626 KB)

Chapter 4 Sample Code

As a web service, StarOffice 8 Server - PDF Converter is mainly triggered via programming. In the following, you will find two example ways to use the web service and an overview of the service parameters.

Sample JSP

A sample JSP file for a PDF conversion web application can be found in <socs-installation-dir>/engine/webapps/soserv/convert.jsp. This file is a template for own conversion JSPs. The sample JSP is fully working and can be accessed using a web browser. For example, for a StarOffice 8 Server - PDF Converter that runs locally and listens on port 8080, the respective URL is http://localhost:8080/soserv/convert.jsp.

A screenshot of the page which is opened in the browser is given below.

Figure 4–1 Screen shot of StarOffice 8 Server - PDF Converter in a web browser.

Browser image of converter

Use the Browse button to select a file to be converted, and click the Convert button to send the file to the server for conversion. After conversion, a dialog opens, where you can choose to download or open the converted file.

The StarOffice 8 Server - PDF Converter browser page shows the following options:

  • Page Range

    Exports the pages that you type in the box. To export a range of pages, use the format, 3-6. To export single pages, use the format, 7;9;11. To export a combination of page ranges and single pages, use the format, 3-6;8;10;12. To export all of the pages, leave the box empty.

  • PDF/A-1

    Export the document as PDF/A-1. This specification stores all required information, for example fonts, in the file. This is needed for archive documents and used in governmental environments.

  • Tagged PDF

    Write tags in the converted document.


    Note –

    The Tagged PDF option can significantly increase the file size of the PDF document.


  • Export notes

    Exports notes in Writer and Calc documents as PDF notes.

  • Use transition effects

    Converts an Impress slide transition effect to the equivalent PDF effect.

  • Create hybrid file

    Hybrid files are PDF documents with the original ODF document embedded.

  • Lossless compression

    Preserves all pixels in the original image.

  • JPEG compression

    Compresses the pixels in the original image. To preserve the appearance of the image, use the high quality setting. If you want to reduce the file size, but with the risk of introducing file artefacts, use the low quality setting.

  • Reduce image resolution

    Reduces the size of the image by reducing the number of pixels per inch. Use this option to select the target resolution for the converted images.

  • Encrypt the PDF document

    Requires that you enter a password to open the converted PDF document.

  • Set open password / Confirm

    Specifies the password for the converted document.


    Note –

    This option is only available when you select the Encrypt the PDF document option.


  • Restrict permissions

    Sets the permissions for opening the converted document.

  • Set permission password / Confirm

    Sets the password that you need to enter to change the restrictions of the PDF document.


    Note –

    This option is only available when you select the Restrict permissions option.


  • Printing

    Sets the printing permissions for the PDF document.


    Note –

    This option is only available when you select the Restrict permissions option.


    • Not permitted

      Does not allow you to print the PDF document.

    • Low resolution

      Allows you to print the PDF document only at a low resolution.

    • High resolution

      Allows you to print the PDF document in high resolution.

  • Changes


    Note –

    This option is only available when you select the Restrict permissions option.


    • Not permitted

      Prevents changes to the PDF document.

    • Inserting, deleting and rotating pages

      Allows you to insert, delete, and rotate pages in the PDF document.

    • Filling in form fields

      Allows you to type in form fields in the PDF document.

    • Commenting, filling in form fields

      Allows you to add comments to the PDF document.

    • Any, except extracting pages

      Allows you to make changes to the PDF document, but does not allow you to extract pages.

  • Enable copying of content

    Allows you to copy the content of the PDF document.


    Note –

    This option is only available when you select the Restrict permissions option.


  • Enable text access for accessibility tools

    Allows you to access the text content of the PDF document with accessibility tools.


    Note –

    This option is only available when you select the Restrict permissions option.


Sample Java Web Service client

The main way to use the web service is via custom clients, embedded in specialized applications. It does not make sense to discuss every particular language binding here, but shown is the way to obtain a connection to the web service using the JAX-WS implementation of Web Services provided by Sun Microsystems.

The best way to create a JAX-WS client is to use Netbeans. Create a new file with the “Web Service Client” template in Netbeans with the provided wsdl.

A client could look like the following.

package soserverclient;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Holder;


public class ConversionClient {
    
    String serviceURL="http://localhost:8080/soserv/SOConverter";
    ConverterSEI converter = null;
    ConvertMime params = null;
    
    /** Creates a new instance of ConversionClient */
    public ConversionClient(String serviceUrl) {
        if (serviceUrl != null)
            serviceURL = serviceUrl;
        
        try {
            converter = new Converter().getConverterSEIPort();
            ((BindingProvider)converter).getRequestContext().put(
                    BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
            
            params = new ConvertMime();
            params.setTargetType("PDF");
        } catch (Throwable e) {
            System.out.println("caught exception: " + e.getClass().getName());
            e.printStackTrace();
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2) {            

            System.out.println("usage: ConversionClient <inputdir> <outputdir> [<service url>]");
            System.exit(-1);
        }
        
        String serviceUrl = null;
        if (args.length > 2) 
            serviceUrl = args[2];
        
        System.out.println("ConversionClient...");
        ConversionClient dc = new ConversionClient(serviceUrl);     
        dc.convertDocs(args[0], args[1] );
    }
    
    public void convertDocs(String input, String output) {
        File dir = new File(input);        
        
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            String childurl 
                    = dir.getAbsolutePath() + File.separator + children[i];
            File f = new File(childurl);
            
            if (f.isDirectory()) {
                File o = new File(output + File.separatorChar + children[i]);
                if (o.exists()) {
                    // TODO: error handling
                } else {
                    o.mkdir();
                }                
                convertDocs(
                        childurl, output + File.separatorChar + children[i]);
            } else {
                String name 
                        = children[i].substring(
                            0, children[i].lastIndexOf('.')) + ".pdf";
                File o = new File(output + File.separatorChar + name);
                convertDoc(f, o);
            }            
        }        
    }
    
    public void convertDoc(File input, File output) {
        try {
            System.out.print(
                    "conversion of \"" + input.getAbsolutePath() + "\"... ");
            
            byte[] fileStream = getByteArray( input );
            Holder<byte[]> mimeAttachment = new Holder<byte[]>();
            mimeAttachment.value = fileStream;
            
            // Important: If possible, supply original source file extension
            // with conversion request. Otherwiese, some input file formats 
            // will not be recognized properly and conversion will fail.
            String options = createFileExtensionOption(input);
            if (options != null)
                params.setOptionString(options);

            ConvertMimeResponse response 
                    = converter.convertMime(params, mimeAttachment);

            InputStream in = new ByteArrayInputStream(mimeAttachment.value);
            FileOutputStream fout = new FileOutputStream(output);
            
            int n = 0;
            byte[] buf = new byte[4096];
            while ((n = in.read(buf))!=-1)
                fout.write(buf, 0, n);
            fout.close();
            
            String status = response.getStatus();
            System.out.println("... done: status: " + status);
            
        } catch (Throwable e) {
            System.out.println("caught exception: " + e.getClass().getName());
            e.printStackTrace();
        }
    }
    
    private byte[] getByteArray(File file)
            throws Exception {
        ByteArrayOutputStream byteArrayOutputStream
                = new ByteArrayOutputStream();
        FileInputStream fin = new FileInputStream(file);
        byte[] buffer = new byte[16384];
        for (int len = fin.read(buffer); len > 0; len = fin.read(buffer)) {
            byteArrayOutputStream.write(buffer, 0, len);
        }
        fin.close();
        
        return byteArrayOutputStream.toByteArray();        
    }
    
    private static String createFileExtensionOption(File srcFile) {

        String ext = srcFile.getName();
        int idx = ext.lastIndexOf('.');
        if (idx >= 0) {
            ext = ext.substring(idx+1);

            if (!ext.equals("")) {
                String options = "settings.sourceFileExtension=";
                try {
                    options += URLEncoder.encode(ext, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    options += ext;
                }
                return options;
            }
        }
        return null;
    }
}