galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.15k stars 170 forks source link

Rotate page & write text afterwards #253

Open thbl opened 6 years ago

thbl commented 6 years ago

so @galkahana made a nice example on how to rotate a page, is it possible to write text after rotating, or do i need to "re open it" ?


`var hummus = require('hummus');

var pageIndexToChange = 0;
var desiredRotation = 90;

var pdfWriter = hummus.createWriterToModify(
                    __dirname + '/original.pdf',
                    {
                        modifiedFilePath:__dirname + '/result.pdf'
                    });

var copyingContext = pdfWriter.createPDFCopyingContextForModifiedFile();
var pageObjectID = copyingContext.getSourceDocumentParser().getPageObjectID(pageIndexToChange);
var pageJSObject = copyingContext.getSourceDocumentParser().parsePage(0).getDictionary().toJSObject();

// create a new version of the page object
var objectsContext = pdfWriter.getObjectsContext();
objectsContext.startModifiedIndirectObject(pageObjectID);
var modifiedPageObject = inPDFWriter.getObjectsContext().startDictionary();

// copy all but Rotate elements
Object.getOwnPropertyNames(thirdPageObject).forEach(function(element,index,array) {
    if (element != 'Rotate') {
        modifiedPageObject.writeKey(element);
        copyingContext.copyDirectObjectAsIs(thirdPageObject[element]);
    }
});

// setup new rotate and finish object
modifiedPageObject.writeKey('Rotate');
objectsContext
    .writeNumber(desiredRotation)
    .endLine()
    .endDictionary(modifiedPageObject)
    .endIndirectObject();

pdfWriter.end();`
galkahana commented 6 years ago

The HummusJS method for adding content on top of an existing page content via PDFPageModifier does not allow for much more than adding content. So rotating a page using the presented method is not possible together with it.

There are several options to approach a possible solution. One that you suggested is to create a rotated version and from that version create one with text. Another option is to port the modification code to JavaScript (shouldn't be too hard, it was originally that way), and then do both rotation and text addition in the same call.

thbl commented 6 years ago

Thanks @galkahana But i'm now considering just flipping the text with #167 My new problem is that when text is fliped i get it inverted, like "Hello Fanta" text I get "atnaF olleH" :/