Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PdfSharp - Javascript injected in PDF document do not work in Firefox

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!

like image 474
Nam Vũ Avatar asked Oct 19 '22 19:10

Nam Vũ


1 Answers

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);
like image 137
EliSherer Avatar answered Nov 03 '22 02:11

EliSherer