#include podofo/podofo.h #include iostream // All PoDoFo classes are member of the PoDoFo namespace. //using namespace std; using namespace PoDoFo; PdfFont* getFont(PdfDocument doc); // Base14 other non-Base14 fonts for comparison static const char* s_base14fonts[] { Times-Roman, Times-Italic, Times-Bold, Times-BoldItalic, Helvetica, Helvetica-Oblique, Helvetica-Bold, Helvetica-BoldOblique, Courier, Courier-Oblique, Courier-Bold, Courier-BoldOblique, Symbol, ZapfDingbats, Arial, Verdana }; const char* GetBase14FontName(unsigned i) { if (i std::size(s_base14fonts)) return nullptr; return s_base14fonts[i]; } void DrawRedFrame(PdfPainter painter, double x, double y, double width, double height) { // draw red box painter.GraphicsState.SetFillColor(PdfColor(1.0f, 0.0f, 0.0f)); painter.GraphicsState.SetStrokeColor(PdfColor(1.0f, 0.0f, 0.0f)); painter.DrawLine(x, y, x width, y); if (height 0.0f) { painter.DrawLine(x, y, x, y height); painter.DrawLine(x width, y, x width, y height); painter.DrawLine(x, y height, x width, y height); } // restore to black painter.GraphicsState.SetFillColor(PdfColor(0.0f, 0.0f, 0.0f)); painter.GraphicsState.SetStrokeColor(PdfColor(0.0f, 0.0f, 0.0f)); } void DemoBase14Fonts(PdfPainter painter, PdfPage page, PdfDocument document) { PdfFontSearchParams params; params.AutoSelect PdfFontAutoSelectBehavior::Standard14; double x 56, y page.GetRect().Height - 56.69; std::string_view demo_text abcdefgABCDEFG12345!#$%-? ; double height 0.0f, width 0.0f; // draw sample of all types for (unsigned i 0; i std::size(s_base14fonts); i) { x 56; y y - 25; std::string text; if (i 12) { // Or u8♠♣♥♦: Symbol font doesnt support regular characters text u8\\u2660\\u2663\\u2665\\u2666; } else if (i 13) { // Or u8❏❑▲▼: ZapfDingbats font doesnt support regular characters text u8\\u274f\\u2751\\u25b2\\u25bc; } else { text demo_text; text.append(GetBase14FontName(i)); } PdfFont* font document.GetFonts().SearchFont(GetBase14FontName(i), params); font document.GetFonts().GetStandard14Font(PdfStandard14FontType::Helvetica); font getFont(document); if (font nullptr) throw std::runtime_error(Font not found); painter.TextState.SetFont(*font, 12.0); width font-GetStringLength(text, painter.TextState); height font-GetLineSpacing(painter.TextState); std::cout GetBase14FontName(i) Width width Height height std::endl; // draw red box DrawRedFrame(painter, x, y, width, height); // draw text painter.DrawText(text, x, y); } // draw some individual characters: std::string_view demo_text2 _1jiPlg .;; auto helveticaStd14 document.GetFonts().SearchFont(Helvetica, params); auto arialImported document.GetFonts().SearchFont(Arial); helveticaStd14 getFont(document); arialImported getFont(document); auto metrics arialImported-GetMetrics(); std::cout Non base 14 font characteristics std::endl; std::cout Font name: metrics.GetFontName() std::endl; std::cout Family font name: metrics.GetFontFamilyName() std::endl; std::cout Font file path: metrics.GetFilePath() std::endl; std::cout Font face index: metrics.GetFaceIndex() std::endl; // draw individuals for (unsigned i 0; i demo_text2.length(); i) { x 56; y y - 25; std::string text; if (i 0) text Helvetica / Arial Comparison:; else text (std::string)demo_text2.substr(i, 1); painter.TextState.SetFont(*helveticaStd14, 12); height helveticaStd14-GetLineSpacing(painter.TextState); width helveticaStd14-GetStringLength(text, painter.TextState); // draw red box DrawRedFrame(painter, x, y, width, height); // draw text painter.DrawText(text, x, y); if (i 0) { // draw again, with non-Base14 font painter.TextState.SetFont(*arialImported, 12); height arialImported-GetLineSpacing(painter.TextState); width arialImported-GetStringLength((std::string_view)text, painter.TextState); // draw red box DrawRedFrame(painter, x 100, y, width, height); // draw text painter.DrawText(text, x 100, y); } } } void drawSquareWithCross(PdfPainter painter, double x, double y) { painter.Save(); const double SquareSize 6; painter.GraphicsState.SetLineWidth(0.6); painter.DrawRectangle(x - SquareSize / 2, y - SquareSize / 2, SquareSize, SquareSize); painter.GraphicsState.SetLineWidth(0); painter.DrawLine(x, y - SquareSize / 2, x, y SquareSize / 2); painter.DrawLine(x - SquareSize / 2, y, x SquareSize / 2, y); painter.Restore(); } void createDocument2(const char* filename) { PdfMemDocument doc; auto page doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); PdfFontCreateParams params; params.Encoding PdfEncodingMapFactory::WinAnsiEncodingInstance(); auto font doc.GetFonts().GetStandard14Font(PdfStandard14FontType::Helvetica, params); PdfPainter painter; auto operators static_castPdfContentStreamOperators(painter); painter.SetCanvas(page); painter.TextState.SetFont(font, 15); painter.TextObject.Begin(); painter.TextObject.MoveTo(100, 500); painter.TextObject.AddText(Test1); // Some low level operations operators.TJ_Operator_Begin(); operators.TJ_Operator_Glyphs(_W, false); operators.TJ_Operator_Delta(-500); operators.TJ_Operator_Glyphs(orld, false); operators.TJ_Operator_End(); painter.TextObject.End(); painter.DrawText(Test2, 100, 600, PdfDrawTextStyle::StrikeThrough); PdfPainterPath path; path.MoveTo(20, 20); path.AddArcTo(150, 20, 150, 70, 50); path.AddLineTo(150, 120); path.AddArc(200, 120, 50, 3.14159, 3.14159 / 8, true); auto currPoint1 path.GetCurrentPoint(); PdfPainterPath path2; path2.MoveTo(250, 120); path2.AddLineTo(250, 80); path.AddPath(path2, true); auto currPoint2 path.GetCurrentPoint(); painter.DrawPath(path, PdfPathDrawMode::Stroke); path.Reset(); path.MoveTo(40, 40); path.AddLineTo(100, 40); path.AddLineTo(70, 80); path.AddLineTo(40, 40); path.AddCircle(200, 300, 60); painter.DrawPath(path, PdfPathDrawMode::Fill); drawSquareWithCross(painter, 100, 20); drawSquareWithCross(painter, 100, 70); drawSquareWithCross(painter, 150, 70); drawSquareWithCross(painter, currPoint1.X, currPoint1.Y); drawSquareWithCross(painter, currPoint2.X, currPoint2.Y); painter.FinishDrawing(); doc.Save(filename); } // 创建一个PDF文件 void testCreatePDF(const char* filename) { PdfMemDocument document; PdfPainter painter; PdfFont* font; try { auto page document.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); painter.SetCanvas(page); font document.GetFonts().SearchFont(Microsoft YaHei); //font document.GetFonts().GetStandard14Font(PdfStandard14FontType::Helvetica); font getFont(document); if (font nullptr) throw std::runtime_error(Invalid handle); auto metrics font-GetMetrics(); std::cout The font name is metrics.GetFontName() std::endl; std::cout The family font name is metrics.GetFontFamilyName() std::endl; std::cout The font file path is metrics.GetFilePath() std::endl; std::cout The font face index is metrics.GetFaceIndex() std::endl; painter.TextState.SetFont(*font, 18); // painter.SetColor(1.0, 0.0, 0.0); painter.DrawText(ABCDEFGHIKLMNOPQRSTVXYZ, 56.69, page.GetRect().Height - 56.69); try { // Add also some non-ASCII characters (Cyrillic alphabet) painter.DrawText(АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЫЭЮЯ, 56.69, page.GetRect().Height - 80); } catch (PdfError err) { if (err.GetCode() PdfErrorCode::InvalidFontData) std::cout WARNING: The matched font metrics.GetFontName() doesnt support cyrillic std::endl; } //DemoBase14Fonts(painter, page, document); painter.FinishDrawing(); // Set some additional information on the PDF file. document.GetMetadata().SetCreator(PdfString(examplahelloworld - A PoDoFo test application)); document.GetMetadata().SetAuthor(PdfString(Dominik Seichter)); document.GetMetadata().SetTitle(PdfString(Hello World)); document.GetMetadata().SetSubject(PdfString(Testing the PoDoFo PDF Library)); document.GetMetadata().SetKeywords(std::vectorstd::string({ Test, PDF, Hello World })); // The last step is to close the document. document.Save(filename); } catch (PdfError e) { // All PoDoFo methods may throw exceptions // make sure that painter.FinishPage() is called // or who will get an assert in its destructor try { painter.FinishDrawing(); } catch (...) { // Ignore errors this time } throw e; } } #include windows.h HFONT createWinFont() { return CreateFont( -15/*高度*/, -7.5/*宽度*/, 0/*不用管*/, 0/*不用管*/, 400 /*一般这个值设为400*/, FALSE/*不带斜体*/, FALSE/*不带下划线*/, FALSE/*不带删除线*/, DEFAULT_CHARSET, //这里我们使用默认字符集还有其他以 _CHARSET 结尾的常量可用 OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, //这行参数不用管 DEFAULT_QUALITY, //默认输出质量 FF_DONTCARE, //不指定字体族*/ 微软雅黑 //字体名 ); } PdfFont* getFont(PdfDocument doc) { return doc.GetFonts().GetOrCreateFont(createWinFont()); } void testFonts() { } void test() { //auto fc GetFontConfigWrapper(); //fc.AddFontDirectory(C://fonts); //PdfCommon::AddFontDirectory(C:\\Users\\ws\Downloads\\podofo\\extern\\resources\\Fonts\\); PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::Warning); testCreatePDF(./test.pdf); createDocument2(./test2.pdf); }输出/*The font name is MicrosoftYaHeiThe family font name is Microsoft YaHeiThe font file path isThe font face index is 0*/参考GitHub - podofo/podofo: A C17 PDF manipulation libraryC pdf库总结-CSDN博客C PDF转图片-CSDN博客创作不易小小的支持一下吧
企业数字化 ERP 产品动态
相关推荐
快速重装系统 /* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/10 9:31:02
C++自定义可对称双向迭代器 /* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/9 8:53:17
C++ 列式内存布局数据存储格式 Arrow /* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/7 11:24:36
重庆考电工证难度大吗?在哪考更靠谱?别再被外地证卡脖子了 重庆考电工证难度大吗?在哪考更靠谱?别再被外地证卡脖子了 之前考的证在外省能不能转过来?这几乎是每个打算转行或者想在重庆发展的电工都问过的问题。别急,今天咱们就来唠唠 重庆考电工证难度 到底怎么样,以及 在哪考 更靠谱,帮你避开那些坑。… · 2026/11/11 13:39:50
淮安安监局电工证报名官方回应:怕考不过白交培训费?这些干货帮你避坑 淮安安监局电工证报名官方回应:怕考不过白交培训费?这些干货帮你避坑 怕考不过白交培训费?很多人在报名淮安安监局电工证时,心里最怕的不是考试内容,而是花了钱却拿不到证。其实,官方回应早已明确:只要符合条件、按流程操作,通过率是有保障的。本文从… · 2026/11/11 13:39:50
工地太忙根本没时间复习考试?考电工证试题大全全国通用的备考干货 工地太忙根本没时间复习考试?考电工证试题大全全国通用的备考干货 工地太忙根本没时间复习考试?你不是一个人。作为驻马店某项目现场管理员,我深知一线作业人员每天要面对高强度的工作节奏,还要抽出时间准备电工证考试,压力山大。别担心,今天就来聊聊… · 2026/11/11 13:39:50
工地太忙根本没时间复习考试?鞍山特种作业操作证在哪考全攻略来了 工地太忙根本没时间复习考试?鞍山特种作业操作证在哪考全攻略来了 工地太忙根本没时间复习考试?这几乎是每个特种作业操作证持有者的共同痛点。你不是不想考,也不是不想提升,而是时间真的不够用。尤其是鞍山地区的建筑、电力、制造等行业从业者,特种作业… · 2026/11/11 13:39:50
电工证快速出证报名入口与办理流程 高中毕业也能考电工证?快速出证流程和电子证书怎么拿 学历不高怕报不上名,但电工证真的不看学历?你不是一个人,很多人都是这样想的,尤其是咱们这些在工地、工厂里干活的兄弟。不过现在政策已经放宽, 电工证快速出证… · 2026/11/11 13:39:49
武汉电工证复审到哪里 郑州报考避坑指南 武汉电工证复审到哪里 郑州报考避坑指南 之前考的证在外省能不能转过来?这个问题困扰了不少在外地打工的电工朋友,尤其是像你在郑州金水区从事公路工程的同行,可能手里拿着外地的电工证,想在武汉继续从事相关工作,但不知道能不能用,更不知道复审该找谁… · 2026/11/11 13:38:49
电工证丢失还能补吗?千万别踩坑,丢了证等于失业 电工证丢失还能补吗?千万别踩坑,丢了证等于失业 你正在找工作,但手里的电工证突然丢了,这下怎么办?别急,先别慌, 电工证丢失还能补吗 ?答案是肯定的,但 千万别踩坑 ,补办流程如果不了解,可能会耽误你上岗、甚至影响到你的收入。… · 2026/11/11 12:18:07
电力电缆作业证直播报名入口与办理流程良心建议 电力电缆作业证直播报名入口与办理流程良心建议 你是不是也担心学历不高报不了名?别急,今天就给你讲讲 电力电缆作业证直播 的报名流程和那些你必须知道的干货, 良心建议 在这里,别错过。 电力电缆作业证直播报名需要什么条件?… · 2026/11/10 17:46:39
英德电工证报考费多少钱,怕考不过白交培训费?看完这篇别再踩坑 英德电工证报考费多少钱,怕考不过白交培训费?看完这篇别再踩坑 怕考不过白交培训费?英德电工证报考费多少钱,你真的了解吗?现在市面上培训机构五花八门,价格参差不齐,很多人一不小心就花了冤枉钱,结果还考不过。别急,今天就来给你说清楚英德电工证的… · 2026/11/10 17:11:43
_中职电工证最新政策解读 中职电工证在哪考?工地太忙根本没时间复习考试的救星来了 工地太忙根本没时间复习考试,这是很多正在备考 中职电工证 的建筑工友的真实写照。你不是不想考,而是真的没时间。但别担心,这期内容是 新乡初次报考人员… · 2026/11/11 0:00:52
昆明电工证培训价格值不值得考?老电工告诉你真相 昆明电工证培训价格值不值得考?老电工告诉你真相 之前考的证在外省能不能转过来?这个问题是很多在外地打工、换城市生活、或者是想在昆明发展的朋友最关心的。尤其是现在云南发展快,很多企业都在招持证上岗的电工,如果你手头有证但不在本地,那这个证是不… · 2026/11/11 0:00:52
特种电缆张工网上查询全攻略:外省证如何转过来 特种电缆张工网上查询全攻略:外省证如何转过来 之前考的证在外省能不能转过来?这是很多特种作业从业者最关心的问题,尤其是像特种电缆张工这类证书,直接关系到日常工作是否合规、能否顺利晋升。很多小伙伴因为证书归属地的问题,陷入职业发展的瓶颈,甚至… · 2026/11/11 0:00:52
双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试 /* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/10 20:19:29
STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析 /* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/10 19:22:45
DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案 /* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/10 17:00:47