打印PDF与多页

本文关键字:PDF 打印 | 更新日期: 2023-09-27 18:11:39

我到处找东西来帮助我解决这个问题,但到目前为止还没有。我试图创建一个程序,允许用户打印pdf文件的集合。我使用ABCPDF9来获取我的pdf(其中大部分存储为html),并将它们全部附加到单个ABCPDF.Doc对象。我得到的问题是,当我有这些多页,我最终只有一页的pdf打印。下面是一些代码片段:

    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事件。

    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手册,但是所有关于打印的帮助都是在他们的示例项目中呈现的,我很难理解。对此事的任何帮助都将是感激的。谢谢:)

打印PDF与多页

我明白了,主要是看下面的问题。我需要博士帮忙。PageNumber以访问pdf的每个页面。下面是打印页面事件,我在这里修改了代码。

    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.
    }
我觉得问了这个问题然后又回答了自己。但是,知道这个问题现在已经摆在了其他被这个问题困住的人面前,我感觉很好。