S.Basnagoda
S.Basnagoda

Reputation: 711

rotationAngle data is lost when creating a CGPDFPage

I have this PDF file with me. I use following code to render the first page of this PDF file.

let page = self.pdfDocument.page(at: 0 )! // get first page
let rotationAngle = page.rotation // this value is 90
let renderedPdfData = renderPDF(page: page.pageRef!)

where renderPDF method is,

func renderPDF(page: CGPDFPage) -> Data {
    let pageRect = page.getBoxRect(.mediaBox)
    let renderer = UIGraphicsPDFRenderer(bounds: pageRect)
    return renderer.pdfData { (context) in
        context.beginPage()
        context.cgContext.setFillColor(UIColor.white.cgColor)
        context.fill(pageRect)
        context.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
        context.cgContext.scaleBy(x: 1, y: -1)
        context.cgContext.drawPDFPage(page)
    }
}

Then later I want to create a CGPDFPage back with above renderedPdfData . For that I use following method.

var newPage: CGPDFPage = createPDFPage(pdfData: renderedPdfData)

where createPDFPage method is,

func createPDFPage(pdfData: Data) -> CGPDFPage? {
    guard let provider = CGDataProvider(data: pdfData as CFData) else {
      return nil
    }

    guard let document = CGPDFDocument(provider) else {
      return nil
    }

    // page starting at 1. The input PDF data only has 1 page.
    return document.page(at: 1)
  }

But how should I set newPage's rotation angle data (ie. 90)? Because 'rotationAngle' is a get-only property. Since I haven't set a value for it, it's value is always 0.

let angle = newPage.rotationAngle // value is 0

Is there a way I can create a CGPDFPage with rotation information available with it?

Upvotes: 1

Views: 74

Answers (0)

Related Questions