Skip to content

Multiple Leading Values Not Applied When Converting DOCX to PDF #700

@dcaruso-RatedPower

Description

@dcaruso-RatedPower

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).

Image

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:

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:

  1. Remove the adjustLeading() call that's overriding the multiplied leading, or
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions