Create a template doc from an inputstream

Product: PDFNet

Product Version: 9.3.0

Please give a brief summary of your issue:
Create a template from a docx-template fetched from our database

We are investigating how to use PDFTron to use docx-templates and JSON data as input. This is straightforward documented her,

But we shall not read in the docx-template from the filesystem, but from our database. I get my template like this,
var template = templateService.getTemplate(TemplateType.VERKSTED_RAPPORT);
now I have an object holding my docx-template. I have a method that gives me the content as a byte array.

PDFTtron use this,
// Create a TemplateDocument object from an input office file.
TemplateDocument templateDoc = Convert.createOfficeTemplate(input, null);

input is either a string pointing to an file in the filesystem or a Filter. My problem is how I can use the Filter class properly as an input_stream filled with my template or if there is another approach?

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:Forums:

var template = templateService.getTemplate(TemplateType.VERKSTED_RAPPORT, "PLA");

FileOutputStream outputStream = new FileOutputStream("/tmp/template.docx");
outputStream.write(template.getFileContent());
TemplateDocument templateDoc = Convert.createOfficeTemplate("/tmp/template.docx", null);

The code over is a workaround, but not a good solution…

Hi Petter,

Please take a look at the following link:

Let me know if that answers your question.

1 Like

Thanks, that worked fine :slight_smile:

I used this code in Java:

//My template from the database
      var template = templateService.getTemplate(TemplateType.VERKSTED_RAPPORT, "PLA");

      MemoryFilter memoryFilter = new MemoryFilter(template.getFileContent().length, false); // false = sink
      // helper filter to allow us to write to buffer
      FilterWriter writer = new FilterWriter(memoryFilter);
      writer.writeBuffer(template.getFileContent());
      writer.flush();
      memoryFilter.setAsInputFilter();

      TemplateDocument templateDoc = Convert.createOfficeTemplate(memoryFilter, null);