Created Date: Feb 10, 2021 15:15
Last Modifed Date: Feb 18, 2021 10:38


Getting started

Mako 6.0 adds language support for Java. As with C# and Python, it is made possible by the use of SWIG, a technology that connects the C++ of Mako with other languages.

Begin by extracting the Java libraries from the Mako distribution. Look for the Windows SWIG (C#, Java, Python) folder which contains a ZIP. Unzip this to retrieve the Java libraries from mako-SWIG\swig\java\release-win-x64-vc16\bindings.

Visual Studio Code

First:

  • Install a Java development kit (JDK) if you haven't already done so. You can download this from Oracle, although there are other JDKs available.
  • Install the Java extension in Visual Studio Code (VSCode). Get it from this Microsoft website (or just search for "VSCode Java" in the search engine of your choice).

Copy the two files to a new folder. It can be located anywhere, but it will become your VSCode workspace, so you may wish to locate it in an appropriate place for use with your version control system.

Next, in VSCode, follow these steps:

Open a folder. You can also use the Create Java Project, but for simplicity we'll use the Open Folder option.The sidebar after opening the folder, which in this case is called GettingStartedNow choose File>New File, and type the code you see here. You can copy and paste the code from the VSCode Java Getting Started guide – from View>Command Palette, choose Java: Getting Started to access it.

Press F5 to make sure it runs. The output window at the bottom of the VSCode windows should display "Hello World".

The next step is to import the Mako library.

  • Add import com.globalgraphics.JawsMako.jawsmakoIF.*; before the first line in your source

This will be underlined with an error squiggle indicating the reference cannot be resolved. To fix this, identify the .jar location to VSCode:

Open JAVA PROJECTS. Click the + next to Reference Libraries. Browse for jawsmakoIF.jar in your project folder.

Press F5 to make sure the project builds correctly.

Adding some Java code

You are now ready to add some code. Here is some Java code you can paste in, to replace what is there.

import com.globalgraphics.JawsMako.jawsmakoIF.*;

class QuickStart {
    public static void main(String[] args) {

        var jawsMako = IJawsMako.create();
        var factory = jawsMako.getFactory();
        IJawsMako.enableAllFeatures(jawsMako);
        var assembly = IDocumentAssembly.create(jawsMako);
        var document = IDocument.create(jawsMako);
        assembly.appendDocument(document);
        var page = IPage.create(jawsMako);
        document.appendPage(page);

        // Create a fixed page A5 landscape.
        double pageWidth = 793.7;
        double pageHeight = 561.1;
        var fixedPage = IDOMFixedPage.create(factory, pageWidth, pageHeight);

        // Now create some DOM for the page contents.

        // Find a font
        long[] fontIndex = {0};
        IDOMFont font = jawsMako.findFont("Arial Bold", fontIndex);
        
        // Create a blue brush 
        var colorSpace = IDOMColorSpacesRGB.create (factory);
        var colorValues = new StdVectFloat();
        colorValues.add(0.0f);
        colorValues.add(0.0f);
        colorValues.add(1.0f);

        var blueColor = IDOMColor.createFromVect(factory, colorSpace, 1.0f, colorValues);
        var solidBrush = IDOMSolidColorBrush.create(factory, blueColor);
        
        // Create glyphs
        String message = "Mako Java API";
        var glyphs = IDOMGlyphs.create(factory, message, 72, new FPoint(10, pageHeight / 2), solidBrush, font, fontIndex[0]);

        // And add to the page
        fixedPage.appendChild(glyphs.toIDOMNode());

        // Assign content to a page
        page.setContent(fixedPage);

        // Now we can write this to a PDF
        IPDFOutput.create(jawsMako).writeAssembly(assembly, "TestJava.pdf", IProgressTick.Null ());

        System.out.println("File created.");
    }
}
JAVA

Run this code by pressing F5. You should see the message "File Created." in the output window. You will find the output in your project folder.