content completed
This commit is contained in:
@@ -101,31 +101,56 @@ public:
|
||||
pdfengine::Lexer lexer(stream->decodedContent);
|
||||
auto tokens = lexer.tokenize();
|
||||
pdfengine::ContentParser parser(tokens);
|
||||
pdfengine::ContentBuilder builder;
|
||||
|
||||
auto objects = builder.build(parser.parse());
|
||||
auto operations = parser.parse();
|
||||
|
||||
int textCount = 0;
|
||||
bool modified = false;
|
||||
for (auto& obj : objects) {
|
||||
if (obj->getType() == pdfengine::ContentObjectType::Text) {
|
||||
if (textCount == object_index) {
|
||||
auto* textObj = static_cast<pdfengine::TextObject*>(obj.get());
|
||||
textObj->text = new_text;
|
||||
modified = true;
|
||||
break;
|
||||
for (auto& op : operations) {
|
||||
if (op.op == "Tj" || op.op == "'") {
|
||||
if (op.operands.empty()) continue;
|
||||
auto& strNode = op.operands.back();
|
||||
if (strNode->type == pdfengine::AstNodeType::String || strNode->type == pdfengine::AstNodeType::HexString) {
|
||||
if (textCount == object_index) {
|
||||
strNode->type = pdfengine::AstNodeType::String;
|
||||
strNode->stringValue = new_text;
|
||||
modified = true;
|
||||
break;
|
||||
}
|
||||
textCount++;
|
||||
}
|
||||
} else if (op.op == "TJ") {
|
||||
if (op.operands.empty()) continue;
|
||||
auto& arrNode = op.operands.back();
|
||||
if (arrNode->type == pdfengine::AstNodeType::Array) {
|
||||
std::string combinedText;
|
||||
for (const auto& item : arrNode->arrayItems) {
|
||||
if (item->type == pdfengine::AstNodeType::String) {
|
||||
combinedText += item->stringValue;
|
||||
} else if (item->type == pdfengine::AstNodeType::HexString) {
|
||||
combinedText += std::string(item->bytesValue.begin(), item->bytesValue.end());
|
||||
} else if (item->type == pdfengine::AstNodeType::Number) {
|
||||
if (item->numberValue < -500.0) combinedText += " ";
|
||||
}
|
||||
}
|
||||
if (!combinedText.empty()) {
|
||||
if (textCount == object_index) {
|
||||
arrNode->arrayItems.clear();
|
||||
auto newStrNode = std::make_shared<pdfengine::AstNode>(pdfengine::AstNodeType::String);
|
||||
newStrNode->stringValue = new_text;
|
||||
arrNode->arrayItems.push_back(std::move(newStrNode));
|
||||
modified = true;
|
||||
break;
|
||||
}
|
||||
textCount++;
|
||||
}
|
||||
}
|
||||
textCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!modified) return false;
|
||||
|
||||
pdfengine::ContentSerializer cSerializer;
|
||||
auto newOps = cSerializer.serialize(objects);
|
||||
|
||||
pdfengine::AstSerializer astSerializer;
|
||||
std::string newRawStream = astSerializer.serialize(newOps);
|
||||
std::string newRawStream = astSerializer.serialize(operations);
|
||||
|
||||
pdfengine::qpdf_layer::QpdfWriter writer;
|
||||
auto res = writer.replacePageStreamAndSave(filepath_, dest_path, page_index, newRawStream);
|
||||
|
||||
Reference in New Issue
Block a user