I want to add text in PDFkit PDF/Annotation in swiftui and overwrite it in write.

Asked 2 years ago, Updated 2 years ago, 382 views

Currently, I am creating an application to display PDF data on swiftui.
You cannot add text to the displayed PDF and print it.

Add text to PDF data with PDFannnotation, write .document?.write(to:path!), and use the 8th generation simulator
The text is printed with an additional text, but when you do it on the actual machine, the text is not added.

Changing PDF data to Sample PDF - Adobe Help Center didn't improve, so it doesn't seem to be a PDF problem.

If you know any solutions or modifications, we would appreciate it if you could give me some advice.

Please let me know if you need any information.

Below are the relevant parts of the code used.

struct PDFViewer:UIViewRepresentable{
@ObservedObject varpdfInfo —PDFInfo
let url: URL = Bundle.main.url (forResource: "sample", withExtension: "pdf")!

func makeUIView(context:UIViewRepresentableContext<PDFViewer>)->PDFViewer.UIViewType{
    // fit the screen size
    pdfInfo.pdfView.autoScales=true
    // Display only single page (cannot be turned over if this is inserted)
    //        pdfView.displayMode=.singlePage
    // Display using pageViewController (displayMode is ignored)
    pdfInfo.pdfView.usePageViewController(true)
    // Scroll horizontally
    pdfInfo.pdfView.displayDirection=.horizontal
    // Scroll vertically
    //        pdfInfo.pdfView.displayDirection=.vertical
    // Fill in the margin
    //        pdfInfo.pdfView.displayPageBreaks=true
    
    pdfInfo.pdfView.document=PDFDocument(url:url)
    let page_test=pdfInfo.pdfView.document?page(at:0)
    
    // Output input value
    variable = CGRect (x:58, y:733, width:300, height:50)
    vartext = PDFAnotation (bound:rect, forType: .freeText, with Properties:nil)
    text.color=.clear
    text.font = UIFont.systemFont (ofSize:10)
    text.fontColor=.black
    text.contents="test"
    page_test ?.addAnnotation(text)
    
    
    let path = Bundle.main.url (forResource: "sample_input", withExtension: "pdf")
    page_test?.document?.write(to:path!)
    
    
    
    return pdfInfo.pdfView
}

swiftui

2022-09-30 21:55

1 Answers

This line specifies the URL for the resource.

let path=Bundle.main.url (forResource: "sample_input", withExtension: "pdf")

Resource URLs are not allowed to be written (on the actual machine).Create and write a URL that points to a writable location from the app.


2022-09-30 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.