PDF在Cocoa中创建

编程入门 行业动态 更新时间:2024-10-26 01:33:29
本文介绍了PDF在Cocoa中创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这里用下面的代码 PDF 获得创建但我能够 code>在 PDF .But我不能在中渲染NSView

Here with below code PDF gets created but i am able to write in PDF.But i am not able to render NSView in PDF

CGContextRef aCgPDFContextRef = [self createPDFContext:CGRectMake(0, 0, 612, 892) path:(CFStringRef)filepath]; CGContextBeginPage(aCgPDFContextRef,nil); //Draw view into PDF NSView *aPageNote=[[NSView alloc]initWithFrame:CGRectMake(0,0, 612, 892)]; CGAffineTransform aCgAffTrans = CGAffineTransformIdentity; aCgAffTrans = CGAffineTransformMakeTranslation(0,892); aCgAffTrans = CGAffineTransformScale(aCgAffTrans, 1.0, -1.0); CGContextConcatCTM(aCgPDFContextRef, aCgAffTrans); NSString *strText = @"Test 123333333333333333"; [strText drawAtPoint:NSMakePoint(100, 200) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:40.0],NSFontAttributeName,nil]];//drawAtPoint:CGPointMake(200, 300) withFont:[NSFont systemFontOfSize:20]]; [aPageNote setWantsLayer:YES]; [aPageNote.layer renderInContext:aCgPDFContextRef]; CGContextEndPage(aCgPDFContextRef); CGContextRelease (aCgPDFContextRef); NSLog(@"Pdf Successfully Created");

方法:

-(CGContextRef) createPDFContext:(CGRect)aCgRectinMediaBox path:(CFStringRef) aCfStrPath { CGContextRef aCgContextRefNewPDF = NULL; CFURLRef aCfurlRefPDF; aCfurlRefPDF = CFURLCreateWithFileSystemPath (NULL,aCfStrPath,kCFURLPOSIXPathStyle,false); if (aCfurlRefPDF != NULL) { aCgContextRefNewPDF = CGPDFContextCreateWithURL (aCfurlRefPDF,&aCgRectinMediaBox,NULL); CFRelease(aCfurlRefPDF); } return aCgContextRefNewPDF; }

EDIT :我可以使用此代码在中输入

CGContextBeginPage(aCgPDFContextRef,nil); NSString *strText1 = @"I am iOS Developer"; CGContextSelectFont (aCgPDFContextRef, "Helvetica", 24, kCGEncodingMacRoman); CGContextSetTextDrawingMode (aCgPDFContextRef, kCGTextFill); CGContextSetRGBFillColor (aCgPDFContextRef, 0, 0, 0, 1); const char *text1 = [strText1 UTF8String]; CGContextShowTextAtPoint (aCgPDFContextRef, 50, 375, text1, strlen(text1)); CGContextEndPage(aCgPDFContextRef);

推荐答案

EDIT : Render NSView并像 PDF 一样添加为

EDIT : Render NSView and add as image into PDF like this:

//Firstly render NS element into NSImage NSImage *i = [[NSImage alloc] initWithData:[yourView dataWithPDFInsideRect:[yourView bounds]]]; //Now u can add in PDF CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)[i TIFFRepresentation], NULL); CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL); CGContextDrawImage(aCgPDFContextRef,[yourView bounds], imageRef);

更多推荐

PDF在Cocoa中创建

本文发布于:2023-10-31 01:49:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PDF   Cocoa

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!