最近做了一个Silverlight5连续打印的功能,需要打印很长的数据列表,但是打印出来的效果出了点小问题:打印在纸上时,内容会在分页的地方被空白断开,想请教下各位如何解决这个问题,多谢指教
以下是打印出来的效果:
XPS效果:
打印代码:
/// <summary> /// 打印次数 /// </summary> private int _PrintCount; /// <summary> /// 打印元素 /// </summary> Canvas printCanvas = new Canvas(); /// <summary> /// 打印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void btnSave_Click(object sender, RoutedEventArgs e) { if (Application.Current.IsRunningOutOfBrowser) { PrintDocument document = new PrintDocument(); // tell the API what to print document.PrintPage += Document_PrintPage; document.EndPrint += Document_EndPrint; PrinterFallbackSettings setting = new PrinterFallbackSettings(); setting.ForceVector = false; try { _PrintCount = 0; indicator.IsBusy = true; //indicator.BusyContent = "正在处理,请稍候..."; rootPage.Children.Remove(rootPrint); printCanvas.Children.Add(rootPrint); document.Print("报表打印", setting, false); } catch { MessageBox.Show("打印时发生了错误"); } } } /// <summary> /// 页打印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Document_PrintPage(object sender, PrintPageEventArgs e) { if (PrintPages > 0) { printCanvas.Width = e.PrintableArea.Width; printCanvas.Height = e.PrintableArea.Height; rootPrint.Width = e.PrintableArea.Width; //定义一个打印的元素 e.PageVisual = printCanvas; //得到最顶端位置 double topPosition = e.PrintableArea.Height * _PrintCount; //位移 Canvas.SetTop(rootPrint, topPosition * -1); _PrintCount++; bool hasPages = rootPrint.ActualHeight - topPosition > e.PrintableArea.Height; //打印多份 if (PrintPages > 1) { e.HasMorePages = true; if (!hasPages) { PrintPages--; _PrintCount = 0; } } else { e.HasMorePages = hasPages; } } else { e.HasMorePages = false; } }
应该是算错了。我记得那个单位跟一般单位是不一样的。1/100像素的。
跟算错没啥关系,A4打印一点问题都没有