I'm using Apache POI in Java to manipulate complex tables (with split cells) in a .docx template and converting it to PDF with LibreOffice 7.6 in headless mode on Linux with WSL. The challenge is to maintain the original table formatting (setting table layout and column widths as per my template) in the generated PDF. The table alignment distorts unless I manually set "Autofit to window" in Word before conversion.
I've learned from other StackOverflow answers that setting the layout to fixed and adjusting individual column lengths might be the solution. However, my table's complex structure prevents hardcoding these lengths. Here's how I'm currently setting the table layout in my code with Apache POI:
private static void performTableOperations(XWPFTable table){
CTTblLayoutType type = table.getCTTbl().getTblPr().addNewTblLayout();
type.setType(STTblLayoutType.FIXED);
//...other operations
}
and the command that I use to convert .docx to PDF using LibreOffice is:
libreoffice --headless --convert-to pdf:writer_pdf_Export --outdir /home/username /home/username/yourfile.docx
I'm looking for a way to programmatically ensure proper table formatting (setting table layout and column widths as per my template) in the PDF, without manual adjustments. Insights on dynamically managing table and column widths or preserving complex table formatting during DOCX to PDF conversion would be greatly appreciated.