在OS X上使用CGPDFContext将文本添加到PDF页面

时间:2016-02-01 00:57:34

标签: objective-c macos cgpdfdocument cgpdf

我试图通过CGPDF和CGPDFContext将文本添加到现有的PDF文件中,该文件有5页,但是失败了。我设置了字体,DrawingMode,FillColor ......我在代码中遗漏了什么?

    CGContextRef writeContext = NULL;
    CGRect mediaBox;
    CGPDFPageRef page;
    size_t i;

        page = CGPDFDocumentGetPage(inputDoc, 1);
        mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

        if (! writeContext)
            writeContext = CGPDFContextCreateWithURL(url, &mediaBox, NULL);





        CGPDFContextBeginPage(writeContext, NULL);
        CGContextDrawPDFPage(writeContext, page);

        //Add text

        CGContextSelectFont(writeContext, "Helvetic", 26, kCGEncodingMacRoman);
        CGContextSetTextDrawingMode(writeContext, kCGTextFill);
        CGContextSetRGBFillColor(writeContext, 0, 0, 0, 1);
        const char *text="Hello Text Test";
        CGContextShowTextAtPoint(writeContext, 100, 100, text, strlen(text));


        //=========

        CGPDFContextEndPage(writeContext);
    }

    if (writeContext)
    {
        CGPDFContextClose(writeContext);
        CGContextRelease(writeContext);
    }

1 个答案:

答案 0 :(得分:0)

我发现您的字体名称为Helvetic a

CGContextSelectFont(writeContext, "Helvetica", 26, kCGEncodingMacRoman);