Editing javascript in pdf format with .net

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

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

On my way? Is it possible?

I tried iTextSharp , but since the PDF contains form fields, the fields are lost when pdf is saved.

Any suggestions?

+4
source share
4 answers

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)

+6
source

Of course, the javascript of a PDF document can be edited.

I do not know how this can be done in other libraries, but Docotic.Pdf (I work for Bit Miracle), you can add / remove / edit javascript actions for controls, pages and the document as a whole.

Here is an example of setting and modifying a javascript action. The code assumes that the document contains 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"); 
+1
source

+1 on iTextSharp. As for the fields lost during saving, it is worth noting that Fox It Reader is free and allows you to save editable PDF files. If this is not an option, then usually it is Acrobat Standard in most companies. You can use any number of PDF print drivers and print the edited PDF as a PDF, although at the moment it will be read-only (still used in some cases).

+1
source

PDF4NET can load PDF files and gives you access to all JavaScript fragments, regardless of whether they are at the document level or in the field / level of annotations.

Disclaimer: I work for a company that develops PDF4NET.

0
source

Source: https://habr.com/ru/post/1341569/


All Articles