防止網頁上的 Crystal Report PDF被另存

要在網頁顯示報表,最相容的方式就是直接輸出PDF,一般都是用Crystal Report製作報表,輸出成PDF到網頁前端讓User列印,但原本的寫法會有PDF的工具列,會讓人另存PDF複本。

如果不考慮有本機權限的User另外安裝可輸出成PDF檔的虛擬印表機或copy圖軟體,以下是簡單的防copy作法:

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
 <%@ Page Language="C#" %>
  
<script runat="server">

    string htm = string.Format(@
"
    <object type='application/pdf' width='0' height='0' id='PrintPDF' name='PrintPDF' >
        
<param name='src' value='GenPDF.aspx?parm=xxx'/>
        
<param name='wmode' value='transparent'> 
    
</object>
    
<br />
    
<iframe id='ViewPDF' SRC='GenPDF.aspx?parm=xxx#toolbar=0&navpanes=0&scrollbar=0' style='z-index=1;position:relative;' width='750' height='490'>
    
</iframe>
    
", "");
    reportContent.InnerHtml = htm;

</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head></head>
<body>
 
<form id="form1" runat="server">
 
<input type="button" value=" 列 印" style="width:80px;height:25px;" onclick="PrintPDF.printWithDialog();" class='clsbtn' />       
 
<div id="reportContent" runat="server">
 
</div>
 
</form>
 
</body>
</html>

隱藏的 object 用來讓 button 可以用 onclick="PrintPDF.printWithDialog();" trigger 預覽PDF的視窗
iframe 用來顯示 PDF 在頁面上

PDF 的來源可以是已存在的 filename.pdf 或一個輸出資料流的 C# 程式

Crystal Report 輸出 PDF 寫法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
DiskFileDestinationOptions objFile = new DiskFileDestinationOptions();
string strPathFileName = Server.MapPath("./") + "Data" + Guid.NewGuid().ToString() + ".pdf";
objFile.DiskFileName = strPathFileName;
RptDoc.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
RptDoc.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
RptDoc.ExportOptions.DestinationOptions = objFile;
RptDoc.Export();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = 
"application/pdf";
byte[] cachePDF = System.IO.File.ReadAllBytes(strPathFileName);
File.Delete(strPathFileName);
Response.BinaryWrite(cachePDF);
Response.End();
File.Delete(strPathFileName);

 

產出PDF後可存到Cache,這樣User在第二次執行後就比較快了

 

posted @ 2013-06-10 15:20  Jimmych  阅读(560)  评论(0)    收藏  举报