2013-05-01 129 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. 
    } 

覺得這個問題很愚蠢,然後回答自己。但感覺很好,因爲知道這個問題現在已經出現在那些陷入問題的人身上了。