How to replace binary content & size attributes from FileDocument IMendixObject in a java action? (by using an outputStream)

0
Hi I try to make a java action where i convert a docx file to a pdf. But i don’t really understand how to set the “FileDocument” IMendixObject. There is a static getter Core.getFileDocumentContent(context,inputFile); but there is no setter? Core.getFileDocumentContent(context,inputFile) There is a ImendixObject.setValue() but i don’t understand how to replace the binary content & size. My code which i’m sending the action : import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IMendixObject; import com.mendix.systemwideinterfaces.core.IContext; import org.apache.poi.xwpf.usermodel.XWPFDocument; import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter; import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; import java.io.InputStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DocxToPDFConverter { public IMendixObject convert(IMendixObject inputFile, IContext context) throws Exception { InputStream inputStream = null; OutputStream outputStream = null; XWPFDocument doc = null; IMendixObject outputFile = null; try { inputStream = Core.getFileDocumentContent(context, inputFile); doc = new XWPFDocument(inputStream); PdfOptions pdfOptions = PdfOptions.create(); outputStream = new FileOutputStream(inputFile.getMember(context, "Name") .getValue(context).toString().replaceFirst("docx","pdf")); PdfConverter.getInstance().convert(doc, outputStream, pdfOptions); System.out.println("Done"); } catch(IOException e) { e.printStackTrace(); } finally { if(inputStream != null) { inputStream.close(); } if(doc != null) doc.close(); if(outputStream != null) { outputFile = inputFile; outputFile.setValue(context, "Contents", outputStream); outputStream.close(); } } return outputFile; } } My action   public class DocxToPDF extends CustomJavaAction<IMendixObject> { private IMendixObject __inputFile; private system.proxies.FileDocument inputFile; public DocxToPDF(IContext context, IMendixObject inputFile) { super(context); this.__inputFile = inputFile; } @java.lang.Override public IMendixObject executeAction() throws Exception { this.inputFile = __inputFile == null ? null : system.proxies.FileDocument.initialize(getContext(), __inputFile); // BEGIN USER CODE IContext context = this.context(); if (inputFile != null && inputFile.getHasContents(context)) { DocxToPDFConverter docxToPDFConverter = new DocxToPDFConverter(); return docxToPDFConverter.convert(__inputFile, context); } else { throw new NullPointerException("Empty input file in DocxToPDF Formatter."); } // END USER CODE } /** * Returns a string representation of this action */ @java.lang.Override public java.lang.String toString() { return "DocxToPDF"; } // BEGIN EXTRA CODE // END EXTRA CODE }  
asked
1 answers
1

Hi Breg , 

 

Please find the sample case below ,

Use the core.storefilestoredocument 

Initialize output file like below,

    customFileDocument OutputFile = new customFileDocument(Context);
    SimpleDateFormat formatter = new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss");  
    Date date = new Date();  
    OutputFile.setName("Sample_"+formatter.format(date)+".pdf");
    Core.getLogger("ExportToPDF").info("File Name : " + OutputFile.getName());

 

for storing the content use core.storefilestoredocument and return output file as output

try (ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray())) {
                    Core.storeFileDocumentContent(Context, OutputFile.getMendixObject(), inputStream);
                }

     return OutputFile;
    }
}

 

Hope this will helps you

answered