Adding form XObjects with PDFPageModifier

195 views
Skip to first unread message

Miikka Miettinen

unread,
Apr 18, 2016, 3:45:38 AM4/18/16
to PDFHummus interest group
Hi,

Thanks for creating HummusJS! I'm trying to add a form XObject to an existing page of another PDF file. Following the PDFEmbedTest (https://github.com/galkahana/HummusJS/blob/master/tests/PDFEmbedTest.js), I'm able to do this when creating a new page, but modifying an existing page with PDFPageModifier doesn't seem to work the same way. This would be my preferred solution, if it worked:

var pdfWriter = hummus.createWriterToModify('template.pdf', {modifiedFilePath: 'doc.pdf'});
var formIDs = pdfWriter.createFormXObjectsFromPDF('graphics.pdf', hummus.ePDFPageBoxMediaBox);
var pageModifier = new hummus.PDFPageModifier(pdfWriter, 0);

pageModifier.startContext().getContext()
    .q()
    .cm(1, 0, 0, 1, 0, -130)
    .doXObject(pageModifier.getResourcesDictionary().addFormXObjectMapping(formIDs[0]))
    .Q();

pageModifier.endContext().writePage();
pdfWriter.end();

PDFPageModifier doesn't have the getResourcesDictionary method, however. Is there a simple way to do what I want or should I be looking at the much more complex sample code with streams and such?

Thanks,
Miikka

Gal Kahana

unread,
Apr 19, 2016, 2:21:56 AM4/19/16
to PDFHummus interest group
Hi,
right. that makes sense. :)
well the thing is that the "context" thats created in startContext is actually a form...so you can just its resource dictionary.

do this:

hummus.PDFPageModifier.prototype.getResourcesDictionary = function()
{
    return this.contextForm ? this.contextForm.getResourcesDictinary() : null;
};

now you got what you need.

Regards,
Gal.

Miikka Miettinen

unread,
Apr 21, 2016, 4:04:33 AM4/21/16
to PDFHummus interest group
Excellent! Thank you very much!

--Miikka

Adam Fleming

unread,
May 10, 2017, 2:09:57 AM5/10/17
to PDFHummus interest group
Hey Gal

Your library is so legit!

It looks like the PDFPageModifier is now in C and this little contextForm trick is no longer possible. 

Is there a new / better way to get an XObject reference when you are using createWriterToModify?

Many Thanks, 

Adam 

Gal Kahana

unread,
May 10, 2017, 2:58:14 AM5/10/17
to PDFHummus interest group
right :(
it's amazing, only yesterday i got to the same conclusion.
so one option, is to expose the form via the C item.
i'll put a little note to do that soon.
i mean...gotta have the resource dictionary.

in the meantime, you can workaround this by placing you own form using do, and then you can use your own form resource dictionary.
commented about this here:

  1. create a form xobject, where you can implement the drawing, and get the resoruces dictionary to get gsName. see here for details - https://github.com/galkahana/HummusJS/wiki/Reusable-forms.
    it's not written there but you can call myForm. getResourcesDictinary() to get the resources dictionary. then use myResDict. addExtGStateMapping() with the object id of the ExtGState that you created to get gsName

  2. place the form in the page using cm and do [see the form usage example in the link above to see details]

it would make sense that you will want to read the page dimensions to setup the form dimensions.
that's a good method if you got the pdfWriter modifier - 
function getPageDimensions(pdfWriter,pageIndex) {
    var pdfParser = pdfWriter.getModifiedFileParser();
    var pageInfo = pdfParser.parsePage(pageIndex);
    var pageMediaBox = pageInfo.getMediaBox();

    return {
        width:pageMediaBox[2]-pageMediaBox[0],
        height:pageMediaBox[3]-pageMediaBox[1]
    };
}

--
You received this message because you are subscribed to a topic in the Google Groups "PDFHummus interest group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pdfhummus-interest-group/dAY48PPx3dM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pdfhummus-interest...@googlegroups.com.
To post to this group, send email to pdfhummus-in...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pdfhummus-interest-group/80ec36c0-b9fe-4360-b58f-47c41953eb88%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages