Question

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();
        }
    }

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);
        }
    }

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 :)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top