我正在尝试实现WPF打印功能。只要用户不想打印超过一页的打印,它就能正常工作。 My Application使用户可以在运行时创建xaml。现在我想让他打印出他创建的xaml控件。
我已经检查了所有xaml控件的总高度,由页面高度划分并取消了该数字,所以我知道我需要打印多少页。
接下来,我为每个页面创建一个FrameworkElements列表(参见下面的逻辑),并将每个List(每个列表代表一页)存储在一个数组中。
最后,我想创建一个包含每个页面的预览,并允许用户打印所有页面。为了做到这一点,我必须将页面放在一起文档,但我不知道如何。请看一下我的代码:
Package package = Package.Open("test.xps", FileMode.Create);
// Create new xps document based on the package opened
XpsDocument doc = new XpsDocument(package);
// Create an instance of XpsDocumentWriter for the document
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
// Write the canvas (as Visual) to the document
double height = element.ActualHeight;
double width = element.ActualWidth;
System.Windows.Controls.PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
Size pageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight);
int pageCount = (int)Math.Ceiling(height / pageSize.Height);
if (pageSize.Height < height) {
var grid = element as Grid;
var children = grid.Children;
List<FrameworkElement>[] pages = new List<FrameworkElement>[pageCount-1];
int i = 0;
double currentHeight = 0;
foreach (FrameworkElement c in children) {
currentHeight += c.RenderSize.Height;
if (currentHeight < pageSize.Height) {
pages[i] = new List<FrameworkElement>();
pages[i].Add(c);
}
else {
i++;
currentHeight = 0;
pages[i] = new List<FrameworkElement>();
pages[i].Add(c);
}
}
for (int j = 0; j < pageCount; j++) {
var collator = writer.CreateVisualsCollator();
collator.BeginBatchWrite();
foreach (FrameworkElement c in pages[j]) {
collator.Write(c);
}
collator.EndBatchWrite();
}
}
doc.Close();
package.Close();
string filename = @"C:\Users\rzimmermann\Documents\Visual Studio 2012\Projects\MvvmLightPrintFunction\MvvmLightPrintFunction\bin\Debug\test.xps";
DocumentViewer viewer = new DocumentViewer();
doc = new XpsDocument(filename, FileAccess.Read);
viewer.Document = doc.GetFixedDocumentSequence();
Window ShowWindow = new Window();
ShowWindow.Width = 400;
ShowWindow.Height = 300;
ShowWindow.Content = viewer;
ShowWindow.Show();
非常感谢你。
答案 0 :(得分:1)
@kenny是对的,你需要使用FixedPages。有关精彩的教程请求请参阅:http://www.nbdtech.com/Blog/archive/2009/04/20/wpf-printing-part-2-the-fixed-document.aspx
就向文档添加页面而言,您可以这样做:
doc.Pages.Add(page1Content);
doc.Pages.Add(page2Content);
//等。
答案 1 :(得分:0)
2019-09-12 15:29:50.576 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Motion] - x [1.0 y [-0.003921509] event [MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1.0, y[0]=-0.003921509, toolType[0]=TOOL_TYPE_UNKNOWN, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=1437191, downTime=0, deviceId=5, source=0x1000010 }]
2019-09-12 15:29:50.576 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Down] - code [22] event [{KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DPAD_RIGHT, scanCode=0, metaState=0, flags=0x400, repeatCount=0, eventTime=1437191, downTime=1437191, deviceId=5, source=0x1000010 }}]
2019-09-12 15:29:51.016 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Motion] - x [0.85882366 y [-0.003921509] event [MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=0.85882366, y[0]=-0.003921509, toolType[0]=TOOL_TYPE_UNKNOWN, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=1437627, downTime=0, deviceId=5, source=0x1000010 }]
2019-09-12 15:29:51.036 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Motion] - x [0.27058828 y [-0.003921509] event [MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=0.27058828, y[0]=-0.003921509, toolType[0]=TOOL_TYPE_UNKNOWN, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=1437651, downTime=0, deviceId=5, source=0x1000010 }]
2019-09-12 15:29:51.036 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Up] - code [22] event [{KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DPAD_RIGHT, scanCode=0, metaState=0, flags=0x400, repeatCount=0, eventTime=1437651, downTime=1437651, deviceId=5, source=0x1000010 }}]
2019-09-12 15:29:51.056 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Motion] - x [0.003921628 y [-0.003921509] event [MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=0.003921628, y[0]=-0.003921509, toolType[0]=TOOL_TYPE_UNKNOWN, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=1437663, downTime=0, deviceId=5, source=0x1000010 }]
this.scrollCheckCardinfo是一个网格。