我一直在寻找能帮到我的东西,但到目前为止还没有。我正在尝试创建一个允许用户打印pdfs集合的程序。我正在使用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手册,但所有关于打印的帮助都出现在他们的样本项目中,我很难理解。任何有关此事的帮助将不胜感激。谢谢:))
答案 0 :(得分: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.
}
在问这个问题然后自言自语之后感到愚蠢。但是知道这个问题现在出现在其他任何陷入这个问题的人身上感觉很好。