开发者

Editing javascript in a pdf with .net

开发者 https://www.devze.com 2023-02-14 06:48 出处:网络
Is it possible to edit the javascript of a pdf document with .net? I\'ve looked at the Acrobat S开发者_Python百科DK, but without much luck.It looks like you can retrieve values from forms etc. but no

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

I've looked at the Acrobat S开发者_Python百科DK, 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?


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)


+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).


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");


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消