首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >打印多页PDF

打印多页PDF
EN

Stack Overflow用户
提问于 2013-05-02 06:49:45
回答 1查看 2.6K关注 0票数 1

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

代码语言:javascript
运行
复制
    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事件。

代码语言:javascript
运行
复制
    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手册,但所有关于打印的帮助都是在他们的示例项目中提供的,我很难理解。在这件事上的任何帮助都将不胜感激。谢谢:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-03 00:14:49

我主要是从下面的question中了解到的。我需要使用Doc.PageNumber来访问pdf的每一页。下面是我修改代码的打印页面事件。

代码语言:javascript
运行
复制
    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.
    }

问了这个问题,然后回答我自己,我觉得很傻。但知道这个问题现在对任何其他被困在这个问题上的人来说都是很好的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16327712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档