Q:
How can I update/correct all URL (link annotations) in a PDF document?
I am developing using C++.
----
A:
You could try using the following code:
#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/Annot.h>
#include <SDF/ObjHierarchy.h>
#include <iostream>
using namespace pdftron;
using namespace SDF;
using namespace PDF;
using namespace std;
void ProcessAnnotations(PDFDoc& doc) {
// traverse all annotations in the document
PageIterator end = doc.PageEnd();
for (PageIterator itr = doc.PageBegin(); itr!=end; ++itr) {
Page page = *itr;
int num_annots = page.GetNumAnnots();
for (int i=0; i<num_annots; ++i) {
Annot annot = page.GetAnnot(i);
Rect bbox = annot.GetRect();
if (annot.GetType() == Annot::e_Link) {
Action action = annot.GetLinkAction();
if (action.GetType() == Action::e_URI) {
Obj* action_dict = action.GetSDFObj();
string str(action_dict->Get("URI")->second->GetStr());
string::size_type pos = str.find("app.launchURL");
if (pos != -1) // the string contains launchURL string
{
pos = str.find("http://", pos);
if (pos != -1) {
string::size_type pos2 = str.find_first_of("\"'", pos);
if (pos != -1) {
string hyperlink = str.substr(pos, pos2-pos);
action_dict->Put("URI", new Str(hyperlink));
}
}
}
}
}
}
}
int main(int argc, char *argv[]) {
if (argc < 3) {
cout << "Usage: fixlinks in.pdf out.pdf" << endl;
return 1;
}
int ret = 0;
PDFNet::Initialize();
try {
PDFDoc doc(argv[1]);
doc.InitSecurityHandler();
ProcessAnnotations(doc);
doc.Save(argv[2], Doc::e_linearized, 0);
}
catch(Common::Exception& e) {
cout << e << endl;
ret = 1;
}
catch(...) {
cout << "Unknown Exception" << endl;
ret = 1;
}
PDFNet::Terminate();
return ret;
}