PdfSharp - Javascript injected in PDF document do not work in Firefox
Asked Answered
K

1

6

I have this code for printing the PDF Document from file stream (using PdfSharp Library):

private HttpResponseMessage PrintPdfDocument2(MemoryStream fileStream)
    {
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);


        PdfSharp.Pdf.PdfDocument document = PdfSharp.Pdf.IO.PdfReader.Open(fileStream);
        PdfSharp.Pdf.PdfDictionary dict = new PdfSharp.Pdf.PdfDictionary(document);

        dict.Elements["/S"] = new PdfSharp.Pdf.PdfName("/JavaScript");
        dict.Elements["/JS"] = new PdfSharp.Pdf.PdfString("this.print(true);\r");

        document.Internals.AddObject(dict);
        document.Internals.Catalog.Elements["/OpenAction"] = PdfSharp.Pdf.Advanced.PdfInternals.GetReference(dict);

        var outputStream = new MemoryStream();
        document.Save(outputStream);
        result.Content = new ByteArrayContent(outputStream.ToArray());
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

        return result;
    }

It work fine in chrome and i.e but not work in Firefox

Any idea for this problem????

Thanks u guys for reading!

Kalin answered 22/12, 2015 at 7:56 Comment(5)
Most likely the PDF viewing component in Firefox is too dumb to understand JavaScript. What happens if you use Acrobat/Reader as helper for displaying PDF via their browser plug-in?Disuse
Because this is real project for client, so we assume that we don't have any plug-in to support open pdf document in browser.Uzial
so we assume that we don't have any plug-in to support open pdf document in browser - in that case you also have to assume that not all special features of your PDF will be available to all recipients of your PDFs.Mnemosyne
The mentioned browser plug-in gets installed (more or less) automatically when the user installs Acrobat/Reader. However, you can not count on it (it can be deactivated, or chosen to not be installed). That also means that you would have to analyze which functionality is essential for you, and then find out which PDF viewers used by your users do support it. …and don't forget PDF viewers on mobile devices…Disuse
A workaround would be to force a download, and then have the user open the document (in Acrobat/Reader) locally.Disuse
C
3

I found s solution (by looking at pdf.js source code)

var dict = new PdfDictionary(document);
dict.Elements["/Type"] = new PdfName("/Action");
dict.Elements["/S"] = new PdfName("/Named");
dict.Elements["/N"] = new PdfName("/Print");
document.Internals.AddObject(dict);
document.Internals.Catalog.Elements["/OpenAction"] = PdfSharp.Pdf.Advanced.PdfInternals.GetReference(dict);
Catlin answered 17/6, 2017 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.