Inserting Blank page after adding image on previous page in pdf using PDFKit Swift

653 views Asked by At

I am facing the problem while inserting a blank page in pdf using PDFKit. when there are images on pdf importing from gallery or capturing from the camera. and then we add a blank page on that pdf at that time the page is very small. it's not in A4 size. here I would post the code. and I am also going to post the screenshot of what problem actually I am getting please refer to it and provide a possible solution.in the screenshot, I am adding 2 blank pages enter image description here

    let format = UIGraphicsPDFRendererFormat()
    
    
    // 2
    let pageWidth = 8.5 * 72.0
    let pageHeight = 11 * 72.0
    let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
    
    // 3
    let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)
    // 4
    
    var firstPdfDocument = PDFDocument()
    var SecondpdfDocument = self.pdfView.document
    let data = renderer.pdfData { (context) in
        // 5
        context.beginPage()
        // 6
        
        let context = context.cgContext
        
        //            let page = PDFPage()
        //            page.setBounds(pageRect, for: .mediaBox)
        // pdfDocument!.insert(page, at: self.pdfView.document!.pageCount)
    }
    
    print(data)
    firstPdfDocument = PDFDocument(data: data)!
    
    let page = firstPdfDocument.page(at: 0)!
    
    SecondpdfDocument!.insert(page, at: self.pdfView.document!.pageCount)
    
    SecondpdfDocument!.write(toFile: self.pdfFile.url.path)
0

There are 0 answers