0

I want to change the starting position of the first page of the table I created

for the first page created

let pageRect = CGRect(x: 0, y: 150, width: pageWidth, height: pageHeight)

for other created pages

let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)

i want to use how do i do this

for detailed code : Swift 5 - How to create table in PDF with PDFKit

class PDFCreator: NSObject {
let defaultOffset: CGFloat = 20
let tableDataHeaderTitles: [String]
let tableDataItems: [TableDataItem]

init(tableDataItems: [TableDataItem], tableDataHeaderTitles: [String]) {
    self.tableDataItems = tableDataItems
    self.tableDataHeaderTitles = tableDataHeaderTitles
    
}

func create() -> Data {
    
    let pageWidth = 8.5 * 72.0
    let pageHeight = 11 * 72.0
    let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
    let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: UIGraphicsPDFRendererFormat())
    let numberOfElementsPerPage = calculateNumberOfElementsPerPage(with: pageRect)
    let tableDataChunked: [[TableDataItem]] = tableDataItems.chunkedElements(into: numberOfElementsPerPage)
    let data = renderer.pdfData { context in
        for tableDataChunk in tableDataChunked {
            context.beginPage()
            
            if pdfHeaderStatus==false {

                let baslikTasarım = [
                    NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17),
                    NSAttributedString.Key.foregroundColor: UIColor(red: 32.0/255.0, green: 161.0/255.0, blue: 218.0/255.0, alpha: 1.0)
                ]
                
                let altBaslık = [
                    NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 11),
                    NSAttributedString.Key.foregroundColor: UIColor.black
                ]

                let unvanTarih = [
                    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 11),
                    NSAttributedString.Key.foregroundColor: UIColor.black
                ]
                
                let image = UIImage(named: "forgotpassword")
                let che = "CARİ   HESAP   EKSTRESİ"
                let text = "TARİH: "
                let alici = "ALICI: "
                let tarih = "\(bastarihTitle) - \(bittarihTitle)"
                let unvans = "\(unvanTitle)"
                che.draw(at: CGPoint(x: 30, y: -120), withAttributes: baslikTasarım)
                alici.draw(at: CGPoint(x: 30, y: -70), withAttributes: altBaslık)
                text.draw(at: CGPoint(x: 30, y: -20), withAttributes: altBaslık)
                image?.draw(at: CGPoint(x: 300, y: -120))
                tarih.draw(at: CGPoint(x: 110, y: -20), withAttributes: unvanTarih)
                unvans.draw(at: CGPoint(x: 110, y: -70), withAttributes: unvanTarih)
                
                let cgContext = context.cgContext
                print("cgContext", cgContext)
                drawTableHeaderRect(drawContext: cgContext, pageRect: pageRect)
                drawTableHeaderTitles(titles: tableDataHeaderTitles, drawContext: cgContext, pageRect: pageRect)
                drawTableContentInnerBordersAndText(drawContext: cgContext, pageRect: pageRect, tableDataItems: tableDataChunk)

                pdfHeaderStatus=true
                
            }
                    let cgContext = context.cgContext
                    drawTableHeaderRect(drawContext: cgContext, pageRect: pageRect)
                    drawTableHeaderTitles(titles: tableDataHeaderTitles, drawContext: cgContext, pageRect: pageRect)
                    drawTableContentInnerBordersAndText(drawContext: cgContext, pageRect: pageRect, tableDataItems: tableDataChunk)
            
        
        }
    }
    
    return data
}


func calculateNumberOfElementsPerPage(with pageRect: CGRect) -> Int {
    print("defaultOffset",defaultOffset)
    let rowHeight = (defaultOffset * 3)
    let number = Int((pageRect.height - rowHeight) / rowHeight)
    print("number",number)
    return number
}}

The header and table of the code I wrote works fine. but i can't change xy locations it won't let me MyPDFTable

MFEkmen
  • 1
  • 3
  • That's quite a bit of code for us to parse through. Is that code working and you need more or is that code not working? If it's not working did you do some basic troubleshooting by stepping through the code line by line, inspecting the vars and code execution along the way, until you spot something that isn't as expected? – Jay Nov 29 '21 at 18:34
  • the code is working properly. I put the link of all the code above and got a reference from there. but what I want is to add a table under the caption in the photo (for example, 5 rows of data will be added). I don't know how to give more detailed information. – MFEkmen Nov 30 '21 at 07:09
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Nov 30 '21 at 10:25
  • I edited the code to be more understandable. you can review – MFEkmen Nov 30 '21 at 10:47
  • I am still not sure the question is clear. You state you want to add a *table* to your PDF? Where's the code you're attempted to do that? Is that the code within the `for tableDataChunk` loop? Is the table you want to add a graphic image or actual table with rows and columns? I would suggest taking a look at the actual [tutorial](https://www.raywenderlich.com/4023941-creating-a-pdf-in-swift-with-pdfkit#toc-anchor-007) in the NSParagraphStyle section as that add's text you can format into "rows" if you like. – Jay Nov 30 '21 at 18:25
  • I can print a table to the screen. I just want the position of the table printed on the first page to be different compared to the other pages. The first code I wrote above is valid for page 1, code 2 is valid for other pages, but I don't know how to do it. my problem is not to create a table but to start the pdftable from the position I want. so in short, changing the 3rd line in the code according to the situation – MFEkmen Dec 01 '21 at 06:20
  • you want to start page from y 150 ? why ? – Zeeshan Ahmad II Dec 08 '21 at 09:40
  • you don't want that header ? to include your pages ? you want header on all pages rather than first ? – Zeeshan Ahmad II Dec 08 '21 at 09:41
  • Since there will be a title on the first page, the table should start a little lower. The table must start from x:0 y:0 as the title will not be displayed on other pages. – MFEkmen Dec 08 '21 at 10:45
  • I'd love to know if there's an easier or correct way to do what I want. but for now i am using this – MFEkmen Dec 08 '21 at 10:47

0 Answers0