我正在尝试打印页面。
这是我的代码:
private void Print(object obj)
{
PrintDialog dialog = new PrintDialog();
//if (dialog.ShowDialog() != true)
//{ return; }
Grid HaemogramReport = new Grid();
RowDefinition rowTopMargin = new RowDefinition()
{
Height = new GridLength(198)
};
RowDefinition rowPatientDetails = new RowDefinition()
{
Height = new GridLength(1, GridUnitType.Star)
};
RowDefinition rowReportHeader = new RowDefinition()
{
Height = new GridLength(66)
};
RowDefinition rowBody = new RowDefinition()
{
Height = new GridLength(8, GridUnitType.Star)
};
RowDefinition rowBottomMargin = new RowDefinition()
{
Height = new GridLength(132)
};
HaemogramReport.RowDefinitions.Add(rowTopMargin);
HaemogramReport.RowDefinitions.Add(rowPatientDetails);
HaemogramReport.RowDefinitions.Add(rowReportHeader);
HaemogramReport.RowDefinitions.Add(rowBody);
HaemogramReport.RowDefinitions.Add(rowBottomMargin);
Grid Head = new Grid();
Grid.SetRow(Head, 2);
ColumnDefinition colLeftMargin = new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Star)
};
ColumnDefinition colHead = new ColumnDefinition()
{
Width = GridLength.Auto
};
ColumnDefinition colRightMargin = new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Star)
};
Head.ColumnDefinitions.Add(colLeftMargin);
Head.ColumnDefinitions.Add(colHead);
Head.ColumnDefinitions.Add(colRightMargin);
TextBlock txtblockReportHeader = new TextBlock()
{
FontSize = 24,
FontWeight = FontWeights.Bold,
Text = "HAEMOGRAM REPORT",
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
var measures = MeasureString(txtblockReportHeader);
Rectangle rectReportHeader = new Rectangle()
{
Stroke = Brushes.Black,
StrokeThickness = 1,
Width = measures.Width + 15,
Height = measures.Height + 3,
RadiusX = 7,
RadiusY = 7,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
Grid.SetColumn(rectReportHeader, 1);
Grid.SetColumn(txtblockReportHeader, 1);
Head.Children.Add(rectReportHeader);
Head.Children.Add(txtblockReportHeader);
HaemogramReport.Children.Add(Head);
HaemogramReport.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
HaemogramReport.Arrange(new Rect(new Point(0, 0), HaemogramReport.DesiredSize));
dialog.PrintVisual(HaemogramReport, "Haemogram Report");
}
private Size MeasureString(TextBlock textBlock)
{
var formattedText = new FormattedText(
textBlock.Text,
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch),
textBlock.FontSize,
Brushes.Black);
return new Size(formattedText.Width, formattedText.Height);
}
这是Print:
我希望矩形和文本块水平居中。但我认为*大小的列不能正常工作。
答案 0 :(得分:2)
我知道了。
我必须将网格的宽度设置为可打印区域的宽度。
以下是代码:
Head.Width = dialog.PrintableAreaWidth;