Java samples guide

Before you start

For the samples to work correctly, make sure that two conditions are met:

  1. ONLYOFFICE Document Builder is installed into default directory C:/Program Files/ONLYOFFICE/DocumentBuilder on your computer.
  2. The directory in which you are going to store the downloaded samples has general editing access to save files created by the Document Builder.

Download samples

Clone a repository with the Document Builder samples from https://github.com/ONLYOFFICE/document-builder-samples. The created folder must have general editing access.

The project folder includes the java folder with the Java samples. Each sample has its own folder with the Program.java program file.

Program structure

To create the Java sample:

  1. Include the Java wrapper doctrenderer library:
import docbuilder.*;
  1. Specify the result path (where the generated file will be saved):
String resultPath = "result.docx";
  1. Create and call the builder function (this function is created by user and calls the DocBuilder method to work with documents):
public static void createBasicForm(String resultPath) {}
  • Initialize the DocBuilder with docbuilder.jar directory by passing an empty string:

    // Initialize builder with docbuilder.jar directory by passing an empty string
    CDocBuilder.initialize("");
    CDocBuilder builder = new CDocBuilder();
    
  • Open or create a file so that Context and Global classes can be accessed:

    // Create file and get context
    builder.createFile(doctype);
    CDocBuilderContext context = builder.getContext();
    CDocBuilderValue global = context.getGlobal();
    CDocBuilderValue api = global.get("Api");
    
  • Edit file using the Document Builder API methods. Use the call method with the name and parameters of the API method you call as arguments:

    // Create basic form
    CDocBuilderValue document = api.call("GetDocument");
    CDocBuilderValue paragraph = document.call("GetElement", 0);
    CDocBuilderValue headingStyle = document.call("GetStyle", "Heading 3");
    
    paragraph.call("AddText", "Employee pass card");
    paragraph.call("SetStyle", headingStyle);
    document.call("Push", paragraph);
    
    CDocBuilderValue pictureForm = api.call("CreatePictureForm");
    setPictureFormProperties(pictureForm, "Photo", "Upload your photo", false, "Photo", "tooBig", true, false, 50, 50);
    paragraph = api.call("CreateParagraph");
    paragraph.call("AddElement", pictureForm);
    document.call("Push", paragraph);
    
    CDocBuilderValue textForm = api.call("CreateTextForm");
    setTextFormProperties(textForm, "First name", "Enter your first name", false, "First name", true, 13, 3, false, false);
    paragraph = api.call("CreateParagraph");
    paragraph.call("AddElement", textForm);
    document.call("Push", paragraph);
    
  • Save and close file after editing, then call the dispose method to destroy DocBuilder:

    // Save file and close DocBuilder
    builder.saveFile(doctype, resultPath);
    builder.closeFile();
    CDocBuilder.dispose();
    

Run the sample

JDK 8 or newer is required.

To run the Java code samples:

  1. Go to the test directory:

    cd ../document-builder-samples/java/creating_presentation
    
  2. Compile the Program.java providing the path to docbuilder.jar located in the Document Builder directory:

    javac -cp "C:\Program Files\ONLYOFFICE\DocumentBuilder\docbuilder.jar" Program.java
    
  3. The .class file should appear in the directory. Run the program:

    java -cp "C:\Program Files\ONLYOFFICE\DocumentBuilder\docbuilder.jar;." Program
    

    Note, that on UNIX systems the path separator is : instead of ;. Thus, on Linux or macOS it should be:

    java -cp "/opt/onlyoffice/documentbuilder/docbuilder.jar:." Program
    

    Documents will be created in the test directory.

Result file

Get Help

  • If you have any questions about ONLYOFFICE Docs, try the FAQ section first.
  • You can request a feature or report a bug by posting an issue on GitHub.
  • You can also ask our developers on ONLYOFFICE forum (registration required).