【问题标题】:Porting creation of a PDF document from iOS to Mac OS X将 PDF 文档的创建从 iOS 移植到 Mac OS X
【发布时间】:2011-10-26 09:07:20
【问题描述】:

我正在将我的代码从 iPhone 移植到 Mac,但我不知道如何在 Mac 中执行此操作。这是我要转换的代码,我知道 Mac 中没有 UIGraphic。有人可以指点我的指南或给我一个快速提示吗?谢谢。

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();

【问题讨论】:

    标签: objective-c cocoa macos pdf-generation core-graphics


    【解决方案1】:

    使用CGPDFContextCreateWithURL 代替UIGraphicsBeginPDFContextToFile(参数非常相似)。要开始/结束页面,请使用 CGPDFContextBeginPageCGPDFContextEndPage。完成后,请致电 CGPDFContextClose 而不是 UIGraphicsEndPDFContext

    其余的可以保持不变——Core Graphics 在 iOS 和 Mac OS X 上都存在——这也意味着如果你想在两个平台上使用相同的代码,你也可以在 iOS 上使用我上面提到的功能.

    【讨论】:

    • 谢谢你!我需要安定下来并阅读一次 Quartz2D 编程指南...
    • 你有没有机会在 Swift 中做到这一点?我正在寻找一个例子。 :)
    【解决方案2】:

    Swift 4,macOS High Sierra 更新

    func generatePdfWithFilePath(thefilePath: String)
        {
            let url = URL(fileURLWithPath: thefilePath) as CFURL
    
            guard let currentContext = CGContext(url, mediaBox: nil, documentInfo as CFDictionary) else {
                return
            }
            self.context = currentContext
    
            self.context!.beginPDFPage(pageInfo as CFDictionary)
    
            drawReport()
    
            self.context!.endPDFPage()
    
    
            // Close the PDF context and write the contents out.
            self.context!.closePDF()
    
            self.context = nil
            //DebugLog("generatePdfWithFilePath() completed")
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 2017-07-04
      相关资源
      最近更新 更多