iTextSharp的PDF打印 [英] iTextSharp PDF printing

查看:1202
本文介绍了iTextSharp的PDF打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建将直接发送PDF文件使用我的打印机的方法(导致打印对话框出现)。



下面是我的代码一直在努力 - 其中大部分是在论坛中这里发现。如果我使用iTextSharp的创建一个新的PDF文档,但只要我尝试一些J​​avaScript注入现有的文件,我调用打印()的时候会得到一个异常正常工作方法说




对象不支持属性或方法打印




 <脚本类型=文/ JavaScript的> 
功能的load(){
尝试{
变种X =的document.getElementById(帧1);
x.print();
}
赶上(错误){
}
}
< / SCRIPT>

<身体的onload =负荷();>
<表ID =form1的=服务器>
< D​​IV>
< IFRAME ID =帧1SRC =C:/1686850_1.pdf=服务器FRAMEBORDER =0的风格=高度:0像素,宽度:0像素; />
< / DIV>
< /表及GT;
< /身体GT;
< / HTML>



.CS文件

 使用System.IO; 
使用iTextSharp.text;使用iTextSharp.text.pdf
;

公共部分类打印:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs五)
{
SetPDF( File.ReadAllBytes(C:\\1686850.pdf),C:\\1686850_1.pdf); //测试文件
}

私人无效SetPDF(字节[]文件,字符串outputPath)
{
PdfReader读卡器=新PdfReader(文件);
INT = PAGECOUNT reader.NumberOfPages;
矩形的pageSize = reader.GetPageSize(1);

文档PDF =新的文件(pageSize的);
PdfWriter作家= PdfWriter.GetInstance(PDF,新的FileStream(outputPath,FileMode.Create));
pdf.Open();

//这个动作直接导致打印机对话
PdfAction jAction = PdfAction.JavaScript(this.print(真); \r,作家);
writer.AddJavaScript(jAction);
//省略此循环,简单地增加一些文本文件产生我想要的行为。
的for(int i = 0; I<页页次,我++)
{
pdf.NewPage();
PdfImportedPage页= writer.GetImportedPage(阅读器,I + 1);
writer.DirectContent.AddTemplate(页面,0,0);
}
pdf.Close();

//打开在框架
frame1.Attributes的PDF [SRC] = outputPath;
}
}


解决方案

我找到一种方法,在这里做到这一点: http://wskidmore.com/2011/03/pdf - 首次 - 视图 - 设置 - iTextSharp的/



在此基础上,我写了这个代码:

 私人无效PrintMenu()
{
...
VAR notUriPath =使用Server.Mappath(〜/+ filePathName);

变种DOC =新的文件();
变种读卡器=新PdfReader(notUriPath);
使用(VAR的MemoryStream =新的MemoryStream())
{
VAR作家= PdfWriter.GetInstance(文件,MemoryStream的);
doc.Open();

//这个动作直接导致打印机对话
VAR jAction = PdfAction.JavaScript(this.print(真); \r,作家);
writer.AddJavaScript(jAction);

变种CB = writer.DirectContent;
doc.AddDocListener(作家);

表示(变量p为1; P&下; = reader.NumberOfPages,P ++)
{
doc.SetPageSize(reader.GetPageSize(P));
doc.NewPage();
VAR页= writer.GetImportedPage(读卡器,P);
变种腐烂= reader.GetPageRotation(对);
如果(腐== 90 ||腐== 270)
cb.AddTemplate(页面,0,-1.0F,1.0F,0,0,reader.GetPageSizeWithRotation(p)的.Height);
,否则
cb.AddTemplate(页面,1.0F,0,0,1.0F,0,0);
}

reader.Close();
doc.Close();
File.WriteAllBytes(notUriPath,memoryStream.ToArray());
}

theIframeForPrint.Attributes.Add(src用户,fullFilePath);
}



我希望它能帮助!


I'm trying to create a method that will send a PDF file directly to my printer (causing the print dialog to appear).

Below is the code I've been working on - most of it found in the forums here. It works fine if I use iTextSharp to create a new PDF document, but as soon as I try to inject some JavaScript into an existing file, I get an exception when calling the print() method saying

Object doesn't support property or method 'print'

<script type="text/javascript">
    function load() {
        try {
            var x = document.getElementById("frame1");
            x.print();
        }
        catch (err) {
        }
    }
</script>

<body onload="load();">
    <form id="form1" runat="server">
    <div>
       <iframe id="frame1" src="C:/1686850_1.pdf"  runat="server" frameborder="0" style="height: 0px; width: 0px;" />
    </div>
    </form>
</body>
</html>

.CS file

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

public partial class Print : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetPDF(File.ReadAllBytes("C:\\1686850.pdf"), "C:\\1686850_1.pdf"); //test files
    }

    private void SetPDF(byte[] file, string outputPath)
    {
        PdfReader reader = new PdfReader(file);
        int pageCount = reader.NumberOfPages;
        Rectangle pageSize = reader.GetPageSize(1);

        Document pdf = new Document(pageSize);
        PdfWriter writer = PdfWriter.GetInstance(pdf, new FileStream(outputPath, FileMode.Create));
        pdf.Open();

        //This action leads directly to printer dialogue
        PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
        writer.AddJavaScript(jAction);
        //Omitting this loop and simply adding some text to the file produces the behavior I want.
        for (int i = 0; i < pageCount; i++)
        {
            pdf.NewPage();
            PdfImportedPage page = writer.GetImportedPage(reader, i + 1);
            writer.DirectContent.AddTemplate(page, 0, 0);
        }
        pdf.Close();

        //Open the pdf in the frame
        frame1.Attributes["src"] = outputPath;
    }
}

解决方案

I found a way to do this here: http://wskidmore.com/2011/03/pdf-initial-view-settings-itextsharp/

Based on that, I wrote this code:

private void PrintMenu()
{
    ...
    var notUriPath = Server.MapPath("~/" + filePathName);

    var doc = new Document();
    var reader = new PdfReader(notUriPath);
    using (var memoryStream = new MemoryStream())
    {
        var writer = PdfWriter.GetInstance(doc, memoryStream);
        doc.Open();

        // this action leads directly to printer dialogue
        var jAction = PdfAction.JavaScript("this.print(true);\r", writer);
        writer.AddJavaScript(jAction);

        var cb = writer.DirectContent;
        doc.AddDocListener(writer);

        for (var p = 1; p <= reader.NumberOfPages; p++)
        {
            doc.SetPageSize(reader.GetPageSize(p));
            doc.NewPage();
            var page = writer.GetImportedPage(reader, p);
            var rot = reader.GetPageRotation(p);
            if (rot == 90 || rot == 270)
                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
            else
                cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
        }

        reader.Close();
        doc.Close();
        File.WriteAllBytes(notUriPath, memoryStream.ToArray());
    }

    theIframeForPrint.Attributes.Add("src", fullFilePath);
}

I hope it helps!

这篇关于iTextSharp的PDF打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆