Problem
When converting a DOCX document with multiple leading (line spacing) settings to PDF using fr.opensagres.xdocreport:fr.opensagres.poi.xwpf.converter.pdf.internal:PdfMapper, the multiple leading values are not correctly applied in the output PDF. This occurs because the current leading value overrides the multiplied leading value.
You can find attached a screenshot that highlights the issue. (DOCX on the left and PDF on the right).
Root Cause
In the PdfMapper class, within the visitRun method, there are two consecutive calls that override each other:
StylableParagraph pdfParagraph = (StylableParagraph) pdfParagraphContainer;
pdfParagraph.adjustMultipliedLeading(currentRunFontAscii);
pdfParagraph.adjustLeading(currentRunFontAscii);
This is the code:
|
pdfParagraph.adjustLeading(currentRunFontAscii); |
The problem is that adjustLeading() is called immediately after adjustMultipliedLeading(), which effectively cancels out the multiplied leading settings. The second call overwrites the value set by the first call, causing the multiple leading setting to be ignored.
Expected Behavior
Both single and multiple leading values should be correctly preserved when converting from DOCX to PDF, with multiple leading taking precedence when specified.
Test to reproduce the issue
This test reproduces the issue. You'll need to add a document named template-multiple-leading.docx with multiple leading settings to a test folder.
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileOutputStream;
class MultipleLeadingTest {
@Test
void testDemonstrateMultipleLeadingIssue() throws Exception {
// Get test directory path
String testDir = System.getProperty("user.dir") + "/test/";
// Create directory if it doesn't exist
new File(testDir).mkdirs();
String docxPath = testDir + "multiple-leading-" + System.currentTimeMillis() + ".docx";
String pdfPath = testDir + "leading-" + System.currentTimeMillis() + ".pdf";
// Create a document with a centered table
String templateFile = testDir + "template-multiple-leading.docx";
XWPFDocument document = new XWPFDocument(new java.io.FileInputStream(templateFile));
// Save DOCX
try (FileOutputStream out = new FileOutputStream(docxPath)) {
document.write(out);
}
// Convert to PDF
PdfOptions options = PdfOptions.create();
try (FileOutputStream out = new FileOutputStream(pdfPath)) {
PdfConverter.getInstance().convert(document, out, options);
}
// Log files location
System.out.println("Files saved to: " + testDir);
// Multiple leading settings in the document are not applied correctly
}
}
Proposed Solution
The solution would be to modify the visitRun method in PdfMapper to either:
- Remove the
adjustLeading() call that's overriding the multiplied leading, or
- Add conditional logic to only call
adjustLeading() when multiple leading is not specified
This would ensure that multiple leading settings are correctly preserved in the PDF output.
Problem
When converting a DOCX document with multiple leading (line spacing) settings to PDF using
fr.opensagres.xdocreport:fr.opensagres.poi.xwpf.converter.pdf.internal:PdfMapper, the multiple leading values are not correctly applied in the output PDF. This occurs because the current leading value overrides the multiplied leading value.You can find attached a screenshot that highlights the issue. (DOCX on the left and PDF on the right).
Root Cause
In the
PdfMapperclass, within thevisitRunmethod, there are two consecutive calls that override each other:This is the code:
xdocreport/thirdparties-extension/fr.opensagres.poi.xwpf.converter.pdf/src/main/java/fr/opensagres/poi/xwpf/converter/pdf/internal/PdfMapper.java
Line 553 in 83a1262
The problem is that
adjustLeading()is called immediately afteradjustMultipliedLeading(), which effectively cancels out the multiplied leading settings. The second call overwrites the value set by the first call, causing the multiple leading setting to be ignored.Expected Behavior
Both single and multiple leading values should be correctly preserved when converting from DOCX to PDF, with multiple leading taking precedence when specified.
Test to reproduce the issue
This test reproduces the issue. You'll need to add a document named template-multiple-leading.docx with multiple leading settings to a test folder.
Proposed Solution
The solution would be to modify the
visitRunmethod inPdfMapperto either:adjustLeading()call that's overriding the multiplied leading, oradjustLeading()when multiple leading is not specifiedThis would ensure that multiple leading settings are correctly preserved in the PDF output.