开发者

Porting creation of a PDF document from iOS to Mac OS X

开发者 https://www.devze.com 2023-03-28 09:22 出处:网络
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

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 tem开发者_Python百科platePage = 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();


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消