将 PDF 文档的创建从 iOS 移植到 Mac OS X

发布于 2024-11-29 04:24:30 字数 1763 浏览 3 评论 0原文

我正在将我的代码从 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();

I am porting my code from iPhone to Mac and I have no idea how I can do this in Mac. Here's my code that I am trying to convert and I know that there's no UIGraphic in Mac. Can someone point me to a guide or give me a quick hint? Thanks.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜扑 2024-12-06 04:24:30

使用CGPDFContextCreateWithURL而不是UIGraphicsBeginPDFContextToFile(参数非常相似)。要开始/结束页面,请使用 CGPDFContextBeginPage 和 CGPDFContextEndPage 。完成后,调用 CGPDFContextClose 而不是 UIGraphicsEndPDFContext

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

Use CGPDFContextCreateWithURL instead of UIGraphicsBeginPDFContextToFile (the parameters are very similar). To begin/end pages, use CGPDFContextBeginPage and CGPDFContextEndPage. When you're done, call CGPDFContextClose instead of UIGraphicsEndPDFContext.

The rest can remain the same – Core Graphics exists on both iOS and Mac OS X – which also means that you could use the functions I've mentioned above on iOS as well if you want to use the same code on both platforms.

醉梦枕江山 2024-12-06 04:24:30

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")
    }

Swift 4, macOS High Sierra Update

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")
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文