直到现在,要将许多用户控件打印到具有许多页面的PDF中,每个用户控件一个页面,我有以下代码:
public static void ImprimirWpf(IEnumerable<dynamic> paramIeViewsParaImprimir)
{
int miFactor = 3;
Size miTamañoPagina = new Size(paramIeViewsParaImprimir.ElementAt(0).Width, paramIeViewsParaImprimir.ElementAt(0).Height);
FixedDocument miDocumento = new FixedDocument();
miDocumento.DocumentPaginator.PageSize = miTamañoPagina;
for (int i = 0; i < paramIeViewsParaImprimir.Count(); i++)
{
paramIeViewsParaImprimir.ElementAt(i).Measure(miTamañoPagina);
paramIeViewsParaImprimir.ElementAt(i).Arrange(new Rect(miTamañoPagina));
paramIeViewsParaImprimir.ElementAt(i).UpdateLayout();
int miAncho = (int)Math.Round(miTamañoPagina.Width / 2, MidpointRounding.AwayFromZero) * 2;
int miAlto = (int)Math.Round(miTamañoPagina.Height / 2, MidpointRounding.AwayFromZero) * 2;
System.Windows.Media.Imaging.RenderTargetBitmap bitmap = new System.Windows.Media.Imaging
.RenderTargetBitmap(miAncho * miFactor, miAlto * miFactor, 96 * miFactor, 96 * miFactor, System.Windows.Media.PixelFormats.Pbgra32);
bitmap.Render(paramIeViewsParaImprimir.ElementAt(i));
System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
myImage.Source = bitmap;
FixedPage miPagina1 = new FixedPage();
miPagina1.Width = miDocumento.DocumentPaginator.PageSize.Width;
miPagina1.Height = miDocumento.DocumentPaginator.PageSize.Height;
miPagina1.Children.Add(myImage);
PageContent miContenido1 = new PageContent();
((IAddChild)miContenido1).AddChild(miPagina1);
miDocumento.Pages.Add(miContenido1);
}
System.Windows.Controls.PrintDialog myDialog = new System.Windows.Controls.PrintDialog();
//With the if, it will show a dialog to select the printer, to print on printer or on PDF or XPS.
if (myDialog.ShowDialog() == true)
{
//Print the image.
myDialog.PrintDocument(miDocumento.DocumentPaginator, string.Empty);
}
}
但是我已经意识到,如果将相同的用户控件更改为窗口,则会得到一个空文档,但是如果我将其更改为用户控件,则会得到预期的PDF。
要打印视觉效果,用户控件与窗口是否有所不同?
谢谢。