Christian Payne
Christian Payne

Reputation: 7169

Editing javascript in a pdf with .net

Is it possible to edit the javascript of a pdf document with .net?

I've looked at the Acrobat SDK, but without much luck. It looks like you can retrieve values from forms etc. but not edit the document.

Am I way off track? Is this even possible?

I've tried iTextSharp, but since the pdf contains form fields, the fields are lost when I save the pdf.

Any suggestions?

Upvotes: 4

Views: 3516

Answers (4)

Bobrovsky
Bobrovsky

Reputation: 14246

Sure, javascript of a PDF document can be edited.

I don't know how it can be done in other libraries but with Docotic.Pdf (I work for Bit Miracle) you can add/remove/edit javascript actions for controls, pages and document as a whole.

Here is a sample for setting and changing javascript action. The code assumes that document contain a button.

PdfDocument doc = new PdfDocument("document-with-button.pdf");
// assume that first widget is a button
PdfButton button = doc.Widgets[0] as PdfButton; 

// add javascript action for OnMouseEnter event
PdfJavaScriptAction action = doc.CreateJavaScriptAction("app.alert('OnMouseEnter JavaScript Action',3)");
button.OnMouseEnter = action;

// change javascript action for OnMouseEnter event
(button.OnMouseEnter as PdfJavaScriptAction).Script = "app.alert('Well, hello from OnMouseEnter JavaScript Action',3)";

doc.Save("output.pdf");

Upvotes: 1

Geoffrey Hudik
Geoffrey Hudik

Reputation: 546

+1 on iTextSharp. As to the fields being lost on save, it is worth noting that Fox It Reader is free and allows saving editable PDFs. If that isn't an option then it is usually Acrobat Standard at most companies. You could use any number of PDF print drivers and print the edited PDF form to PDF though it will be readonly at that point (still of some use in cases).

Upvotes: 1

Christian Payne
Christian Payne

Reputation: 7169

Well, apparently you can with iTextSharp:

        Document document = new Document(PageSize.A4, 50, 50, 50, 50);

        PdfReader reader = new PdfReader(@"Source.pdf");
        FileStream output = new FileStream(@"Destination.pdf", FileMode.Create);

        PdfStamper pdfStamper = new PdfStamper(reader, output, '\0', true);               
        pdfStamper.JavaScript = "app.alert(\"Hello world!\");";

        pdfStamper.FormFlattening = false;
        pdfStamper.Close();
        reader.Close();

(This question helped)

Upvotes: 6

iPDFdev
iPDFdev

Reputation: 5834

PDF4NET can load PDF files and it gives you access to all JavaScript fragments, whether they are located at document level or at field/annotation actions level.

Disclaimer: I work for the company that develops PDF4NET.

Upvotes: 0

Related Questions