Editing javascript in a pdf with .net

ⅰ亾dé卋堺 提交于 2019-12-10 16:41:41

问题


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?


回答1:


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)




回答2:


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



回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/5137222/editing-javascript-in-a-pdf-with-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!