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

Add text to PDF Page using CGPDFContext on OS X

我试图通过 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);
    }

我发现您的字体名称 Helvetic 有错字a

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