打印按钮在调试模式下工作正常。当实施到服务器时,它会继续打印第一个文档。
使用新的打印页面生成PDF文件(如果在服务器文件夹中手动检查PDF)但是iframe继续打印旧页面。它不需要从服务器获取新的PDF。
有人可以帮助我吗?我是ASP.net的新手
a.aspx
<form id="Form1" runat="server">
<asp:Label ID="lbl" runat="server" Width="334px"></asp:Label>
<asp:Button ID="btnPrint_Click" runat="server" Height="20px" Text="Print From Here" Width="183px" OnClick="btnPrint_Click_Click" ForeColor="Red" />
<div title="print report">
<rsweb:ReportViewer ID="rvViewer" runat="server" ShowParameterPrompts="false" AsyncRendering="true"
Width="100%" BackColor="#F7F6F3" ShowRefreshButton="true" Height="800px" ToolTip="Click the arrow to Sort" >
</rsweb:ReportViewer>
</div>
</form>
<iframe id="frmPrint" name="IframeName" width="500"
height="200" runat="server"
style="display: none">
</iframe>
a.aspx.cs
protected void btnPrint_Click_Click(object sender, EventArgs e)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = rvViewer.ServerReport.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.CreateNew));
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";
}