Requesting help checking for issue in implementing freetext

148 views
Skip to first unread message

ods

unread,
Dec 1, 2023, 2:01:47 PM12/1/23
to pdfium
Hello, I am trying to use pdfium to implement the freetext feature. The following is my code. The strange thing is that the annot rectangular box is displayed on the interface but no text is displayed. If you could help me with the issue, please write me back, thank you.

FPDF_ANNOTATION annot = FPDFPage_CreateAnnot((FPDF_PAGE)pagePtr, 3);
FPDFAnnot_SetFlags(annot, 4);
FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 0x00, 0x00, 0x00, 0xFF);
FS_RECTF rect{100.0f, 100.0f, 500.0f, 200.0f};
FPDFAnnot_SetRect(annot, &rect);
unsigned short *kContents = convertWideString(env, "Hello PDF");
FPDFAnnot_SetStringValue((FPDF_ANNOTATION)annotPtr, "Contents", kContents);
FPDFPage_GenerateContent((FPDF_PAGE)pagePtr);

Dimuthu Upeksha

unread,
Nov 15, 2024, 10:29:08 AM11/15/24
to pdfium
This works fine for me

// 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;
}

Lei Zhang

unread,
May 8, 2025, 2:43:40 PMMay 8
to Dimuthu Upeksha, pdfium
I also fixed https://crbug.com/380434959 recently, so PDFium renders
freetext annotations better.
> --
> You received this message because you are subscribed to the Google Groups "pdfium" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pdfium+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/pdfium/26188378-4608-4db2-a44b-763abe0f6aa8n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages