[英]print ReportViewer from custom button not toolbar
好的,我已經花了幾個小時(准確地說是4個)來尋找解決方案。 我發現了一些結果,但到目前為止沒有任何結果。 :(
問題:我在用戶控件中有一個reportviewer,並且隱藏了工具欄並創建了自己的工具。 現在,我添加了一個應該可以打印的按鈕,但似乎無法使其正常工作。 我將為您提供所有可能的解決方案。 但這必須是一個按鈕,報表中沒有默認欄。
這是我的代碼:
<rsweb:reportviewer
ID="rvReports"
runat="server"
Height="600px"
Width="600px"
ShowToolBar="False"
SizeToReportContent="True" AsyncRendering="false" />
<asp:ImageButton ID="btnprint" runat="server" ImageUrl="../img/print.png"
OnClientClick="PrintReport();" />
Javascript:
<script type="text/javascript">
function PrintReport() {
var viewerReference = $find("rvReports");
var reportArea = viewerReference.get_reportAreaContentType();
if (reportArea == Microsoft.Reporting.WebFormsClient.ReportAreaContent.ReportPage) {
$find("rvReports").invokePrintDialog();
}
}
</script>
我得到的錯誤是:
Uncaught TypeError: Cannot call method 'get_reportAreaContentType' of null
以防萬一我添加了一個Jquery庫,因為我認為是這樣,但是什么也沒做。.順便說一句,我從這里得到了javascript,因為它是另一個stackoverflow問題的答案之一。
從該錯誤看來, $find("rvReports")
將返回沒有控件的集合,結果get_reportAreaContentType報告了一個錯誤。 檢查腳本調試器中$find("rvReports")
調用的結果是什么,並查看視圖中是否存在所需的元素。
注意我認為選擇器應該是“ #rvReports” ...
我最終為此使用了ITextSharp,這非常簡單。 添加隱藏的iframe,並在此代碼的后面:
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = rvReports.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_LANDSCAPE, 0, 0, 0, 0);
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";
做到了,猜想只能繼續在google..lol內部進行挖掘
在$ find函數中使用clientId而不是serverId:
$find(<%=rvReports.ClientID%>)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.