使用硬编码创建简单的pdf文件

时间:2010-11-25 10:29:00

标签: iphone

我已经以编程方式生成了pdf bt问题是文本没有出现在pdf中。 PLz为我提供了pdf创建者的简单示例。

这是我的代码:

-(void) MyCreatePDFFile //(CGRect pageRect, const char *filename)// 1
{

 CGRect pageRect=CGRectMake(10,10,400,600);
 const char *filename="/Users/msoni/Desktop/my.pdf";

    CGContextRef pdfContext;

    CFStringRef path;

    CFURLRef url;

    CFMutableDictionaryRef myDictionary = NULL;

 //const char *filename="a.pdf";

    path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8);

    url = CFURLCreateWithFileSystemPath (NULL, path,kCFURLPOSIXPathStyle, 0);

    CFRelease (path);

    myDictionary = CFDictionaryCreateMutable(NULL, 0,

            &kCFTypeDictionaryKeyCallBacks,

            &kCFTypeDictionaryValueCallBacks); // 4

    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));

    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));

    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); // 5

    CFRelease(myDictionary);

    CFRelease(url);

    CGContextBeginPage (pdfContext, &pageRect); // 6
 CGContextStrokeRect(pdfContext, CGRectMake(20, 100, 200,300));
 const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
 CGContextShowTextAtPoint (pdfContext, 100, 400, text, strlen(text));
 //CGContextSetTextDrawingMode (pdfContext);
 CGContextEndPage (pdfContext);// 8

    CGContextRelease (pdfContext);// 9

}

等待回复..

感谢

1 个答案:

答案 0 :(得分:0)

使用以下代码和本教程的网址为link text http://www.iphonedevsdk.com/forum/tutorial-requests/8560-tutorial-request-creating-pdf-file-iphone-app.html

nameText = [NSString stringWithFormat:@“%@”,nameField.text];

然后使用

CGContextSelectFont(pdfContext,“Helvetica”,24,kCG​​EncodingMacRoman); CGContextSetTextDrawingMode(pdfContext,kCGTextFill); CGContextSetRGBFillColor(pdfContext,0,0,0,1); const char * text2 = [nameText UTF8String]; CGContextShowTextAtPoint(pdfContext,50,375,text2,strlen(text2));