Generate a Word Document

Updated: May 25, 2022

For this example you are going to learn how to use DataPipeline's SimpleJsonReader to read JSON data and write the contents to a word document.

This example can easily be modified to show how to write to a JSON stream.

JSON input

[
{
"stageName":"John Wayne",
"realName":"Marion Robert Morrison",
"gender":"male",
"city":"Winterset","balance":156.35
},
{
"stageName":"Spiderman",
"realName":"Peter Parker",
"gender":"male",
"city":"New York",
"balance":-0.96
}
]

Java Code listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.json.SimpleJsonReader;
import com.northconcepts.datapipeline.rtf.RtfWriter;

public class GenerateAWordDoc {

    public static void main(String[] args) {
        DataReader reader = new SimpleJsonReader(
                new File("example/data/input/simple-json-input.json"));
        
        DataWriter writer = new RtfWriter(
                new File("example/data/output/simple-json-output.doc"));
        
        Job.run(reader, writer);
    }

}

Code walkthrough

  1. The SimpleJsonReader is created corresponding to the input file i.e. simple-json-input.json.
  2. A RtfWriter is also created corresponding to the output file i.e. simple-json-output.doc.
  3. RtfWriter extends BinaryWriter and writes records to an RTF document stream. It is used for generating documents in Rich Text Format.
  4. Data is transferred from the SimpleJsonReader to RtfWriter via Job.run() method.

Output

The output will be written to the .doc file and stored in the specified location i.e example/data/output.

Mobile Analytics