如何在PDF文件中使用PoDoFo库设置文本颜色
下面是来自podofo GitHub的一个小型编译示例。
「SetColor」已被注释掉,我还没找到如何设置文本颜色。是针对整段文本,还是仅针对其中的某个单词设置颜色?
// undef for Windows, otherwise does not link (DrawText/DrawTextW)
#ifdef DrawText
#undef DrawText
#endif
#include <podofo/podofo.h>
using namespace PoDoFo;
int main()
{
PdfMemDocument document;
PdfPainter painter;
PdfFont* font;
auto& page = document.GetPages().CreatePage(PdfPageSize::A4);
painter.SetCanvas(page);
font = document.GetFonts().SearchFont("Arial");
// If the PdfFont object cannot be allocated return an error.
//if (font == nullptr)
// throw runtime_error("Invalid handle");
auto& metrics = font->GetMetrics();
painter.TextState.SetFont(*font, 18.0);
//PdfColor textColor(1.0, 0.0, 0.0); // red
//painter.SetColor(1.0, 0.0, 0.0);
painter.DrawText("Hello World!", 56.69, page.GetRect().Height - 56.69);
painter.FinishDrawing();
document.Save("C:\\Temp\\test.pdf");
}
解决方案
你需要调用:
painter.GraphicsState.SetNonStrokingColor(PdfColor(1.0, 0.0, 0.0));
请参阅 https://github.com/podofo/podofo/pull/331 以获取更新的示例代码。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。