2009-08-26 26 views
10

Tôi đang viết một ứng dụng trong mục tiêu-c (sử dụng ca cao). tôi có một mẫu PDF, tôi cần phải thay thế giá trị thực tế vào phần giữ chỗ trong PDF và sau đó lưu kết quả vào PDF mới.cách chỉnh sửa PDF trong mục tiêu-c?

Tôi có thể làm như thế nào? nên sử dụng thư viện nào?

Trả lời

23

Tôi đã tìm thấy giải pháp! Nó kết nối sức mạnh của quartz2d và đơn giản của UIGraphics.

NSString *newFilePath = @"path/to/your/newfile.pdf"; 
NSString *templatePath = @"path/to/your/template.pdf"; 

//create empty pdf file; 
UIGraphicsBeginPDFContextToFile(newFilePath, CGRectMake(0, 0, 792, 612), nil); 

CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)templatePath, kCFURLPOSIXPathStyle, 0); 

//open template file 
CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url); 
CFRelease(url); 

//get amount of pages in template 
size_t count = CGPDFDocumentGetNumberOfPages(templateDocument); 

//for each page in template 
for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) { 
    //get bounds of template page 
    CGPDFPageRef templatePage = CGPDFDocumentGetPage(templateDocument, pageNumber); 
    CGRect templatePageBounds = CGPDFPageGetBoxRect(templatePage, kCGPDFCropBox); 

    //create empty page with corresponding bounds in new document 
    UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    //flip context due to different origins 
    CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    //copy content of template page on the corresponding page in new file 
    CGContextDrawPDFPage(context, templatePage); 

    //flip context back 
    CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    /* Here you can do any drawings */ 
    [@"Test" drawAtPoint:CGPointMake(200, 300) withFont:[UIFont systemFontOfSize:20]]; 
} 
CGPDFDocumentRelease(templateDocument); 
UIGraphicsEndPDFContext(); 
+3

Tôi yêu bạn rất nhiều! – Seb

5

Có thể là PDFKit. Đối với một số tác vụ, API PDFKit cấp cao không thể thực hiện những gì bạn muốn và bạn có thể bị buộc sử dụng mức thấp CG PDF parsing libraries. Tuy nhiên, chúng khá thấp. Chúng có nghĩa là thực sự hiểu được định dạng tệp PDF.

0

Tôi biết rằng câu hỏi là về obj-c, nhưng nếu bạn đang ở đây vì chỉnh sửa pdf, bên dưới có một giải pháp trong Swift 3:

PS: Trong tôi giải pháp tôi cần để chỉnh sửa thông tin tài liệu của tệp PDF mới, vì vậy tôi đã sử dụng thông số chủ đề để thực hiện việc này.

func createPDF(on path: String?, from templateURL: URL?, with subject: String?) { 
    guard let newPDFPath = path, 
     let pdfURL = templateURL else { return } 

    let options = [(kCGPDFContextSubject as String): subject ?? ""] as CFDictionary 

    UIGraphicsBeginPDFContextToFile(newPDFPath, .zero, options as? [AnyHashable : Any]) 

    let templateDocument = CGPDFDocument(pdfURL as CFURL) 
    let pageCount = templateDocument?.numberOfPages ?? 0 

    for i in 1...pageCount { 

     //get bounds of template page 
     if let templatePage = templateDocument?.page(at: i) { 
      let templatePageBounds = templatePage.getBoxRect(.cropBox) 

      //create empty page with corresponding bounds in new document 
      UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil) 
      let context = UIGraphicsGetCurrentContext() 

      //flip context due to different origins 
      context?.translateBy(x: 0.0, y: templatePageBounds.height) 
      context?.scaleBy(x: 1.0, y: -1.0) 

      //copy content of template page on the corresponding page in new file 
      context?.drawPDFPage(templatePage) 

      //flip context back 
      context?.translateBy(x: 0.0, y: templatePageBounds.height) 
      context?.scaleBy(x: 1.0, y: -1.0) 
     } 
    } 
    UIGraphicsEndPDFContext() 
} 
Các vấn đề liên quan