2013-05-01 127 views
1

我已经找遍了东西来帮助我,但迄今为止没有。我正在尝试创建一个允许用户打印一组pdf的程序。我正在使用ABCPDF9来获取我的pdf(其中大部分都以html形式存储),并将它们全部附加到单个ABCPDF.Doc对象。我得到的问题是,当我有这些多页时,我最终只有一页PDF打印。以下是一些代码片段。用多个页面打印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(); 
     } 
    } 

和我的打印页面事件。

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手册,但是所有打印帮助都出现在他们的示例项目中,我很难理解。任何帮助这个问题,将不胜感激。谢谢:)

+0

我在我的打印对话框中使用了设置,并且似乎只有第一页甚至会被绘制到文档中。如何将其他页面绘制到打印文档中?我使用ABCPDF的Save方法将pdf保存到一个文件中,并且pdf中包含所有页面。他们似乎没有被吸引到printdocument对象。 – TOlsen 2013-05-02 15:55:07

回答

0

我明白了,主要是看下面的question。我需要使用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. 
    } 

觉得这个问题很愚蠢,然后回答自己。但感觉很好,因为知道这个问题现在已经出现在那些陷入问题的人身上了。