How to move text in PDF?

484 Views Asked by At

Is there a Java or Nodejs library that can move existing text in a PDF file? I'd like to extract all the text nodes, then move some of them to a new location based on some conditions. I tried PdfClown, galkahana/HummusJS, Hopding/pdf-lib, but seems they don't have exactly what I need. can anyone help? thanks

1

There are 1 best solutions below

3
On

After inspecting the variables, I figured out how to move text, here is the code

PrimitiveComposer composer = new PrimitiveComposer(page);
ContentScanner scanner = composer.getScanner();
tranverse(scanner);
composer.flush();

...

while (level.moveNext()){
    ContentObject content = level.getCurrent();
    if (content instanceof Text){

...    
    List<ContentObject> objects = text.getBaseDataObject().getObjects();
    for(ContentObject co: objects){
        if(co instanceof SetTextMatrix){
            List<PdfDirectObject> operands = ((SetTextMatrix)co).getOperands();
            PdfInteger y = (PdfInteger)operands.get(5);
            operands.set(5, new PdfInteger(y.getIntValue()-100));
         }
     }