diff --git a/src/EPPlus.Export.Pdf.Tests/PdfTests.cs b/src/EPPlus.Export.Pdf.Tests/PdfTests.cs index 71424c9d4..16c919d0a 100644 --- a/src/EPPlus.Export.Pdf.Tests/PdfTests.cs +++ b/src/EPPlus.Export.Pdf.Tests/PdfTests.cs @@ -13,17 +13,8 @@ Date Author Change using EPPlus.Export.Pdf.Settings; using OfficeOpenXml; using OfficeOpenXml.Export.PdfExport; -using System.Diagnostics; using System.Text; -using OfficeOpenXml.Export.PdfExport; -using EPPlus.Export.Pdf.Settings.PdfPageSizes; -using EPPlus.Export.Pdf; -using EPPlus.Fonts.OpenType; -using EPPlus.Fonts.OpenType.Integration; -using OfficeOpenXml; -using OfficeOpenXml.Interfaces.RichText; using OfficeOpenXml.Style; -using System.Diagnostics; namespace EPPlusTest.PDF { diff --git a/src/EPPlus.Export.Pdf/DocumentObjects/PdfContentStream.cs b/src/EPPlus.Export.Pdf/DocumentObjects/PdfContentStream.cs index 7039d470a..f6e6870b6 100644 --- a/src/EPPlus.Export.Pdf/DocumentObjects/PdfContentStream.cs +++ b/src/EPPlus.Export.Pdf/DocumentObjects/PdfContentStream.cs @@ -47,6 +47,7 @@ public void AddCommand(string command) public void AddCellLayout(PdfCellLayout cell, string label) { + if (cell.Size.X <= 0d || cell.Size.Y <= 0d) return; if (cell.CellFillData.GradientFillData != null && cell.CellFillData.PatternStyle != ExcelFillStyle.Solid) { commands.Add($"% Pattern Start: {cell.Name}"); diff --git a/src/EPPlus.Export.Pdf/Layout/PdfCellLayout.cs b/src/EPPlus.Export.Pdf/Layout/PdfCellLayout.cs index dd037480c..153a9c621 100644 --- a/src/EPPlus.Export.Pdf/Layout/PdfCellLayout.cs +++ b/src/EPPlus.Export.Pdf/Layout/PdfCellLayout.cs @@ -53,6 +53,10 @@ internal void SetPattern(PdfDictionaries dictionaries, ExcelFillStyle patternSty } internal void SetGradient(PdfDictionaries dictionaries, ExcelFillGradientType gradientType, Color color1, Color color2, Color color3, double degree, double top, double bottom, double left, double right) { + if (Size.X <= 0d || Size.Y <= 0d) + { + return; + } CellFillData.GradientFillData = new PdfCellGradientFillData(); CellFillData.GradientFillData.GradientType = gradientType; CellFillData.GradientFillData.Color1 = color1; diff --git a/src/EPPlus/Export/PdfExport/Layout/PdfLayout.cs b/src/EPPlus/Export/PdfExport/Layout/PdfLayout.cs index b5b9b3f71..6aae8aebb 100644 --- a/src/EPPlus/Export/PdfExport/Layout/PdfLayout.cs +++ b/src/EPPlus/Export/PdfExport/Layout/PdfLayout.cs @@ -701,7 +701,6 @@ private static Page PrecomputePageMergedCells(PdfPageSettings pageSettings, PdfR string key = cell.MergedAddress.Address; if (page.MergedCells.ContainsKey(key)) continue; var addr = cell.MergedAddress; - var mainCell = cell.Main ?? cell; // Main == null means this cell IS the top-left // --- X --- // Start from the current column and walk left to the merge origin. // Columns within the current page come from colX; columns that lie on @@ -727,12 +726,33 @@ private static Page PrecomputePageMergedCells(PdfPageSettings pageSettings, PdfR if (rangeIdx >= 0 && rangeIdx < range.RowHeights.Count) drawY += range.RowHeights[rangeIdx].Height; } + // --- Width / Height --- + // Size the merge from the SAME arrays that produced X/Y above + // (range.ColWidths / range.RowHeights) rather than from mainCell. + // Those arrays already store 0 for hidden rows/columns and use the + // same unit conversion, default-height and auto-fit values as the + // rest of the grid, so the merge rectangle can never disagree with + // the surrounding cells (which is why columns worked but rows did not). + double mergeWidth = 0d; + for (int c = addr._fromCol; c <= addr._toCol; c++) + { + int rangeIdx = c - range.Range._fromCol; + if (rangeIdx >= 0 && rangeIdx < range.ColWidths.Count) + mergeWidth += range.ColWidths[rangeIdx]; + } + double mergeHeight = 0d; + for (int r = addr._fromRow; r <= addr._toRow; r++) + { + int rangeIdx = r - range.Range._fromRow; + if (rangeIdx >= 0 && rangeIdx < range.RowHeights.Count) + mergeHeight += range.RowHeights[rangeIdx].Height; + } page.MergedCells[key] = new MergedCellDrawInfo { X = drawX, Y = drawY, - Width = mainCell.Width, - Height = mainCell.Height + Width = mergeWidth, + Height = mergeHeight }; } } diff --git a/src/EPPlus/Export/PdfExport/TextMapping/PdfTextMap.cs b/src/EPPlus/Export/PdfExport/TextMapping/PdfTextMap.cs index e09939f34..c90f64af7 100644 --- a/src/EPPlus/Export/PdfExport/TextMapping/PdfTextMap.cs +++ b/src/EPPlus/Export/PdfExport/TextMapping/PdfTextMap.cs @@ -121,10 +121,12 @@ private static void HandleMergedCell(PdfPageSettings pageSettings, PdfDictionari double totalWidth = 0, totalHeight = 0; for (int k = address._fromRow; k <= address._toRow; k++) { + if (worksheet.Row(k).Hidden) continue; totalHeight += UnitConversion.ExcelRowHeightToPoints(worksheet.Row(k).Height); } for (int l = address._fromCol; l <= address._toCol; l++) { + if (worksheet.Column(l).Hidden) continue; totalWidth += UnitConversion.ExcelColumnWidthToPoints(worksheet.Column(l).Width, ZeroCharWidth); } checkedMergedCells.Add(mergeAddress);