Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is it possible to replace text in the (mediabox) of the pdf? #445

Open
andresRiosC opened this issue Nov 26, 2019 · 0 comments
Open

Comments

@andresRiosC
Copy link

andresRiosC commented Nov 26, 2019

Hi,

I'm trying to replace text from the attached pdf, which has the content in (mediabox).

I am running the following script on the attached file.

SCRIPT

var hummus = require('hummus');

function strToByteArray(str) {
  var myBuffer = [];
  var buffer = new Buffer.from(str);
  for (var i = 0; i < buffer.length; i++) {
      myBuffer.push(buffer[i]);
  }
  return myBuffer;
}

function replaceText(sourceFile, targetFile, pageNumber, findText, replaceText) {  
    var writer = hummus.createWriterToModify(sourceFile, {
        modifiedFilePath: targetFile
    });
    var modifier = new hummus.PDFPageModifier(writer, pageNumber);
    var sourceParser = writer.createPDFCopyingContextForModifiedFile().getSourceDocumentParser();
    var pageObject = sourceParser.parsePage(pageNumber);
    var textObjectId = pageObject.getDictionary().toJSObject().Contents.getObjectID();
    var textStream = sourceParser.queryDictionaryObject(pageObject.getDictionary(), 'Contents');
    var data = [];
    var readStream = sourceParser.startReadingFromStream(textStream);
    while(readStream.notEnded()){
        Array.prototype.push.apply(data, readStream.read(10000));
    }
    var string = new Buffer.from(data).toString().replace(findText, replaceText);

   var objectsContext = writer.getObjectsContext();
    objectsContext.startModifiedIndirectObject(textObjectId);
    var stream = objectsContext.startUnfilteredPDFStream();
    stream.getWriteStream().write(strToByteArray(string));
    objectsContext.endPDFStream(stream);
    objectsContext.endIndirectObject();
    writer.end();
}

replaceText('./senapliegos.pdf', './output.pdf', 0, /(SENA)/g, 'SERVICIO NACIONAL');

senapliegos.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant