我有以下代码用于从文件流中打印PDF文档(使用PdfSharp库):
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;
}
它在chrome和chrome中都可以正常工作,但在Firefox中却不能正常工作
任何想法这个问题???
谢谢你们的阅读!
最佳答案
我找到了解决方案(通过查看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);