done anotations update

This commit is contained in:
azeeee05
2026-06-11 11:46:15 +05:30
parent ab88a05d36
commit c410f787f7
6 changed files with 204 additions and 10 deletions
+86
View File
@@ -2250,6 +2250,92 @@ std::expected<void, EngineError> PdfiumDocument::applyEdits(const std::string& e
spdlog::warn("delete_annotation: annotation '{}' not found on page {}", targetId, pageIndex);
}
FPDF_ClosePage(page);
} else if (type == "update_annotation") {
if (!op.contains("data") || !op["data"].is_object()) {
spdlog::error("update_annotation operation missing 'data' object");
return std::unexpected(EngineError::InvalidFormat);
}
auto data = op["data"];
std::string targetId = data.value("annotationId", "");
if (targetId.empty()) {
spdlog::error("update_annotation missing 'annotationId'");
return std::unexpected(EngineError::InvalidFormat);
}
FPDF_PAGE page = FPDF_LoadPage(doc_, pageIndex);
if (!page) {
spdlog::error("Failed to load page index {} for update_annotation", pageIndex);
return std::unexpected(EngineError::Unknown);
}
int count = FPDFPage_GetAnnotCount(page);
FPDF_ANNOTATION targetAnnot = nullptr;
for (int i = 0; i < count; ++i) {
FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, i);
if (!annot) continue;
std::string id;
unsigned long len = FPDFAnnot_GetStringValue(annot, "NM", nullptr, 0);
if (len > 2) {
std::vector<uint8_t> buf(len);
FPDFAnnot_GetStringValue(annot, "NM", reinterpret_cast<FPDF_WCHAR*>(buf.data()), len);
id = utf16le_to_utf8(reinterpret_cast<const char16_t*>(buf.data()), len / sizeof(char16_t));
while (!id.empty() && id.back() == '\0') id.pop_back();
}
if (id.empty()) {
id = "anno_" + std::to_string(pageIndex) + "_" + std::to_string(i);
}
if (id == targetId) {
targetAnnot = annot;
break;
}
FPDFPage_CloseAnnot(annot);
}
if (targetAnnot) {
// Update Rect
if (data.contains("x") && data.contains("y") && data.contains("width") && data.contains("height")) {
double pageHeight = FPDF_GetPageHeightF(page);
float x = static_cast<float>(data["x"].get<double>());
float y = static_cast<float>(data["y"].get<double>());
float width = static_cast<float>(data["width"].get<double>());
float height = static_cast<float>(data["height"].get<double>());
FS_RECTF rect;
rect.left = x;
rect.right = x + width;
rect.top = static_cast<float>(pageHeight - y);
rect.bottom = static_cast<float>(pageHeight - (y + height));
FPDFAnnot_SetRect(targetAnnot, &rect);
}
// Update Color
if (data.contains("color")) {
std::string colorStr = data["color"].get<std::string>();
unsigned int r = 0, g = 0, b = 0;
parseHexColor(colorStr, r, g, b);
FPDFAnnot_SetColor(targetAnnot, FPDFANNOT_COLORTYPE_Color, r, g, b, 255);
}
// Update Thickness
if (data.contains("thickness")) {
float thickness = static_cast<float>(data["thickness"].get<double>());
FPDFAnnot_SetBorder(targetAnnot, 0.0f, 0.0f, thickness);
}
// Update Text
if (data.contains("text")) {
std::string text = data["text"].get<std::string>();
auto utf16 = utf8_to_utf16le(text);
FPDFAnnot_SetStringValue(targetAnnot, "Contents", reinterpret_cast<FPDF_WIDESTRING>(utf16.data()));
}
FPDFPage_CloseAnnot(targetAnnot);
} else {
spdlog::warn("update_annotation: annotation '{}' not found on page {}", targetId, pageIndex);
}
FPDF_ClosePage(page);
} else {
spdlog::warn("Unsupported edit operation type: {}", type);
}