Answered by:
Print button outside the report viewer

Question
-
User1093090287 posted
Hi everyone,
I would like to have simple print asp:button outside the report viewer instead the embedded one. Is it possible to be done?
Thanks in advance
Wednesday, April 25, 2012 5:39 AM
Answers
-
User-37275327 posted
This is possible. I have achieved this. You need to get support of thirdparty ddl called iTextShart. Just google and download. You need to export the report pdf. After applying the code, clicking button will raise printer dialogbox on browser and print directly to the client printer.
Add iframe to the markup
<iframe id="frmPrint" name="IframeName" width="500" height="200" runat="server" style="display: none" runat="server"></iframe>
Refer followings
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;On the button click
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output.pdf"), FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
//Open exsisting pdf
Document document = new Document(PageSize.LETTER);
PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output.pdf"));
//Getting a instance of new pdf wrtiter
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
int i = 0;
int p = 0;
int n = reader.NumberOfPages;
Rectangle psize = reader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
//Add Page to new document
while (i < n)
{
document.NewPage();
p++;
i++;
PdfImportedPage page1 = writer.GetImportedPage(reader, i);
cb.AddTemplate(page1, 0, 0);
}
//Attach javascript to the document
PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
writer.AddJavaScript(jAction);
document.Close();
//Attach pdf to the iframe
frmPrint.Attributes["src"] = "Print.pdf";Thanks
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 26, 2012 12:33 AM
All replies
-
User2105670541 posted
I dont think that is possible.
Wednesday, April 25, 2012 6:54 AM -
User1093090287 posted
not even some kind of workaround?
Wednesday, April 25, 2012 6:55 AM -
User2105670541 posted
yes work arround is possible, visit these threads:
Wednesday, April 25, 2012 6:58 AM -
User1093090287 posted
thanks, but it's not what i was looking for. i need a code to print the report but the button to be a simple asp:button, not the button that is inside the report.
Wednesday, April 25, 2012 7:05 AM -
User2105670541 posted
do you need something like below:
http://blogs.msdn.com/b/bryanke/archive/2004/02/11/71491.aspx
Wednesday, April 25, 2012 7:19 AM -
User1093090287 posted
this is strictly for local or network known printer. my problem is addressed within web application.
thanks anyway
Wednesday, April 25, 2012 8:16 AM -
User-37275327 posted
This is possible. I have achieved this. You need to get support of thirdparty ddl called iTextShart. Just google and download. You need to export the report pdf. After applying the code, clicking button will raise printer dialogbox on browser and print directly to the client printer.
Add iframe to the markup
<iframe id="frmPrint" name="IframeName" width="500" height="200" runat="server" style="display: none" runat="server"></iframe>
Refer followings
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;On the button click
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output.pdf"), FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
//Open exsisting pdf
Document document = new Document(PageSize.LETTER);
PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output.pdf"));
//Getting a instance of new pdf wrtiter
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
int i = 0;
int p = 0;
int n = reader.NumberOfPages;
Rectangle psize = reader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
//Add Page to new document
while (i < n)
{
document.NewPage();
p++;
i++;
PdfImportedPage page1 = writer.GetImportedPage(reader, i);
cb.AddTemplate(page1, 0, 0);
}
//Attach javascript to the document
PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
writer.AddJavaScript(jAction);
document.Close();
//Attach pdf to the iframe
frmPrint.Attributes["src"] = "Print.pdf";Thanks
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 26, 2012 12:33 AM -
User1093090287 posted
It works :). Thank you very very much cnranasinghe.
Thursday, April 26, 2012 4:00 AM -
User-37275327 posted
Thursday, April 26, 2012 5:45 AM