Java Android Generate a document from a template .docx

0

I am trying to generate a document thanks to the plugin:
xdocreport-1.0.4.jar

Let's say that it has worked for me from the PC, following a template to generate another one with the data entered in the form but from the android phone I can not make it work, I imagine that the problem is in the GeneradorDocumentosService by the TOURL ...

I have two classes:
 - Main.java
 - GeneradorDocumentosService.java

My project:

My document Test.docx within Assets with its variables with MergeField

My problem when I gave it to generate the file:

In the class Main.java I have this, full code Main.Java

public static void generateDocument(String rutaPlantilla, String extension, 
boolean convertirPdf) 
throws IOException, XDocReportException
{

Map<String, Object> variablesMap = new HashMap<String, Object>();

variablesMap.put("name", name);
variablesMap.put("surname", surname);
variablesMap.put("date", date);

// 2) Create fields metadata to manage lazy loop (#forech velocity)
// for table row.
FieldsMetadata metadata = new FieldsMetadata();
metadata.addFieldAsList("listaNumeros.Numero");
metadata.addFieldAsList("listaNumeros.Cuadrado");
metadata.addFieldAsList("listaNumeros.Raiz");

// Mapa con las variables de tipo imagen. Estas variables contienen el path 
de la imagen
Map<String, String> imagenesMap = new HashMap<String, String>();
imagenesMap.put("header_image_logo", "./Logo.png");

GeneradorDocumentosService generadorDocumentosService = new 
GeneradorDocumentosService();
byte[] mergedOutput = 
generadorDocumentosService.generarDocumento(rutaPlantilla,
        TemplateEngineKind.Freemarker, variablesMap, imagenesMap, 
convertirPdf, metadata
);

FileOutputStream os = new FileOutputStream("Faycan_Contract_"+"." 
+extension);
os.write(mergedOutput);
os.flush();
os.close();
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtNombre = (EditText) findViewById(R.id.txtNombre);
txtApellidos = (EditText) findViewById(R.id.txtApellidos);
txtFecha = (EditText) findViewById(R.id.txtFecha);

name = txtNombre.getText().toString();
surname = txtApellidos.getText().toString();
date = txtFecha.getText().toString();

btnGenerar = (Button) findViewById(R.id.btnGenerar);
btnGenerar.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
  try
  {
  generateDocument("file:///android_asset/test.docx", "docx", false);
  } catch (Exception e)
  {
   Toast.makeText(getApplicationContext(), "The document could not be 
   generated"+e, Toast.LENGTH_SHORT).show();
  }
    }
});
}

In the class GeneratorDocumentsService.java I have this that could be the error, full code GeneradorDocumentosService.Java

public static InputStream loadDocumentAsStream(String filePath) throws 
IOException
{ 
 URL url = new File(filePath).toURI().toURL();
 InputStream documentAsStream = url.openStream();
 return documentAsStream;
}
    
asked by DomingoMG 13.02.2018 в 20:08
source

1 answer

0

My solution to this problem is using the plugin: JWord and indicating the template from the "sdcard" card with "Environment.getExternalStorageDirectory.GetPath () +" / Template.docx ", and the same for the output of the file. ..

I'll do the check to put it in the "Assets" folder and call it by "file: ///android_asset/template.docx" so the template will not be loose on the phone ...

    
answered by 20.02.2018 / 23:54
source