I add pdfdocument to the PDFView . but it is showing margin and also showing in the canter. How to remove this margin . And Is there any way to set pdfdocument to the top-left position.
Asked
Active
Viewed 1,776 times
2 Answers
8
I think this would help with the problem.
https://developer.apple.com/documentation/pdfkit/pdfview/2881210-pagebreakmargins
Just set a value to this variable pageBreakMargins
by passing a UIEdgeInsets
value.
Something like this
self.pdfView.pageBreakMargins = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

jyce1492
- 199
- 2
- 15
-
This is the best answer! Thanks! – Silvering Sep 01 '23 at 13:44
1
- To remove the margin: you need to set pageShadowsEnabled to false.
- Change the position: You need to adjust height of the PdfView.
Here is the sample code:
private func openPdf() {
let pdfView = PDFView(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: self.view.frame.height))
view.addSubview(pdfView)
guard let path = Bundle.main.url(forResource: "test", withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
pdfView.pageShadowsEnabled = false
pdfView.displayMode = .singlePage
pdfView.autoScales = true
pdfView.frame.size.height = pdfView.documentView?.frame.height ?? self.view.frame.height
self.view.layoutIfNeeded()
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
}
}

Razib Mollick
- 4,617
- 2
- 22
- 20