1 不调用itextsharp.dll的操作
///
/// 生成pdf流
///
///
private void EditPDF(string fpath)
{
string path = fpath.Replace("\\", "/");
FileStream MyFileStream = new FileStream(path, FileMode.Open);
ViewPdf(MyFileStream);
}
///
/// 显示pdf
///
///
private void ViewPdf(Stream fs)
{
byte[] buffer = new byte[fs.Length];
fs.Position = 0;
fs.Read(buffer, 0, (int)fs.Length);
Response.Clear();
Response.AddHeader("Content-Length", fs.Length.ToString());
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline;FileName=out.pdf");
fs.Close();
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
2 引用itextsharp.dll操作的,此操作可以对显示PDF进行打开即弹出打印操作及设置是否显示工具栏,打印按钮等.
private void EditPDF(string fpath)
{
string path = fpath.Replace("\\","/");
PdfReader reader = new PdfReader(path);
MemoryStream ms = new MemoryStream();
PdfStamper stamper = new PdfStamper(reader, ms);
stamper.Writer.ViewerPreferences = PdfWriter.HideWindowUI;
stamper.Writer.SetEncryption(PdfWriter.STRENGTH128BITS, null, null, PdfWriter.AllowPrinting | PdfWriter.AllowFillIn);
stamper.Writer.CloseStream = false;
//直接弹出打印不用点击打印按钮
//PdfAction.JavaScript("myOnMessage();", stamper.Writer);
//stamper.Writer.AddJavaScript("this.print(true);function myOnMessage(aMessage) {app.alert('Test',2);} var msgHandlerObject = new Object();doc.onWillPrint = myOnMessage;this.hostContainer.messageHandler = msgHandlerObject;");
//StringBuilder script = new StringBuilder();
//script.Append("this.print({bUI: false,bSilent: true,bShrinkToFit: true});").Append("\r\nthis.closeDoc();");
//script.Append("var pp = this.getPrintParams();pp.printerName = '\\\\fpserver\\hp LaserJet 1010'; this.print(pp);");
//script.Append("this.print(flase);");
//stamper.Writer.AddJavaScript(script.ToString(),false);
//PdfContentByte cb = stamper.GetOverContent(1);
//cb.Circle(250, 250, 50);
//cb.SetColorFill(iTextSharp.text.Color.RED);
//cb.SetColorStroke(iTextSharp.text.Color.WHITE);
//cb.FillStroke();
stamper.Close();
ViewPdf(ms);
}
private void ViewPdf(Stream fs)
{
byte[] buffer = new byte[fs.Length];
fs.Position = 0;
fs.Read(buffer, 0, (int)fs.Length);
Response.Clear();
//Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");
Response.AddHeader("Content-Length", fs.Length.ToString());
//Response.AddHeader("Content-Disposition", "inline;FileName=out.pdf");
Response.ContentType = "application/pdf";
fs.Close();
Response.BinaryWrite(buffer);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
3 web页面显示方式
可以直接输出显示在当前页也可以在另外一个页面中引用下面显示
test.aspx表示直接输出显示的页面,
如果直接输出显示的话不好控制页面布局,在另外一个新页中显示的话容易控制页面布局.