1

I am trying to add javascript to my PDF file using iText7 library and C#

Currently, here is my code...which is by far not finish yet

public FileResult Download(string id)
    {
        var fileSelect = _context.FileStores.SingleOrDefault(c => c.File_Id == id);
        
        string base64string = Convert.ToBase64String(fileSelect.File_Content, 0, fileSelect.File_Content.Length);


        using (MemoryStream stream = new System.IO.MemoryStream())
        {

            MemoryStream memory = new MemoryStream(fileSelect.File_Content);
            BinaryReader BRreader = new BinaryReader(memory);
            StringBuilder text = new StringBuilder();


            PdfReader reader = new PdfReader(memory);
            //FileStream output = new FileStream(@"Manual.pdf", FileMode.Create);

            PdfDocument Pdfdoc = new PdfDocument(reader);
            Document doc = new Document(Pdfdoc);
            PdfAction action = PdfAction.CreateJavaScript("var rightNow = new Date(); " +
                                                          "var endDate = new Date('May 03, 2021 10:00:00');" +
                                                          "if(rightNow.getTime() > endDate){" +
                                                          "app.alert('This Document has expired, please contact us for a new one');" +
                                                          "this.closeDoc();}");
            reader.Close();

            return File(memory, "application/pdf", "ExportData.pdf");
        }

I want to add this javascript to my PDF and also download the file after it is finished adding the Javascript. Is there anybody that knows how to add Javascript to pdf? thanks

2
  • 1
    As an aside: You are aware that such a script is trivial to circumvent? Commented May 3, 2021 at 8:26
  • yes @mkl, I am trying to see if it's possible to implement it like acrobat Commented May 3, 2021 at 8:52

2 Answers 2

2

You can add the Javascript snippet as a document level OpenAction, to be executed when the document is opened:

PdfReader reader = new PdfReader("input.pdf");
PdfWriter writer = new PdfWriter("output.pdf");

PdfDocument Pdfdoc = new PdfDocument(reader, writer);
PdfAction action = PdfAction.CreateJavaScript(
    "var rightNow = new Date(); " +
    "var endDate = new Date('May 03, 2021 10:00:00');" +
    "if(rightNow.getTime() > endDate){" +
    "app.alert('This Document has expired, please contact us for a new one');" +
    "this.closeDoc();}"
);
Pdfdoc.getCatalog().SetOpenAction(action);
Pdfdoc.Close();

Javascript popup

Sign up to request clarification or add additional context in comments.

2 Comments

Hey man @rhens, thank alot! This does the job!... by any chance, do you know how to implement a download feature where the user can choose the path where he/she want to save the file?
I'm not sure downloading and saving, through Javascript, from within the PDF viewer, will be possible. You could open a URL and handle the file saving through the browsers: app.launchURL('https://URL/to/your/PDF'); Regardless, be prepared to run into security issues, possible depending on the configuration of the PDF viewer.
0

The following code worked for me. Multiple scripts can be added to the same document this way.

using var pdfReader = new PdfReader("input.pdf");
using var outputStream = new FileStream("output.pdf", FileMode.Create);
using var pdfWriter = new PdfWriter(outputStream);
using var outputPdfDocument = new PdfDocument(pdfReader, pdfWriter);

var outputJavaScriptNameTree = outputPdfDocument.GetCatalog().GetNameTree(PdfName.JavaScript);
outputJavaScriptNameTree.AddEntry(
    "Script1",
    PdfAction.CreateJavaScript("app.alert('Hello world!');").GetPdfObject());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.