#include "fpdf_annot.h"
#include "fpdf_attachment.h"
#include "fpdf_catalog.h"
#include "fpdf_dataavail.h"
#include "fpdf_doc.h"
#include "fpdf_edit.h"
#include "fpdf_ext.h"
#include "fpdf_flatten.h"
#include "fpdf_formfill.h"
#include "fpdf_fwlevent.h"
#include "fpdf_javascript.h"
#include "fpdf_ppo.h"
#include "fpdf_progressive.h"
#include "fpdf_save.h"
#include "fpdf_searchex.h"
#include "fpdf_signature.h"
#include "fpdf_structtree.h"
#include "fpdf_sysfontinfo.h"
#include "fpdf_text.h"
#include "fpdf_thumbnail.h"
#include "fpdf_transformpage.h"
#include "fpdfview.h"
#include "cpp/fpdf_scopers.h"
#include "cpp/fpdf_deleters.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <memory>
using namespace std;
constexpr int pageNum = 1;
constexpr double width = 1200.0;
constexpr double height = 800.0;
// 读取文件内容到内存
std::vector<uint8_t> GetFileContents(const std::string& filename) {
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (!file) {
throw std::runtime_error("Failed to open: " + filename);
}
std::streamsize file_length = file.tellg();
if (file_length == -1) {
throw std::runtime_error("Failed to get file size: " + filename);
}
file.seekg(0, std::ios::beg);
if (file_length == 0) {
return {};
}
std::vector<uint8_t> buffer(file_length);
if (!file.read(reinterpret_cast<char*>(buffer.data()), file_length)) {
throw std::runtime_error("Failed to read: " + filename);
}
return buffer;
}
// 自定义删除器
struct FreeDeleter { void operator()(FPDF_WCHAR* p) const { free(p); } };
using ScopedFPDFWideString = std::unique_ptr<FPDF_WCHAR[], FreeDeleter>;
// 将宽字符串转换为 FPDF_WCHAR 类型
ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
size_t length = (wstr.size() + 1) * sizeof(FPDF_WCHAR);
ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
for (size_t i = 0; i < wstr.size(); ++i) {
result.get()[i] = static_cast<FPDF_WCHAR>(wstr[i]);
}
result.get()[wstr.size()] = 0;
return result;
}
// PDFium 保存 FPDF_FILEWRITE 绑定的函数
int WriteBlockPDF(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size) {
static std::ofstream outFile("D:/multi_font_text.pdf", std::ios::binary | std::ios::trunc);
if (!outFile.is_open()) {
cerr << "Failed to open file for writing." << endl;
return -1;
}
outFile.write(static_cast<const char*>(buffer), size);
if (!outFile) {
cerr << "Failed to write data to file." << endl;
return -1;
}
return 1;
}
// 加载字体
FPDF_FONT LoadFont(FPDF_DOCUMENT document, const std::string& font_path, float font_size, bool is_truetype) {
std::vector<uint8_t> font_data = GetFileContents(font_path);
FPDF_FONT font = FPDFText_LoadFont(document, font_data.data(), font_data.size(),
is_truetype ? FPDF_FONT_TRUETYPE : FPDF_FONT_TYPE1, 1);
if (!font) {
throw runtime_error("Failed to load font from path: " + font_path);
}
return font;
}
// 创建文本对象
FPDF_PAGEOBJECT CreateTextObject(FPDF_DOCUMENT document, FPDF_FONT font, float font_size,
const std::wstring& text, double x, double y) {
FPDF_PAGEOBJECT text_obj = FPDFPageObj_CreateTextObj(document, font, font_size);
if (!text_obj) {
throw runtime_error("Failed to create text object!");
}
ScopedFPDFWideString fpdfWideString = GetFPDFWideString(text);
FPDFText_SetText(text_obj, fpdfWideString.get());
FPDFPageObj_Transform(text_obj, 1, 0, 0, 1, x, y);
return text_obj;
}
// 添加文本到页面
void AddTextToPage(FPDF_DOCUMENT document, FPDF_PAGE page, FPDF_FONT font, float font_size,
const std::wstring& text, double x, double y) {
FPDF_PAGEOBJECT text_obj = CreateTextObject(document, font, font_size, text, x, y);
FPDFPage_InsertObject(page, text_obj);
}
int main() {
try {
FPDF_InitLibrary();
// 创建新的 PDF 文档
FPDF_DOCUMENT document = FPDF_CreateNewDocument();
if (!document) {
throw runtime_error("Failed to create PDF document!");
}
// 创建新页面
FPDF_PAGE page = FPDFPage_New(document, pageNum, width, height);
if (!page) {
throw runtime_error("Failed to create new page!");
}
// 加载不同字体
FPDF_FONT segoeui_font = LoadFont(document, "C:/Windows/Fonts/segoeui.ttf", 12.0f, true);
//FPDF_FONT times_new_roman_font = LoadFont(document, "D:\\times.ttf", 14.0f, true);
// 添加文本对象到页面
//AddTextToPage(document, page, arial_font, 12.0f, L"Hello, Arial Font!", 300, 700);
AddTextToPage(document, page, segoeui_font, 14.0f,L" منها، فوجئت بنفسي وشعرت أنها غريبة ومملة. لكن الوقت", 300, 650);
可以继续添加更多文本
//AddTextToPage(document, page, arial_font, 12.0f, L"This is another line in Arial.", 300, 600);
//AddTextToPage(document, page, times_new_roman_font, 14.0f, L"And this one in Times New Roman.", 300, 550);
// 生成页面内容
FPDFPage_GenerateContent(page);
// 关闭页面
FPDF_ClosePage(page);
// 保存 PDF 文件
FPDF_FILEWRITE fileWrite = { 1, WriteBlockPDF };
if (!FPDF_SaveAsCopy(document, &fileWrite, FPDF_NO_INCREMENTAL)) {
throw runtime_error("Failed to save PDF.");
}
// 关闭文档
FPDF_CloseDocument(document);
cout << "PDF saved successfully." << endl;
}
catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
FPDF_DestroyLibrary();
return 0;
}
pdfium 文本
最新推荐文章于 2025-06-11 00:00:00 发布