C# 无需预览即可从 ASP.Net 打印 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/270674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print PDF from ASP.Net without preview
提问by Nelson Miranda
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.
我已经使用 iTextSharp 生成了一个 pdf,我可以在 ASP.Net 中很好地预览它,但我需要将它直接发送到打印机而无需预览。我希望用户单击打印按钮并自动打印文档。
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.
我知道可以使用 javascript window.print() 将页面直接发送到打印机,但我不知道如何将其制作为 PDF。
Edit: 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");
...
And here I am.
我在这里。
回答by Stefan
Is the pdf embedded in the page with embedd-tag or just opened in a frame or how are you showing it?
pdf 是使用 embedd-tag 嵌入到页面中还是只是在框架中打开,或者您如何显示它?
If its embedded, just make sure that the object is selected and then do a print().
如果是嵌入的,只需确保选择了对象,然后执行 print()。
Get the ref to the embedded document.
获取嵌入文档的引用。
var x = document.getElementById("mypdfembeddobject");
x.click();
x.setActive();
x.focus();
x.print();
回答by Stefan
ALso, try this gem:
另外,试试这个宝石:
<link ref="mypdf" media="print" href="mypdf.pdf">
I havent tested it, but what I have read about it, it can be used in this way to let the mypdf.pdf be printed instead of page content whatever method you are using to print the page.
我还没有测试过它,但是我所读到的关于它的内容,可以通过这种方式使用它来打印 mypdf.pdf 而不是页面内容,无论您使用什么方法打印页面。
Search for media="print" to check out more.
搜索 media="print" 以查看更多信息。
回答by Stefan
You can embed javascript in the pdf, so that the user gets a print dialog as soon as their browser loads the pdf.
您可以在 pdf 中嵌入 javascript,以便用户在浏览器加载 pdf 后立即获得打印对话框。
I'm not sure about iTextSharp, but the javascript that I use is
我不确定 iTextSharp,但我使用的 javascript 是
var pp = this.getPrintParams();
pp.interactive = pp.constants.interactionLevel.automatic;
this.print(pp);
For iTextSharp, check out http://itextsharp.sourceforge.net/examples/Chap1106.cs
对于 iTextSharp,请查看http://itextsharp.sourceforge.net/examples/Chap1106.cs
回答by Nelson Miranda
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;
最后我做到了,但我不得不使用 IFRAME,我在 aspx 中定义了一个 IFrame 并且没有设置 src 属性,在我生成的 cs 文件中生成了 pdf 文件并将 iFrame 的 src 属性设置为生成的pdf文件名,像这样;
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);\r", 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";
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.
这就是诀窍,但是,我认为我应该实施您的解决方案 Stefan,问题是我是 asp.net 和 javascript 的新手,如果我没有完整的源代码,我无法编写您的建议,但是至少是第一步,我很惊讶我需要学习多少 html 和 javascript 代码。谢谢。
回答by frenchone
It's a little more tricky if you're using pdfsharp but quite doable
如果您使用 pdfsharp 会有点棘手但很可行
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
// real stuff starts here
// current version of pdfsharp doesn't support actions
// http://www.pdfsharp.net/wiki/WorkOnPdfObjects-sample.ashx
// so we got to get close to the metal see chapter 12.6.4 of
// http://partners.adobe.com/public/developer/pdf/index_reference.html
PdfDictionary dict = new PdfDictionary(document); //
dict.Elements["/S"] = new PdfName("/JavaScript"); //
dict.Elements["/JS"] = new PdfString("this.print(true);\r");
document.Internals.AddObject(dict);
document.Internals.Catalog.Elements["/OpenAction"] =
PdfInternals.GetReference(dict);
document.Save(Server.MapPath("2.pdf"));
frame1.Attributes["src"] = "2.pdf";