WPF:创建XpsDocument分页
我正在尝试创建一个分页器,该分页器将创建一个XpsDocument,我可以使用它进行预览,然后进行打印。我在网上找到了以下代码,但不了解它是如何工作的 我遇到的问题是,如果按原样运行,页面将成功生成。如果我注释掉OnRender()中生成数据实际值的行(随机之后的所有行…),我会得到大约一行高的页面,上面没有文本,但它们看起来是正确的长度。是什么阻止显示“行编号”和“列i”的值 我已经包括了两个屏幕截图来说明WPF:创建XpsDocument分页,wpf,c#-4.0,pagination,xps,xpsdocument,Wpf,C# 4.0,Pagination,Xps,Xpsdocument,我正在尝试创建一个分页器,该分页器将创建一个XpsDocument,我可以使用它进行预览,然后进行打印。我在网上找到了以下代码,但不了解它是如何工作的 我遇到的问题是,如果按原样运行,页面将成功生成。如果我注释掉OnRender()中生成数据实际值的行(随机之后的所有行…),我会得到大约一行高的页面,上面没有文本,但它们看起来是正确的长度。是什么阻止显示“行编号”和“列i”的值 我已经包括了两个屏幕截图来说明 public class TaxCodePrintPaginator : Docume
public class TaxCodePrintPaginator : DocumentPaginator
{
private int _RowsPerPage;
private Size _PageSize;
private int _Rows;
private List<TaxCode> _dataList;
public TaxCodePrintPaginator(List<TaxCode> dt, int rows, Size pageSize)
{
_dataList = dt;
_Rows = rows;
PageSize = pageSize;
}
public override DocumentPage GetPage(int pageNumber)
{
int currentRow = _RowsPerPage * pageNumber;
var page = new PageElement(currentRow, Math.Min(_RowsPerPage, _Rows - currentRow))
{
Width = PageSize.Width,
Height = PageSize.Height
};
page.Arrange(new Rect(new Point(0, 0), PageSize));
return new DocumentPage(page);
}
public override bool IsPageCountValid
{ get { return true; } }
public override int PageCount
{ get { return (int)Math.Ceiling(_Rows / (double)_RowsPerPage); } }
public override Size PageSize
{
get { return _PageSize; }
set
{
_PageSize = value;
_RowsPerPage = 40;
//Can't print anything if you can't fit a row on a page
Debug.Assert(_RowsPerPage > 0);
}
}
public override IDocumentPaginatorSource Source
{ get { return null; } }
}
public class PageElement : UserControl
{
private const int PageMargin = 75;
private const int HeaderHeight = 25;
private const int LineHeight = 20;
private const int ColumnWidth = 140;
private int _CurrentRow;
private int _Rows;
public PageElement(int currentRow, int rows)
{
Margin = new Thickness(PageMargin);
_CurrentRow = currentRow;
_Rows = rows;
}
private static FormattedText MakeText(string text)
{
return new FormattedText(text, CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Tahoma"), 14, Brushes.Black);
}
public static int RowsPerPage(double height)
{
return (int)Math.Floor((height - (2 * PageMargin)
- HeaderHeight) / LineHeight);
}
protected override void OnRender(DrawingContext dc)
{
Point curPoint = new Point(0, 0);
dc.DrawText(MakeText("Row Number"), curPoint);
curPoint.X += ColumnWidth;
for (int i = 1; i < 4; i++)
{
dc.DrawText(MakeText("Column " + i), curPoint);
curPoint.X += ColumnWidth;
}
curPoint.X = 0;
curPoint.Y += LineHeight;
dc.DrawRectangle(Brushes.Black, null, new Rect(curPoint, new Size(Width, 2)));
curPoint.Y += HeaderHeight - LineHeight;
Random numberGen = new Random();
for (int i = _CurrentRow; i < _CurrentRow + _Rows; i++)
{
dc.DrawText(MakeText(i.ToString()), curPoint);
curPoint.X += ColumnWidth;
for (int j = 1; j < 4; j++)
{
dc.DrawText(MakeText(numberGen.Next().ToString()), curPoint);
curPoint.X += ColumnWidth;
}
curPoint.Y += LineHeight;
curPoint.X = 0;
}
}
}
公共类TaxCodePrintPaginator:DocumentPaginator
{
私人互联网网页;
私人大小PageSize;
私有整数行;
私有列表(dataList),;
公共TaxCodePrintPaginator(列表dt、整数行、大小pageSize)
{
_dataList=dt;
_行=行;
PageSize=页面大小;
}
公共覆盖文档页面GetPage(内部页码)
{
int currentRow=_RowsPerPage*页码;
var page=new PageElement(currentRow,Math.Min(_RowsPerPage,_Rows-currentRow))
{
宽度=页面大小。宽度,
高度=页面大小。高度
};
page.Arrange(新的Rect(新的点(0,0),PageSize));
返回新文档页面(第页);
}
公共覆盖bool IsPageCountValid
{get{return true;}}
公共覆盖整页计数
{get{return(int)Math.天花(_Rows/(double)_RowsPerPage);}
公共覆盖大小PageSize
{
获取{return\u PageSize;}
设置
{
_PageSize=值;
_RowsPerPage=40;
//如果无法在页面上放置行,则无法打印任何内容
Assert(_RowsPerPage>0);
}
}
公共覆盖IDocumentPaginatorSource源
{get{returnnull;}}
}
公共类PageElement:UserControl
{
专用常量int PageMargin=75;
私人建筑总建筑高度=25;
专用常数int线宽=20;
private const int ColumnWidth=140;
私有int_CurrentRow;
私有整数行;
公共页面元素(int currentRow,int rows)
{
边距=新厚度(页边距);
_CurrentRow=CurrentRow;
_行=行;
}
私有静态格式化文本生成文本(字符串文本)
{
返回新的格式化文本(文本,CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
新字体(“Tahoma”),14号,黑色;
}
公共静态int RowsPerPage(双高)
{
返回(整数)数学楼层((高度-(2*PageMargin)
-头重/线高);
}
受保护的覆盖无效OnRender(DrawingContext dc)
{
点curPoint=新点(0,0);
dc.DrawText(MakeText(“行号”),curPoint;
curPoint.X+=列宽;
对于(int i=1;i<4;i++)
{
dc.DrawText(MakeText(“列”+i),curPoint);
curPoint.X+=列宽;
}
凝乳点X=0;
curPoint.Y+=线宽;
dc.DrawRectangle(画笔.黑色,空,新矩形(电流点,新尺寸(宽度,2));
curPoint.Y+=磁头高度-线宽;
Random numberGen=新随机数();
对于(int i=\u CurrentRow;i<\u CurrentRow+\u Rows;i++)
{
dc.DrawText(MakeText(i.ToString()),curPoint);
curPoint.X+=列宽;
对于(int j=1;j<4;j++)
{
DrawText(MakeText(numberGen.Next().ToString()),curPoint);
curPoint.X+=列宽;
}
curPoint.Y+=线宽;
凝乳点X=0;
}
}
}
之前
之后
屏幕截图在哪里?@Jon我想我最初发布这篇文章时遇到了问题。我已经将它们添加到OP中。我没有发现代码有任何明显的错误。您是否尝试解压缩生成的xps文档并查看文本是否已添加?我现在最好的猜测是文本被添加了,但是页面大小出了问题。