PDF打印多页(Printing PDF with Multiple Pages)

2019-10-18 02:56发布

我到处寻找的东西帮助我,但至今一无所获。 我想创建一个程序,允许用户打印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手动,但所有的打印的帮助下在我有一个很难理解他们的样本项目介绍。 在这个问题上的任何帮助将不胜感激。 谢谢 :)

Answer 1:

我得到了它,大多是从看下面的问题 。 我需要使用Doc.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.
    }

觉得很傻已经问过这个问题,然后回答自己。 但感觉好知道这个问题现在已经在那里为其他任何人被卡住的问题。



文章来源: Printing PDF with Multiple Pages