无需预览即可从 ASP.Net 打印 PDF [英] Print PDF from ASP.Net without preview

查看:33
本文介绍了无需预览即可从 ASP.Net 打印 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 iTextSharp 生成了一个 pdf,我可以在 ASP.Net 中很好地预览它,但我需要将它直接发送到打印机而无需预览.我希望用户单击打印按钮并自动打印文档.

I've generated a pdf using iTextSharp and I can preview it very well in ASP.Net but I need to send it directly to printer without a preview. I want the user to click the print button and automatically the document prints.

我知道可以使用 javascript window.print() 将页面直接发送到打印机,但我不知道如何将其制作为 PDF.

I know that a page can be sent directly to printer using the javascript window.print() but I don't know how to make it for a PDF.

它不是嵌入的,我是这样生成的;

it is not embedded, I generate it like this;

                ...
                FileStream stream = new FileStream(Request.PhysicalApplicationPath + "~1.pdf", FileMode.Create);
                Document pdf = new Document(PageSize.LETTER);
                PdfWriter writer = PdfWriter.GetInstance(pdf, stream);
                pdf.Open();
                pdf.Add(new Paragraph(member.ToString()));
                pdf.Close();

                Response.Redirect("~1.pdf");
                ...

我在这里.

推荐答案

终于搞定了,但是我不得不使用一个 IFRAME,我在 aspx 中定义了一个 IFrame 并没有设置 src 属性,在 cs 文件中我生成了pdf文件并将iFrame的src属性设置为生成的pdf文件名,如下所示;

Finally I made it, but I had to use an IFRAME, I defined an IFrame in the aspx and didn't set the src property, in the cs file I made generated the pdf file and set the src property of the iFrame as the generated pdf file name, like this;

Document pdf = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(pdf, 
new FileStream(Request.PhysicalApplicationPath + "~1.pdf", FileMode.Create));
pdf.Open();

//This action leads directly to printer dialogue
PdfAction jAction = PdfAction.JavaScript("this.print(true);
", writer);
writer.AddJavaScript(jAction);

pdf.Add(new Paragraph("My first PDF on line"));
pdf.Close();

//Open the pdf in the frame
frame1.Attributes["src"] = "~1.pdf";

这就是诀窍,但是,我认为我应该实施您的解决方案 Stefan,问题是我是 asp.net 和 javascript 的新手,如果我没有完整的源代码,我将无法编码你的建议但至少是第一步,我很惊讶我需要学习多少 html 和 javascript 代码.谢谢.

And that made the trick, however, I think that i should implement your solution Stefan, the problem is that I'm new to asp.net and javascript and if I don't have a complete source code I could not code your suggestion but at least is the first step, I was very surprised how much code in html and javascript i need to learn. Thnx.

这篇关于无需预览即可从 ASP.Net 打印 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆