【问题标题】:Add text to PDF Page using CGPDFContext on OS X在 OS X 上使用 CGPDFContext 将文本添加到 PDF 页面
【发布时间】:2016-02-01 00:57:33
【问题描述】:

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

【问题讨论】:

    标签: objective-c macos cgpdfdocument cgpdf


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!你拯救了我的一天:)
    猜你喜欢
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多