【问题标题】:drawAtPoint not working for PDF OS XdrawAtPoint 不适用于 PDF OS X
【发布时间】:2016-03-15 16:39:41
【问题描述】:

CGContextShowTextAtPoint 说它已被弃用,所以我想找到一种将文本绘制到 PDF 上下文的替代方法。以下工作(这是我想更改的代码):

CGPDFContextBeginPage(self.writeContext, pageDictionary);

 //Add text
 CGContextSelectFont(self.writeContext, "Helvetica", 26, kCGEncodingMacRoman);
 CGContextSetTextDrawingMode(self.writeContext, kCGTextFill);
 CGContextSetRGBFillColor(self.writeContext, 0, 0, 0, 1);
 const char *text="Hello Text Test";
 CGContextShowTextAtPoint(self.writeContext, 110, 0, text, strlen(text));

 CGPDFContextEndPage(self.writeContext);

但是这段代码不起作用(页面最终只是空白)

CGPDFContextBeginPage(self.writeContext, pageDictionary);

 //Add text
[@"Hello Text Test" drawAtPoint:CGPointMake(100, 100) withAttributes:@{NSFontAttributeName:[NSFont fontWithName:@"Helvetica" size:24]}];
 //=========

CGPDFContextEndPage(self.writeContext);

【问题讨论】:

    标签: objective-c macos pdf


    【解决方案1】:

    我认为您缺少 NSGraphicsContext

    完整代码如下:

    NSLog(@"Print PDF");
    CFURLRef filePath;
    NSSavePanel*    panel = [NSSavePanel savePanel];
    [panel setNameFieldStringValue:[self.selectedAccount.name stringByAppendingString:@".pdf"]];
    [panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
        if (result == NSFileHandlingPanelOKButton) {
    
            NSURL*  theFile = [panel URL];
            CGRect mediaBox = CGRectMake(0, 0, 576.0, 576.0);
            CFURLRef saveLocation = CFBridgingRetain(theFile);
            //dictionary to store doc attributes
            CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(NULL,
                                                                          3,
                                                                          &kCFTypeDictionaryKeyCallBacks,
                                                                          &kCFTypeDictionaryValueCallBacks);
    
            CFDictionaryAddValue(attributes, kCGPDFContextAuthor, CFSTR("Rich Claxton"));
            CFDictionaryAddValue(attributes, kCGPDFContextTitle, CFSTR("This is working"));
    
            CGContextRef pdfContext = CGPDFContextCreateWithURL(saveLocation, &mediaBox, attributes);
    
            /** Create NSGraphicsContext !! **/
            NSGraphicsContext* newGC = [NSGraphicsContext graphicsContextWithGraphicsPort:pdfContext flipped:NO];
            [NSGraphicsContext saveGraphicsState];
            [NSGraphicsContext setCurrentContext:newGC];
    
    
            CGPDFContextBeginPage(pdfContext, NULL);
    
            //Draw Strings here
            [@"Hello World !!" drawAtPoint:CGPointMake(2.0,2.0) withAttributes:(NULL)];
    
            CGPDFContextEndPage(pdfContext);
            CGPDFContextClose(pdfContext);
            CGContextRelease(pdfContext);
        }
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多