打印多页 PDF [英] Printing PDF with Multiple Pages

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

问题描述

我四处寻找可以帮助我解决此问题的方法,但到目前为止一无所获.我正在尝试创建一个允许用户打印 pdf 集合的程序.我正在使用 ABCPDF9 来获取我的 pdf(其中大部分存储为 html)并将它们全部附加到一个 ABCPDF.Doc 对象中.我遇到的问题是当我有这些多页时,我最终只有一页 pdf 打印.下面是一些代码片段.

I've looked all over for something to help me with this, but so far nothing. I am trying to create a program that allows a user to print a collection of pdfs. I am using ABCPDF9 to get my pdfs (most of which are stored as html) and append them all to a single ABCPDF.Doc object. The problem I'm getting is when I have these multiple pages I end up with only one page of the pdf printing. Here are some code snippets below.

    private void ProcessAndPrintSelected()
    {
        var selectedForm = SubSonicRepository.Instance.CommunicationRepository.GetMessageTemplateByID((int)cmboChooseForm.SelectedValue);
        _currentItemIndex = 0;
        int itemsCount = dataGridViewLoans.RowCount;
        _currentPrintPageIndex = 1;           
        foreach (DataGridViewRow row in this.dataGridViewLoans.Rows)
        {                 
            lblPrinterProgress.Text = "Printing document " + _currentItemIndex + " of " + itemsCount + ".";
            lblPrinterProgress.Refresh();
            Application.DoEvents();
            BulkPrinterLoanModel loan = row.DataBoundItem as BulkPrinterLoanModel;
            try
            {
                if (selectedForm.MailMessageContent != null)
                {
                    byte[] formBytes = GetFormBytes(selectedForm.ID, loan.ApplicantID, loan.LoanID);
                    doc.Read(formBytes);
                    appendedDocs.Append(doc);
                }
                else
                {
                    throw new InvalidOperationException("No PDF data to print.");
                }
            }
            catch (Exception x)
            {
                //for now, don't do anything, not even logging, but don't halt queue either.
                MessageBox.Show(x.ToString());
            }
        }
        printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
        printDoc.PrinterSettings.FromPage = 1;
        printDoc.PrinterSettings.ToPage = appendedDocs.PageCount;
        printDoc.PrinterSettings.MinimumPage = 1;
        printDoc.PrinterSettings.MaximumPage = appendedDocs.PageCount;
        PrintDialog pDialog = new PrintDialog();
        pDialog.Document = printDoc;
        pDialog.AllowSomePages = true;
        if (pDialog.ShowDialog() == DialogResult.OK)
        {
            pDialog.Document.Print();
        }
    }

还有我的 printpage 事件.

and my printpage event.

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        XRect cropBox = appendedDocs.CropBox;
        double srcWidth = (cropBox.Width / 72) * 100;
        double srcHeight = (cropBox.Height / 72) * 100;
        double pageWidth = e.PageBounds.Width;
        double pageHeight = e.PageBounds.Height;
        double marginX = e.PageSettings.HardMarginX;
        double marginY = e.PageSettings.HardMarginY;

        //center it
        double x = (pageWidth - srcWidth) / 2;
        double y = (pageHeight - srcHeight) / 2;
        x -= marginX;
        y -= marginY;

        RectangleF rect = new RectangleF((float)x, (float)y, (float)srcWidth, (float)srcHeight);
        appendedDocs.Rect.SetRect(cropBox);
        int rez = e.PageSettings.PrinterResolution.X;
        appendedDocs.Rendering.DotsPerInch = rez;
        Graphics g = e.Graphics;
        using (Bitmap bitmap = appendedDocs.Rendering.GetBitmap())
        {
            g.DrawImage(bitmap, rect);
        }
    }

我查看了 ABCPDF 手册,但所有有关打印的帮助都在他们的示例项目中提供,我很难理解.对此事的任何帮助将不胜感激.谢谢:)

I've looked in the ABCPDF manual, but all the help on printing is presented in their sample project which I'm having a hard time understanding. Any help on this matter would be appreciated. Thanks :)

推荐答案

我明白了,主要是看下面的问题.我需要使用 Doc.PageNumber 来访问 pdf 的每一页.这是我更改代码的打印页面事件.

I got it, mostly from looking at the following question. I needed to use the Doc.PageNumber to access each page of the pdf. Here's the print page event where I changed the code.

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        _currentItemIndex++;//added index to keep track of page. default to 1
        appendedDocs.PageNumber = _currentItemIndex;//set to current page for printing
        XRect cropBox = appendedDocs.CropBox;
        double srcWidth = (cropBox.Width / 72) * 100;
        double srcHeight = (cropBox.Height / 72) * 100;
        double pageWidth = e.PageBounds.Width;
        double pageHeight = e.PageBounds.Height;
        double marginX = e.PageSettings.HardMarginX;
        double marginY = e.PageSettings.HardMarginY;

        //center it
        double x = (pageWidth - srcWidth) / 2;
        double y = (pageHeight - srcHeight) / 2;
        x -= marginX;
        y -= marginY;

        RectangleF rect = new RectangleF((float)x, (float)y, (float)srcWidth, (float)srcHeight);
        appendedDocs.Rect.SetRect(cropBox);
        int rez = e.PageSettings.PrinterResolution.X;
        appendedDocs.Rendering.DotsPerInch = rez;
        Graphics g = e.Graphics;

        using (Bitmap bitmap = appendedDocs.Rendering.GetBitmap())
        {
            g.DrawImage(bitmap, rect);
        }

        e.HasMorePages = _currentItemIndex < appendedDocs.PageCount;//check for more pages.
    }

问这个问题然后回答我自己感觉很傻.但是,知道这个问题现在可供任何陷入该问题的其他人使用,这感觉很好.

Feel silly having asked this question and then answering myself. But it feels good knowing that this question is now out there for anyone else that gets stuck on the issue.

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

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