I'm trying to write a console script to replace some placeholder text in 
a pdf (eg. XYZ) with a variable generated by web script supplied to the 
command line.

I can go through and read properties of the pdf, but I can't seem to 
change any text.  I don't know if I need to add operators to the stream 
or what, none of my experiments have worked.  All I can do is get the 
stream text and modify the variable:

text = stream.getText();
text = text.replace("XYZXYZXYZ", ycode);
print("textnow: "+text);

Is it possible to do such a search replace from a console script?

Any help is much appreciated.


Here's my experimental code, it's just a mess, but at least you can see 
what I've been up to if you want.:

// Console: addycode

function addycode_help() {
  print(tr("Usage:"));
  print("addycode ["+tr("input file")+"] ["+tr("text")+"]");
  print(" "+tr("Input file must exist"));
// print(" "+tr("Output file must not exist"));
  exit(1);
}

function addycode_fail(err) {
  print(tr("Adding code failed!"));
  print(err);
  exit(2);
}




p=parameters();
if (p.length!=2) {
  addycode_help("addycode "+tr("is expecting 2 parameters"))
}
inFile=p[0];
ycode=p[1];
if (!exists(inFile)) addycode_fail(tr("Input file '%1' does not 
exist").arg(inFile));
//if (exists(outFile)) addycode_fail(tr("Output file '%1' already 
exist").arg(outFile));
//if (inFile==outFile) delinearize_fail(tr("Input and output files must 
be different"));

if (1) {
// print(tr("added code.")+" :"+inFile+" -> "+text);
outFile = "/home/chris/pdf/outFile.pdf";
delinearize(inFile, outFile);
print("done delin");
pdf = loadPdf(inFile, true);
page = pdf.getPage(1);

stream_count = page.getContentStreamCount();
print("numstreams: "+stream_count);
if (!stream_count) {
print("no count");
exit(1);
}

//dict = page.getDictionary();
//print("dictionary: "+dict.getText());

//print("TEXT: "+page.getText());


for (i=0; i< stream_count; i++){
stream = page.getContentStream(i);
print("it: "+stream.getText());
//print("decstream: " + stream.getDecoded());

text = stream.getText();
text = text.replace("XYZXYZXYZ", ycode);
print("textnow: "+text);

//stream.insertOperator();


//op = stream.getFirstOperator();
//iter = op.textIterator();
//while (op){
//  print("op : "+op.());
//  op = iter.next();
//}

}

//print "numstreams: "+stream_count;
//print "stream: "+stream;

pdf.unloadPdf();
} else {
  addycode_fail(error());
}

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Pdfedit-support mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pdfedit-support

Reply via email to