I’m doing an Android Kotlin project which will auto-generate certificates when we enter some details in the EditTexts. I have word files (.docx) in my assets folder which has some variables which will be replaced by the data entered in the EditTexts. I’m getting proper output word file, and it downloads properly. Now I need to convert that converted word to pdf in-app itself, and it must be downloaded in my phone instead of the word file.
I used these libraries for reading & editing word file –
implementation 'org.apache.poi:poi:5.2.4'
implementation 'org.apache.poi:poi-ooxml:5.2.4'
This the function that edits the original word file and downloads the new word file :
private fun editWordFile(context: Context, name: String, date: String assetsName: String) {
// assetsName = "joinai.docx" This is written somewhere on the top where the function is called
try {
val assetManager = applicationContext.assets
val wordFileName = assetsName.toString()
val inputStream: InputStream = assetManager.open(wordFileName)
val outputDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val outputName = "${name}_${date}"
try {
// Read Word file
val document = XWPFDocument(inputStream)
// Replace placeholders/variables with EditText data
for (paragraph: XWPFParagraph in document.paragraphs) {
for (run: XWPFRun in paragraph.runs) {
val text = run.text()
println("Text: $text, Date: $date")
if (text.contains("XDATEX")) {
run.setText(text.replace("XDATEX", date), 0)
}
if (text.contains("XLAMAX")) {
run.setText(text.replace("XLAMAX", name), 0)
}
}
}
// Save modified Word file
val outputFile = File(outputDir, "${outputName}.docx")
val fos = FileOutputStream(outputFile)
document.write(fos)
fos.close()
Toast.makeText(this, "File $outputName downloaded.", Toast.LENGTH_SHORT).show()
// Convert Word to PDF
// You may implement PDF conversion logic here
} catch (e: Exception) {
e.printStackTrace()
} finally {
inputStream.close()
}
}
catch (e: Exception) {
e.printStackTrace()
Toast.makeText(context, "Error occurred: ${e.message}! Contact developer", Toast.LENGTH_SHORT).show()
}
}
I need to convert this resulting .docx to .pdf, which has good quality, and is most probably free of cost (if you are recommending any service APIs).
2
Answers
Comment to @Zeros-N-Ones As you told , extra processing need to be done for images, I have background image for this word file, which isn't visible in pdf file, and also the formatting is getting wrecked. What to do next? I've give below my updated code and sample ss images of output word and pdf files.
I think the best approach to this is using Apache POI with iText, which is an open-source pdf library. See modified code below:
The above solution uses iText library for pdf conversion; creates a temporary Word file in the cache directory; converts the Word content to pdf while maintaining text formatting; automatically deletes the temporary Word file; and saves only the final pdf to the Downloads folder.
To implement this:
Add the iText dependency to your build.gradle:
Replace your existing
editWordFile
function with the neweditWordFileAndConvertToPDF
function from the artifact.Note that this basic implementation handles text and tables. If you need more advanced formatting (like images, complex layouts, or specific fonts), you might need to add additional conversion logic in the
convertWordToPDF
function.Some limitations to be aware of: