如何一键打印两张图像,但每个图像都在自己的页面上

本文关键字:图像 自己的 打印 何一键 一键 两张 | 更新日期: 2023-09-27 18:32:55

我正在构建一个ASP网站页面,上面有两个图像,如下所示:

<asp:Image ID="ImgPg1" runat="server" ImageUrl="Page1.ashx" />
<asp:Image ID="ImgPg2" runat="server" ImageUrl="Page2.ashx" />

我想一键打印两个图像。

Page1.ashx上的图像需要打印在第 1 页上,Page2.ashx上的图像需要打印在第 2 页上。

如何每页打印一张图像?

这就是我所做的,但我发现PrintDocument在服务器端运行,因此出现错误说"找不到打印机"。 下面的代码示例打印一个矩形作为测试,但无论如何,这就是 .ashx 页所包含的内容。

    protected void Print_Click(object sender, EventArgs e)
    {
        // Page.ClientScript.RegisterClientScriptBlock(GetType(), "Print CI", "window.print()", true);
        PrintDocument pd = new PrintDocument();
        pd.DefaultPageSettings.Landscape = false; 
        nPage = 0;
        pd.PrintPage += new PrintPageEventHandler(printImages);
        pd.Print();
    }
    private void printImages(object sender, PrintPageEventArgs ev)
    {
        Rectangle rect = new Rectangle(0, 0, 353, 230);        // 0.236 Inches @ 96 dpi = 23
        switch (nPage)
        {
            case 0:     // Page 1 just for test
                ev.Graphics.FillRectangle(new SolidBrush(Color.LightGray), rect);
                ev.Graphics.DrawRectangle(new Pen(Color.Black, 1), rect);
                break;
            case 1:     // Page 2 just for test
                rect = new Rectangle(360, 0, 353, 230);
                ev.Graphics.FillRectangle(new SolidBrush(Color.LightPink), rect);
                ev.Graphics.DrawRectangle(new Pen(Color.Black, 1), rect);
                break;
        }
        ++nPage;
        if (nPage < 2)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }

我需要每页打印一个图像的原因是,一个图像是表单的正面,另一个图像是表单的背面。 图像尺寸小于 8.5 x 11 页(大约是一张纸的 1/4)。

谢谢巴勃罗

如何一键打印两张图像,但每个图像都在自己的页面上

你真的想尝试以编程方式将东西发送到打印机吗? 除非有很好的商业理由,否则我认为"最好的情况"是将您的.ashx页面创建为打印友好的页面,上面只有两个图像,然后让用户从浏览器打印页面。

但。。。要回答您的实际问题,为了确保两个图像打印在不同的页面上,您可以在两者之间使用 CSS 分页符。

.PageBreak {
   page-break-after: always;
}
<asp:Image ID="ImgPg1" runat="server" ImageUrl="Page1.ashx" CssClass="PageBreak" />
<asp:Image ID="ImgPg2" runat="server" ImageUrl="Page2.ashx" />

这将强制打印机在第一个图像之后和第二个图像之前送入一张新纸。假设图像的大小适合一张 8.5 x 11 的纸,这应该可以很好地工作。

有关分页符后的更多信息:http://www.w3schools.com/cssref/pr_print_pageba.asp

另外,这里有很多很棒的.ashx提示:http://www.dotnetperls.com/ashx

让我们知道它是怎么回事!

凯西的回答帮助很大。 这是最终结果。

标头代码:

<script type="text/javascript">
    function printCI() {
        var printWindow = window.open('', 'CI', 'height=600,width=800');
        printWindow.document.write('<html><head><title>CI Form</title>');
        printWindow.document.write('<style>.PageBreak{page-break-after: always;}</style>');
        printWindow.document.write('</head><body >');
        printWindow.document.write('<div class=''PageBreak''><img src=''Page1.ashx'' /></div>');
        printWindow.document.write('<div><img src=''Page2.ashx'' /></div>');
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.print();
    }
</script>    

CS 代码:

protected void Print_Click(object sender, EventArgs e)
{
    // Print CI with a javascript so we can print in two pages
    Page.ClientScript.RegisterClientScriptBlock(GetType(), "Print CI", "printCI()", true);
}