我遵循本指南https://blog.techchee.com/pdf-composer-app-swiftui/能夠在 PDF 上繪圖,并且能夠加載現有的 PDF 檔案并在其上繪圖,但現在我的所有繪圖相互重疊。
例如,我可以用一些用戶名和公司名稱填寫一次表格,但可能我拼寫錯誤并回傳修改它。當我再次打開 PdfView 時,它會在現有文本之上繪制,而不是像我預期的那樣重新開始。
有沒有辦法從 PdfDocument 的資料中洗掉所有繪圖并重新開始或類似的東西?
我有 XCode 12 和 Swift 5。謝謝!
這是一個非常簡單的例子:
用戶等級:
class User {
var name: String
var company: String
init(name: String, company: String) {
self.name = name
self.company = company
}
}
PDF的創建:
struct PdfCreation {
private var user: User
private var fileName: String
let attributes = [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)
]
let pageRect = CGRect(x: 0, y: 0, width: (8.5 * 72.0), height: (11 * 72.0))
init(user: User) {
self.user = user
// can replace filename with any resource saved in project
// I have a pdf named Check.pdf that I use
self.fileName = "Check"
}
private func resourceUrl() -> URL? {
if let resourceUrl = Bundle.main.url(forResource: self.fileName,
withExtension: "pdf") {
return resourceUrl
}
return nil
}
func getPdfData() -> Data? {
if let url = resourceUrl() {
if let doc = PDFDocument(url: url) {
let page: PDFPage = doc.page(at: 0)!
var mediaBox: CGRect = page.bounds(for: .mediaBox)
let context = CGContext(url as CFURL, mediaBox: &mediaBox, nil)
UIGraphicsPushContext(context!)
context!.beginPDFPage(nil)
// Draws the PDF into the context
page.draw(with: .mediaBox, to: context!)
let flipVertical: CGAffineTransform = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: mediaBox.size.height)
context!.concatenate(flipVertical)
// draw anything you want here
addName()
addCompany()
context!.endPDFPage()
context?.closePDF()
UIGraphicsPopContext()
let pdfData = doc.dataRepresentation()
return pdfData
}
return nil
}
return nil
}
}
extension PdfCreation {
private func addName() {
let width = 200
let x = Int(self.pageRect.width) - width
let y = 80
let height = 40
let nameRect = CGRect(x: x, y: y, width: width, height: height)
user.name.draw(in: nameRect, withAttributes: attributes)
}
private func addCompany() {
let x = 180
let y = 125
let width = Int(self.pageRect.width) - x - 100
let height = 40
let companyRect = CGRect(x: x, y: y, width: width, height: height)
user.company.draw(in: companyRect, withAttributes: attributes)
}
}
PDF查看
struct PdfViewer: UIViewRepresentable {
private var data: Data? // pdf data
init(data: Data?) {
self.data = data
}
func makeUIView(context: Context) -> PDFView {
let pageRect = CGRect(x: 0, y: 0, width: (8.5 * 72.0), height: (11 * 72.0))
let pdfView = PDFView(frame: pageRect)
pdfView.autoScales = true
// create PDFDocument from the data given
if let data = self.data {
pdfView.document = PDFDocument(data: data)
}
return pdfView
}
func updateUIView(_ uiView: PDFView, context: Context) {
// Empty
}
}
導航到的實際視圖
struct PdfPreviewViewer: View {
private var user: User
private var data: Data?
init(user: User) {
self.user = user
self.data = PdfCreation(user: user).getPdfData()
}
func actionSheet() {
// share pdf
let activityVC = UIActivityViewController(activityItems: [self.data as Any], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
}
var body: some View {
PdfViewer(data: data)
.navigationTitle(Text("Your PDF"))
.toolbar() {
ToolbarItemGroup(placement: .bottomBar) {
Spacer()
// Share Button
Button(action: actionSheet) {
Image(systemName: "square.and.arrow.up")
}
}
}
}
}
uj5u.com熱心網友回復:
我沒有仔細閱讀您的所有代碼,但這是過去對我有用的:
extension PDFPage {
func clearAnnotations() {
// get only our annotations, not the other annotations on the page
let ourAnnotations = self.annotations.filter { $0.id == MyAnnotation.id} // <-- here adjust for your purpose
for anno in ourAnnotations {
self.removeAnnotation(anno)
}
}
}
// use it like this to clear all annotations
if let page = pdfView?.document?.page(at: 0) {
page.clearAnnotations()
}
uj5u.com熱心網友回復:
我從作業狗的回答中獲得了一些靈感,并決定從繪圖轉向 PDFAnnotations,結果比我想象的還要好。我使用本指南繪制注釋:
https://pspdfkit.com/blog/2021/adding-annotations-in-swift-with-pdfkit-vs-pspdfkit/
通過首先加載我的 PDF,然后將注釋添加到 PDFView,我能夠提取資料并共享它,并在導航到查看頁面時更改注釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411409.html
標籤:
上一篇:我如何安裝以前的版本react-native-pdf
下一篇:Python多答案回圈
