Skip to content

fix(table rendering): fix table cell background color and column width with percentage#733

Open
codefan wants to merge 1 commit into
opensagres:masterfrom
codefan:fix/table-shading-and-column-width
Open

fix(table rendering): fix table cell background color and column width with percentage#733
codefan wants to merge 1 commit into
opensagres:masterfrom
codefan:fix/table-shading-and-column-width

Conversation

@codefan

@codefan codefan commented Jun 12, 2026

Copy link
Copy Markdown

Summary

Two fixes for table rendering in DOCX to PDF conversion:

1. Fix table cell shading background color not rendered when w:fill="auto"

Problem: Table header cells with dark blue background (#1A237E) and white text lose their background color during conversion, making white text invisible on the white page.

Root cause: ColorHelper.getFillColor() only reads the w:fill attribute from <w:shd>. When the DOCX uses <w:shd w:val="solid" w:color="1A237E" w:fill="auto"/>, the w:fill="auto" is resolved as white, while the actual color 1A237E is in the w:color attribute.

Fix: When w:fill="auto", fall back to the w:color attribute for the actual fill color, per the OOXML specification.

2. Fix incorrect table column widths when cells use percentage-based widths

Problem: Table columns have incorrect proportions — some columns are too narrow, causing text to be clipped.

Root cause: XWPFTableUtil.computeColWidths() always uses absolute twips values from <w:gridCol> for column widths. When cells define percentage widths (<w:tcW w:type="pct">), the grid column twips may not reflect the intended proportions. For example, a grid column of 119 twips (~6pt) vs. a cell percentage of 18.24%.

Fix: Added computeColWidthsFromCells() that detects when cells use percentage widths and uses those proportions instead. The percentage values are scaled to match the grid total width to preserve correct overall table dimensions.

Test

  • Added TableHeaderColorTestCase with a Chinese DOCX document containing tables with colored headers.
  • All 144 existing tests continue to pass with no regressions.

🤖 Generated with Claude Code

…h with percentage

1. Fix table cell shading background color not rendered when w:fill='auto'
   - In ColorHelper.getFillColor(), when w:fill='auto' and w:val='solid',
     fall back to w:color attribute for the actual fill color per OOXML spec.
   - This fixes dark blue header backgrounds that were lost during conversion.

2. Fix incorrect table column widths when cells use percentage-based widths
   - In XWPFTableUtil.computeColWidths(), when cells define w:type='pct'
     widths, use those proportions instead of grid column twips values.
   - Grid column widths are always in absolute twips and may not reflect
     the intended percentage-based layout, causing some columns to be
     too narrow or too wide.
   - Scale cell percentage proportions to match grid total width to
     preserve correct overall table width.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant