// src/main.cpp
#include <pdfium/fpdfview.h>
#include <pdfium/fpdf_annot.h>
#include <pdfium/fpdf_save.h>
#include <pdfium/fpdf_edit.h>
#include <iostream>
#include <string>
#include <codecvt>
#include <locale>
FPDF_WIDESTRING ConvertToWideString(const std::string& utf8) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wideStr = converter.from_bytes(utf8);
return reinterpret_cast<FPDF_WIDESTRING>(wideStr.c_str());
}
static FILE *s_pdf_df = nullptr;
int WriteBlockImpl(FPDF_FILEWRITE *pThis, const void *pData, unsigned long size)
{
fwrite(pData, size, 1, s_pdf_df);
return 1;
}
int main() {
// Initialize PDFium and load the document
FPDF_LIBRARY_CONFIG pdf_library_config;
pdf_library_config.version = 2;
pdf_library_config.m_pUserFontPaths = NULL;
pdf_library_config.m_pIsolate = NULL;
pdf_library_config.m_v8EmbedderSlot = 0;
FPDF_InitLibraryWithConfig(&pdf_library_config);
FPDF_DOCUMENT document = FPDF_LoadDocument("input.pdf", nullptr);
FPDF_PAGE page = FPDF_LoadPage(document, 0); // Load the first page
// Create the annotation
FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_FREETEXT);
// Set the annotation rectangle
FS_RECTF rect;
rect.bottom = 100.0;
rect.top = 650.0;
rect.left = 200.0;
rect.right = 450.0;
FPDFAnnot_SetRect(annot, &rect);
//FPDF_PAGEOBJECT text_object = FPDFPageObj_NewTextObj(document, "TimesNewRoman", 12.0f);
std::u16string contents_wide = u"WWW trying to get some text to display in an annotation in my pdf using the Stamp and Ink annotations. I'd like the text to wrap in the bounding rect, but I've so far only been able to get it to come out on one line. Is there a way to get text to wrap within the existing API? I'm trying it out in annotations because they fit in with some of the other features I'm using.But if there's another way to do it I'll gladly switch gears.";
const char key_Contents[] = "Contents";
FPDF_WIDESTRING contents_pdf_wide = reinterpret_cast<FPDF_WIDESTRING>(contents_wide.c_str());
FPDFAnnot_SetStringValue(annot, FPDFAnnot_GetSubtype(annot) == FPDF_ANNOT_FREETEXT ? "Contents" : "T", contents_pdf_wide);
std::wstring val(L"/TiRo 12 Tf 1 0 0 rg");
FPDFAnnot_SetStringValue(annot, "DA", (FPDF_WIDESTRING)val.c_str());
FPDFPage_CloseAnnot(annot);
// Add text content to the annotation
s_pdf_df = fopen("out.pdf", "wb");
FPDF_FILEWRITE file_output_wiriter;
file_output_wiriter.version = 1;
file_output_wiriter.WriteBlock = WriteBlockImpl;
FPDF_SaveAsCopy(document, &file_output_wiriter, 0);
//SavePDFToFile(document, "output.pdf");
// Cleanup
FPDF_ClosePage(page);
FPDF_CloseDocument(document);
FPDF_DestroyLibrary();
return 0;
}