diff --git a/Source/Internal/Table/PDFTableObject.swift b/Source/Internal/Table/PDFTableObject.swift index 7a631307..9450c88c 100644 --- a/Source/Internal/Table/PDFTableObject.swift +++ b/Source/Internal/Table/PDFTableObject.swift @@ -284,6 +284,7 @@ internal class PDFTableObject: PDFRenderObject { let startPosition: CGPoint = cells.first?.frames.cell.origin ?? .zero var nextPageCells: [PDFTableCalculatedCell] = cells var pageEnd = CGPoint.null + var headerShift = true repeat { var pageStart = CGPoint.null @@ -296,7 +297,9 @@ internal class PDFTableObject: PDFRenderObject { var cellFrame = item.frames.cell var contentFrame = item.frames.content cellFrame.origin.y -= startPosition.y - minOffset + cellFrame.origin.y += table.margin contentFrame.origin.y -= startPosition.y - minOffset + contentFrame.origin.y += table.margin pageStart = pageStart == .null ? cellFrame.origin : pageStart pageEnd = CGPoint(x: cellFrame.maxX, y: cellFrame.maxY) + CGPoint(x: table.margin, y: table.margin) @@ -329,6 +332,15 @@ internal class PDFTableObject: PDFRenderObject { } minOffset += headerHeight } + if !firstPage { + // shift the rest of the cells down by headerHeight + if headerShift { + nextPageCells = shiftCellsBy(cells: nextPageCells, shiftValue: headerHeight) + headerShift = false + } + //add table padding around cells + nextPageCells = shiftCellsBy(cells: nextPageCells, shiftValue: table.padding) + } let filterResult = filterCellsOnPage(for: generator, items: nextPageCells, @@ -432,7 +444,6 @@ internal class PDFTableObject: PDFRenderObject { if shouldSplitCellsOnPageBreak && cellFrame.minY < maxOffset { result.cells.append(item) } - // In any case, if the cell does not fit on the active page entirely, it must be repositioned for further pages var nextPageCell = item if shouldSplitCellsOnPageBreak { nextPageCell.frames.cell.origin.y -= contentHeight @@ -450,7 +461,22 @@ internal class PDFTableObject: PDFRenderObject { } return result } - + + internal typealias ShiftedCells = ([PDFTableCalculatedCell]) + + internal func shiftCellsBy(cells: [PDFTableCalculatedCell], shiftValue: CGFloat) -> ShiftedCells { + var shiftedCells: [PDFTableCalculatedCell] = [] + + for cell in cells { + var shiftedCell = cell + + shiftedCell.frames.cell.origin.y += shiftValue + shiftedCell.frames.content.origin.y += shiftValue + shiftedCells.append(shiftedCell) + } + return shiftedCells + } + internal func createSliceObject(frame: CGRect, elements: [PDFRenderObject], minOffset: CGFloat, maxOffset: CGFloat) -> PDFSlicedObject { let sliceObject = PDFSlicedObject(children: elements, frame: frame) if frame.maxY > maxOffset { diff --git a/Source/Internal/Utils/PDFCalculations.swift b/Source/Internal/Utils/PDFCalculations.swift index aaff5eed..9b148307 100644 --- a/Source/Internal/Utils/PDFCalculations.swift +++ b/Source/Internal/Utils/PDFCalculations.swift @@ -144,8 +144,10 @@ internal enum PDFCalculations { return pageLayout.height - layout.margin.top - layout.heights.maxHeaderHeight() + - pageLayout.space.header - layout.heights.content - generator.currentPadding.bottom + - pageLayout.space.footer - layout.heights.maxFooterHeight() - layout.margin.bottom } @@ -153,8 +155,10 @@ internal enum PDFCalculations { internal static func calculateTopMinimum(for generator: PDFGenerator) -> CGFloat { let layout = generator.layout + let pageLayout = generator.document.layout return layout.margin.top + layout.heights.maxHeaderHeight() + + pageLayout.space.header } /// Calculates the maximum offset from the top edge when the main content should break to the next page