原文 itextsharp生成pdf后的直接打印问题

小弟这两天用itextsharp生成pdf文档,生成的pdf可以直接保存在指定路径的文件夹下,可是user不想保存,想要点一下button,就可以直接打印,我搞了半天也没找到合适的方法,还希望有做过这方面的,给些建议.多谢多谢啦
我的代码是这样的:  iTextSharp.text.Rectangle page = new Rectangle(683,850);
 iTextSharp.text.Document document = new iTextSharp.text.Document(page, 71, 43, 60, 28);
            try
            {
                // and directs a PDF-stream to a file
                PdfWriter.GetInstance(document, new FileStream("C:\\" + DateTime.Now.ToString("yyyyMMdd") +
 ".pdf", FileMode.Create));
                document.Open();
        //我增加的文档内容部分
            }
            catch (Exception e)
            {
            }
            // close the document
            document.Close();
        }

解决方法:

我把我的解决办法也贴出来吧,
1,把生成PDF文档到MemoryStream.
MemoryStream pdfdata = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, pdfdata);
2,在PdfWriter中加javescript.

StringBuilder script = new StringBuilder();
                script.Append("this.print({bUI: true,bSilent: true,bShrinkToFit: false});");

writer.ViewerPreferences = PdfWriter.HideMenubar | PdfWriter.HideToolbar | PdfWriter.HideWindowUI;
                writer.AddJavaScript(script.ToString());
3,输出到客户端缓存.

protected void submit_Click(object sender, EventArgs e)
        {
            MemoryStream PDFData =1中的返回值;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.Charset = string.Empty;
            Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
            Response.AddHeader("Content-Disposition",
            "inline; filename=" + Title.Replace(" ", "").Replace(":", "-") + ".pdf");

Response.OutputStream.Write(PDFData.GetBuffer(), 0, PDFData.GetBuffer().Length);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
        }
4,我在页面上放一个iframe(width=0,height=0),就是让他不可见,让pdf文档在他下面打开,就会执行打印动作.
 <form id="form1" runat="server" target="iframe1">
        <div><center>        
        <br />
        <asp:Button ID="submit" runat="server" OnClick="submit_Click" Text="Print PO" />
         </center></div>
    </form>
    <iframe name="iframe1" width="0" height="0">      
    </iframe>
希望对想和我实现一样功能的朋友有些参考价值.

itextsharp生成pdf后的直接打印问题的更多相关文章

  1. iTextSharp生成PDF文件

    这是一篇简单的教程,所以只涉及一些iTextSharp生成pdf的简单应用,详细教程请搜索iTextSharp进入官网看官方文档(英文版). iTextSharp官方文档:https://itextp ...

  2. iTextSharp生成pdf的一个简单例子

    效果图: 参考:http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html http://www.cnblogs.com/julyl ...

  3. C#使用itextsharp生成PDF文件

    项目需求需要生成一个PDF文档,使用的是VS2010,ASP.NET. 网络上多次搜索没有自己想要的,于是硬着头皮到itextpdf官网看英文文档,按时完成任务,以实用为主,共享一下: 使用HTML文 ...

  4. iTextSharp生成pdf含模板(二)---C#代码部分

    参考地址:https://www.cnblogs.com/ibeisha/p/itextsharp-pdf.html 一.先在程序中使用Nuget安装iTextSharp(我是创建的控制台程序) 二. ...

  5. iTextSharp生成pdf文档案例

    1.using iTextSharp.text;using iTextSharp.text.pdf; 2.设置页面大小 iTextSharp.text.Rectangle pageSize = new ...

  6. itextsharp生成pdf

    itextsharp在ios中可用,亲测 (一)生成文档 Document document = , , , ), , , , ); //Document document = new Documen ...

  7. .Net iTextSharp 生成pdf

    拿别人例子 public ActionResult index() { var ms = new MemoryStream(); #region CreatePDF Document document ...

  8. 利用ItextSharp 生成PDF文档改进版

    导入的ItextSharp.dll一定要是较高的版本 数据库表结构 生成的PDF样式 代码: namespace WebPDF { public partial class _Default : Sy ...

  9. iTextSharp 合并PDF后,无法删除已经合并的单个文件

    private void MergePDFFiles(string[] fileList, string outMergeFile) { List<PdfReader> readerLis ...

随机推荐

  1. HTML+CSS基础总结

    1. 首先声明正确的文档类型doctype 通常有四种文档类型可供选择: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ...

  2. IOS 指纹识别的简单使用

    首先导入LocalAuthentication框架 然后导入头文件 #import <LocalAuthentication/LAPublicDefines.h> - (void)begi ...

  3. LeetCode:链表排序

    Sort a linked list in O(n log n) time using constant space complexity. public class Solution { publi ...

  4. codeforces 559A(Gerald's Hexagon)

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Gera ...

  5. float与position

    使用float会使块级元素的宽高表现为包裹内容(在不设定宽高的情况下)  这是当然的  我们使用float就是使几个div排在一行 当然不可能在宽度上撑满父元素啦  至于高度 不论有没有float 高 ...

  6. HTTPS的学习总结

    HTTPS学习总结 简述 HTTPS对比HTTP就多了一个安全层SSL/TLS,具体就是验证服务端的证书和对内容进行加密. 先来看看HTTP和HTTPS的区别 我用AFN访问http下的httpbin ...

  7. cocos2d-x游戏开发系列教程-超级玛丽06-CMGameScene

    背景 在CMMenuScene中,当用户点击开始游戏时,导演让场景进入到CMGameScene 头文件 class CMGameScene : public cocos2d::CCLayer,publ ...

  8. 饭卡------HDOJ杭电2546(还是01背包!!!!!!)

    Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩 ...

  9. Swift编程语言学习2.1——基础运营商(在)

    操作员正在检查,更改.归并值特殊符号或短语.例如,加+这两个数字相加(例如let i = 1 + 2). 算如更复杂的逻辑和操作的实施&&(例如if enteredDoorCode & ...

  10. 内核必看: spinlock、 mutex 以及 semaphore

    linux 内核的几种锁介绍 http://wenku.baidu.com/link?url=RdvuOpN3RPiC5aY0fKi2Xqw2MyTnpZwZbE07JriN7raJ_L6Ss8Ru1 ...