<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rodrigo Díaz Concha &#187; PrintDocument</title>
	<atom:link href="http://rdiazconcha.com/tag/printdocument/feed/" rel="self" type="application/rss+xml" />
	<link>http://rdiazconcha.com</link>
	<description>Silverlight y XAML en Español para México, Latinoamérica y España</description>
	<lastBuildDate>Thu, 02 Feb 2012 20:54:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Silverlight 4 Beta &#8211; Impresi&#243;n, Modularizaci&#243;n y Par&#225;metros</title>
		<link>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin-modularizacin-y-parmetros/</link>
		<comments>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin-modularizacin-y-parmetros/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 21:37:02 +0000</pubDate>
		<dc:creator>Rodrigo</dc:creator>
				<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[Impresion]]></category>
		<category><![CDATA[PrintDocument]]></category>

		<guid isPermaLink="false">http://rdiazconcha.com/?p=448</guid>
		<description><![CDATA[En el artículo anterior vimos cómo mandar a imprimir múltiples páginas dentro de nuestras aplicaciones de Silverlight 4.&#160; En este artículo iremos un paso más allá, modularizando un poco más nuestro código e implementando la funcionalidad de parametrización de las opciones de impresión (encabezado y márgen principalmente).&#160; Esto nos ofrecerá un código más robusto que [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://rdiazconcha.com/?p=437" target="_blank">En el artículo anterior</a> vimos cómo mandar a imprimir múltiples páginas dentro de nuestras aplicaciones de Silverlight 4.&#160; En este artículo iremos un paso más allá, modularizando un poco más nuestro código e implementando la funcionalidad de parametrización de las opciones de impresión (encabezado y márgen principalmente).&#160; Esto nos ofrecerá un código más robusto que permita escenarios de impresión más detallados.&#160; Nota: esta implementación es una propuesta e idea para poder modularizar y de ninguna manera pretende ser el único mecanismo o camino para lograrlo.</p>
<h3>La Solución</h3>
<p>Utilizaremos la misma solución del proyecto del artículo anterior.&#160; A esta solución le agregaremos un nuevo proyecto de tipo Silverlight Class Library para poder incorporar en él la funcionalidad de impresión, en vez de tenerlo directamente en el code-behind de MainPage.xaml del proyecto Silverlight.&#160; Al nuevo proyecto le pondremos el nombre de SL4.Demo.Impresion.Util y agregaremos dos clases: Impresion.cs y Parametros.cs</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://rdiazconcha.com/wp-content/uploads/2009/11/image_thumb.png" width="404" height="386" /></a> </p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/image1.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://rdiazconcha.com/wp-content/uploads/2009/11/image_thumb1.png" width="404" height="459" /></a> </p>
<h3>Impresion.cs</h3>
<p>Será en esta clase en donde implementaremos el código de impresión.&#160; Esta clase tendrá un método público Imprimir() el cual nos servirá como manejador del evento clic del botón en donde deseemos invocar la funcionalidad (botón button2 en MainPage.xaml).&#160; De esta manera estaremos manejando el evento clic en una instancia de Impresion en vez de hacerlo directamente en code-behind.</p>
<p>La clase Impresion será de tipo genérica, ya que necesitamos conocer de antemano el tipo de contenido que se mandará a imprimir para poder realizar el filtrado de los registros de manera adecuada.&#160; En nuestro ejemplo es un objeto de tipo <em>Albumes. </em></p>
<p>Por las razones descritas, el código de MainPage.xaml.cs quedará de la siguiente manera:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8bbc5741-66d2-4962-9ae1-1bb8e5fa9198" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"> <span style="color:#0000ff">public</span> <span style="color:#0000ff">partial</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MainPage</span> : <span style="color:#2b91af">UserControl</span><br /> &#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">SL4</span>.<span style="color:#800000">Impresion</span>.<span style="color:#2b91af">Impresion</span>&lt;<span style="color:#2b91af">Album</span>&gt; <span style="color:#800000">util</span> = <span style="color:#0000ff">null</span>;</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">public</span> <span style="color:#800000">MainPage</span>()<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">InitializeComponent</span>();</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">util</span> = <span style="color:#0000ff">new</span> <span style="color:#800000">Impresion</span>.<span style="color:#2b91af">Impresion</span>&lt;<span style="color:#2b91af">Album</span>&gt;(<span style="color:#0000ff">this</span>.<span style="color:#800000">Resources</span>[<span style="color:#ff0000">&quot;albumes&quot;</span>] <span style="color:#0000ff">as</span> <span style="color:#2b91af">Albumes</span>, <br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">this</span>.<span style="color:#800000">Resources</span>[<span style="color:#ff0000">&quot;ImpresionDataTemplate&quot;</span>] <span style="color:#0000ff">as</span> <span style="color:#2b91af">DataTemplate</span>,<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a52a2a">40.0</span>,<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000">&quot;Documento de Prueba&quot;</span>, <br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000">&quot;Lista de &#193;lbumes &#8211; The Beatles&quot;</span>);</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">util</span>.<span style="color:#800000">ImpresionFinalizada</span> += <span style="color:#0000ff">new</span> <span style="color:#2b91af">EventHandler</span>(<span style="color:#800000">util_ImpresionFinalizada</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">this</span>.<span style="color:#800000">button1</span>.<span style="color:#800000">Click</span> += <span style="color:#0000ff">new</span> <span style="color:#2b91af">RoutedEventHandler</span>(<span style="color:#800000">button1_Click</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">this</span>.<span style="color:#800000">button2</span>.<span style="color:#800000">Click</span> += <span style="color:#800000">util</span>.<span style="color:#800000">Imprimir</span>;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">void</span> <span style="color:#800000">button1_Click</span>(<span style="color:#0000ff">object</span> <span style="color:#800000">sender</span>, <span style="color:#2b91af">RoutedEventArgs</span> <span style="color:#800000">e</span>)<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">util</span>.<span style="color:#800000">Configurar</span>();<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">void</span> <span style="color:#800000">util_ImpresionFinalizada</span>(<span style="color:#0000ff">object</span> <span style="color:#800000">sender</span>, <span style="color:#2b91af">EventArgs</span> <span style="color:#800000">e</span>)<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">MessageBox</span>.<span style="color:#800000">Show</span>(<span style="color:#ff0000">&quot;ok!!!&quot;</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</p>
<p> &#160;&#160;&#160;&#160;}</p></div>
</p></div>
</p></div>
</p>
<p>En el constructor de la clase Impresion mandamos los parámetros necesarios los cuales son la fuente de datos, el DataTemplate que será usado para definir la distribución de los elementos, el alto que tendrá cada línea (corresponde al alto del registro que tiene el Grid del DataTemplate), el nombre que deseamos que aparezca en el panel de la impresora y el título deseado.</p>
<p>Además, con la siguiente línea:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2a602fcd-467a-43ff-bca0-859c2e4876dd" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">this</span>.<span style="color:#800000">button2</span>.<span style="color:#800000">Click</span> += <span style="color:#800000">util</span>.<span style="color:#800000">Imprimir</span>;</div>
</p></div>
</p></div>
</p>
<p>Estamos definiendo como manejador del evento Click del botón al método público <strong>Imprimir</strong> implementado en la clase Impresion.&#160; Será en ese método en donde implementaremos la funcionalidad para mandar a imprimir nuestro contenido.</p>
<p>También cabe resaltar:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d387c913-9d48-4690-b4a7-f64cdcbf61c4" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px;"><span style="color:#800000">util</span>.<span style="color:#800000">ImpresionFinalizada</span> += <span style="color:#0000ff">new</span> <span style="color:#2b91af">EventHandler</span>(<span style="color:#800000">util_ImpresionFinalizada</span>);</div>
</p></div>
</p></div>
</p>
<p>Con la línea anterior estamos manejando el evento ImpresionFinalizada, evento que ha sido implementado también en la clase Impresion y que se disparará una vez finalizado el proceso de impresión (efectivamente, en el evento <strong>EndPrint</strong> del objeto PrintDocument).&#160; Lo interesante de este mecanismo es que si no establecemos un manejador para el evento, la clase impresión tiene el suyo propio: muestra un MessageBox cuando se está ejecutando adentro del navegador o muestra una ventana de Notificación (característica nueva en Silverlight 4) indicando que la impresión ha concluído.&#160; El siguiente fragmento de código muestra este concepto:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:17156ac1-dfcc-4627-9941-2c719dc05635" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px;">&#8230;.<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">EndPrint</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">this</span>.<span style="color:#800000">paginaActual</span> = <span style="color:#a52a2a">0</span>;</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> (<span style="color:#800000">a</span>.<span style="color:#800000">Error</span> != <span style="color:#0000ff">null</span>)<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">throw</span> <span style="color:#800000">a</span>.<span style="color:#800000">Error</span>;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> (<span style="color:#800000">ImpresionFinalizada</span> != <span style="color:#0000ff">null</span>)<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">ImpresionFinalizada</span>(<span style="color:#0000ff">this</span>, <span style="color:#0000ff">null</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">else</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">Notificar</span>();<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;};<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">Print</span>();<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">void</span> <span style="color:#800000">Notificar</span>()<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">if</span> (<span style="color:#2b91af">Application</span>.<span style="color:#800000">Current</span>.<span style="color:#800000">IsRunningOutOfBrowser</span>)<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">NotificationWindow</span> <span style="color:#800000">nw</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">NotificationWindow</span>();<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">nw</span>.<span style="color:#800000">Content</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">TextBlock</span>() { <span style="color:#800000">Text</span> = <span style="color:#ff0000">&quot;Impresi&#243;n finalizada&quot;</span> };<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">nw</span>.<span style="color:#800000">Show</span>(<span style="color:#a52a2a">1000</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">else</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">MessageBox</span>.<span style="color:#800000">Show</span>(<span style="color:#ff0000">&quot;Impresi&#243;n finalizada&quot;</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</div>
</p></div>
</p></div>
<h3>&#160;</h3>
<p>Si ejecutamos la aplicación Fuera del Navegador, la notificación sería mostrada (quitando el manejador de evento personalizado de nuestro código):</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/image2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://rdiazconcha.com/wp-content/uploads/2009/11/image_thumb2.png" width="504" height="344" /></a> </p>
<h3>Parametros.cs</h3>
<p>&#160;</p>
<p>La clase Parametros nos servirá para permitir al usuario que modifique los parámetros para la impresión, principalmente para el encabezado y el márgen izquierdo para el <strong>ItemsControl</strong>.&#160; El mecanismo seleccionado para este efecto será tener un ChildWindow (llamado Configuracion) el cual contenga un control de tipo DataForm; para permitir la edición de los valores se una manera sencilla y rápida.&#160; La siguiente figura muestra la caja de diálogo de configuración en acción:</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/image3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://rdiazconcha.com/wp-content/uploads/2009/11/image_thumb3.png" width="504" height="335" /></a> </p>
<p>Esto brinda al usuario una manera de poder cambiar algunos parámetros a la hora de imprimir.&#160; El resultado será el siguiente (impresión a un archivo .xps):</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/image4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://rdiazconcha.com/wp-content/uploads/2009/11/image_thumb4.png" width="504" height="108" /></a> </p>
<p>Pueden descargar el código de <a href="http://rdiazconcha.com/Files/Demo.SL4.ImpresionAvanzada.rar" target="_blank">aquí</a></p>
<p>Pueden ver el demo en vivo <a href="http://rdiazconcha.com/demos/SL4_Impresion_Avanzada/Demo.SL4.ImpresionMultipleTestPage.aspx" target="_blank">aquí</a> (requiere Silverlight 4)</p>
]]></content:encoded>
			<wfw:commentRss>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin-modularizacin-y-parmetros/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight 4 Beta &#8211; Impresi&#243;n de M&#250;ltiples P&#225;ginas</title>
		<link>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin-de-mltiples-pginas/</link>
		<comments>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin-de-mltiples-pginas/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 21:44:31 +0000</pubDate>
		<dc:creator>Rodrigo</dc:creator>
				<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[Impresion]]></category>
		<category><![CDATA[PrintDocument]]></category>

		<guid isPermaLink="false">http://rdiazconcha.com/?p=437</guid>
		<description><![CDATA[En el artículo anterior, vimos los fundamentos del modelo de clases para imprimir desde nuestras aplicaciones de Silverlight 4.&#160; Recordemos que la propiedad HasMorePages indica si nuestro documento tiene más páginas que imprimir; si HasMorePages es true entonces el evento PrintPage se disparará nuevamente hasta que HasMorePages sea false.
En este artículo veremos cómo aprovechar esta [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://rdiazconcha.com/?p=424" target="_blank">En el artículo anterior</a>, vimos los fundamentos del modelo de clases para imprimir desde nuestras aplicaciones de <a href="http://silverlight.net/getstarted/silverlight-4-beta/" target="_blank">Silverlight 4</a>.&#160; Recordemos que la propiedad HasMorePages indica si nuestro documento tiene más páginas que imprimir; si HasMorePages es true entonces el evento PrintPage se disparará nuevamente hasta que HasMorePages sea false.</p>
<p>En este artículo veremos cómo aprovechar esta característica para poder mandar a imprimir un catálogo de artículos (en este ejemplo, un catálogo de álbumes de música) ya que este escenario será bastante común en las Aplicaciones de Línea de Negocio que construyamos con Silverlight 4.</p>
<h2>Escenario</h2>
<p>Dado el siguiente escenario:</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/11.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="1" border="0" alt="1" src="http://rdiazconcha.com/wp-content/uploads/2009/11/1_thumb1.png" width="504" height="458" /></a> </p>
<p>Podemos apreciar que tenemos un ListBox el cual contiene una lista de álbumes de música.&#160; Además estamos cambiando el DataTemplate para poder mostrar de una mejor manera cada uno de esos elementos.&#160; El siguiente fragmento de código muestra el Xaml de la aplicación:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:80dfaa79-a876-473e-a1be-bec867ea2475" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">&lt;</span><span style="color:#a31515">UserControl</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Class</span><span style="color:#0000ff">=&quot;Demo.SL4.ImpresionMultiple.MainPage&quot;</span><br /> &#160;&#160;&#160;<span style="color:#ff0000"> xmlns</span><span style="color:#0000ff">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span><br /> &#160;&#160;&#160;<span style="color:#ff0000"> xmlns</span><span style="color:#0000ff">:</span><span style="color:#ff0000">x</span><span style="color:#0000ff">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span><br /> &#160;&#160;&#160;<span style="color:#ff0000"> xmlns</span><span style="color:#0000ff">:</span><span style="color:#ff0000">d</span><span style="color:#0000ff">=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;</span><br /> &#160;&#160;&#160;<span style="color:#ff0000"> xmlns</span><span style="color:#0000ff">:</span><span style="color:#ff0000">mc</span><span style="color:#0000ff">=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span><br /> <span style="color:#ff0000"> xmlns</span><span style="color:#0000ff">:</span><span style="color:#ff0000">local</span><span style="color:#0000ff">=&quot;clr-namespace:Demo.SL4.ImpresionMultiple&quot;</span><br /> &#160;&#160;&#160;<span style="color:#ff0000"> mc</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Ignorable</span><span style="color:#0000ff">=&quot;d&quot;</span><br /> &#160;&#160;&#160;<span style="color:#ff0000"> d</span><span style="color:#0000ff">:</span><span style="color:#ff0000">DesignHeight</span><span style="color:#0000ff">=&quot;300&quot;</span><span style="color:#ff0000"> d</span><span style="color:#0000ff">:</span><span style="color:#ff0000">DesignWidth</span><span style="color:#0000ff">=&quot;500&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">UserControl.Resources</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">local</span><span style="color:#0000ff">:</span><span style="color:#a31515">Albumes</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;albumes&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">DataTemplate</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;VistaDataTemplate&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Grid</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Grid.ColumnDefinitions</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ColumnDefinition</span><span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;100&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ColumnDefinition</span><span style="color:#0000ff"> /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Grid.ColumnDefinitions</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Border</span><span style="color:#ff0000"> BorderBrush</span><span style="color:#0000ff">=&quot;Black&quot;</span><span style="color:#ff0000"> Padding</span><span style="color:#0000ff">=&quot;3&quot;</span><span style="color:#ff0000"> BorderThickness</span><span style="color:#0000ff">=&quot;2&quot;</span><span style="color:#ff0000"> CornerRadius</span><span style="color:#0000ff">=&quot;10&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Image</span><span style="color:#ff0000"> Source</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:#ff0000"> Foto}</span><span style="color:#0000ff">&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;100&quot; /&gt;</span><span style="color:#ff0000"> </span><br /> &#160;&#160;&#160;&#160;<span style="color:#ff0000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Border</span><span style="color:#0000ff">&gt;</span><span style="color:#ff0000"></span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">StackPanel</span><span style="color:#ff0000"> Grid.Column</span><span style="color:#0000ff">=&quot;1&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> Text</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:#ff0000"> Titulo}</span><span style="color:#0000ff">&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> FontSize</span><span style="color:#0000ff">=&quot;20&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> Text</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:#ff0000"> Banda}</span><span style="color:#0000ff">&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">StackPanel</span><span style="color:#0000ff">&gt;</span><span style="color:#ff0000"></span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Grid</span><span style="color:#0000ff">&gt;</span><span style="color:#ff0000"></span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">DataTemplate</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">UserControl.Resources</span><span style="color:#0000ff">&gt;</span></p>
<p> &#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Grid</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Name</span><span style="color:#0000ff">=&quot;LayoutRoot&quot;</span><span style="color:#ff0000"> Background</span><span style="color:#0000ff">=&quot;White&quot;</span><span style="color:#ff0000"> DataContext</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">StaticResource</span><span style="color:#ff0000"> albumes}</span><span style="color:#0000ff">&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Grid.ColumnDefinitions</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ColumnDefinition</span><span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;Auto&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ColumnDefinition</span><span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;*&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Grid.ColumnDefinitions</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ListBox</span><span style="color:#ff0000"> Grid.Column</span><span style="color:#0000ff">=&quot;1&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Left&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Margin</span><span style="color:#0000ff">=&quot;5&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Name</span><span style="color:#0000ff">=&quot;listBox1&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> VerticalAlignment</span><span style="color:#0000ff">=&quot;Top&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;450&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Height</span><span style="color:#0000ff">=&quot;450&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> ItemsSource</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">Binding}</span><span style="color:#0000ff">&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> ItemTemplate</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">StaticResource</span><span style="color:#ff0000"> VistaDataTemplate}</span><span style="color:#0000ff">&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">StackPanel</span><span style="color:#ff0000"> Height</span><span style="color:#0000ff">=&quot;228&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Left&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Name</span><span style="color:#0000ff">=&quot;stackPanel1&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> VerticalAlignment</span><span style="color:#0000ff">=&quot;Top&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Margin</span><span style="color:#0000ff">=&quot;5&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Button</span><span style="color:#ff0000"> Height</span><span style="color:#0000ff">=&quot;50&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Name</span><span style="color:#0000ff">=&quot;button1&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;120&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Button.Content</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">StackPanel</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Center&quot;&gt;</span><span style="color:#ff0000">Imprimir</span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">TextBlock</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Center&quot;&gt;</span><span style="color:#ff0000">ListBox</span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">TextBlock</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">StackPanel</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Button.Content</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Button</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Button</span><span style="color:#ff0000"> Height</span><span style="color:#0000ff">=&quot;50&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Name</span><span style="color:#0000ff">=&quot;button2&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;120&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Button.Content</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">StackPanel</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Center&quot;&gt;</span><span style="color:#ff0000">Impresi&#243;n</span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">TextBlock</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Center&quot;&gt;</span><span style="color:#ff0000">Personalizada</span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">TextBlock</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">StackPanel</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Button.Content</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Button</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">StackPanel</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Grid</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">&lt;/</span><span style="color:#a31515">UserControl</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
<h3>&#160;</h3>
<h3>Impresión de Elementos</h3>
<p>La propiedad PageVisual del objeto de argumentos en el evento PrintPage indica ultimadamente cuál es el contenido a imprimir, siendo este contenido parte del árbol de Xaml o no.&#160; Es decir que, si establecemos el ListBox como el PageVisual…</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:48a13023-b3c3-4283-81b9-5a84e6d1c0a1" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#800000">pd</span>.<span style="color:#800000">PrintPage</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Establecemos el ListBox como el PageVisual</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">a</span>.<span style="color:#800000">PageVisual</span> = <span style="color:#800000">listBox1</span>;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;};</div>
</p></div>
</p></div>
<p>…nuestro resultado de impresión será el siguiente (impresión en XPS):</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/21.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="2" border="0" alt="2" src="http://rdiazconcha.com/wp-content/uploads/2009/11/2_thumb1.png" width="504" height="352" /></a> </p>
<p>Tal vez este mecanismo no sea el adecuado para presentar correctamente la impresión, ya que se imprime el elemento tal y como aparece en pantalla; es decir, una “fotografía” del elemento (incluyendo las barras de scroll, los bordes, etc.).</p>
<h3>Impresión Personalizada</h3>
<p>Podemos lograr un grado más alto de personalización creando de manera dinámica el contenido Xaml que deseamos imprimir.&#160; En el caso de este ejemplo generaremos dinámicamente un Canvas que contenga un encabezado, además de un ItemsControl que despliegue todos y cada uno de los elementos a imprimir. Además de lo anterior, nos aseguraremos que la impresión permita múltiples páginas y que se impriman los registros en cada página de manera correcta.</p>
<h4>Canvas</h4>
<p>Tal como en el artículo anterior, creamos un Canvas que tenga el mismo tamaño del área de impresión disponible, según la impresora seleccionada:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:eb7447ac-18a6-4e23-9706-7b365ce4c873" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px;"><span style="color:#800000">pd</span>.<span style="color:#800000">PrintPage</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt;<br /> {<br /> &#160;&#160;&#160;&#160;<span style="color:#800000">canvas</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Canvas</span>() { <span style="color:#800000">Width</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Width</span>, <span style="color:#800000">Height</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Height</span> };<br /> &#160;&#160;&#160;&#160;&#8230;</div>
</p></div>
</p></div>
</p>
<h4>Encabezado</h4>
<p>Para cada página a imprimir, se agregará un encabezado que contenga un logo a la izquierda, el título del encabezado centrado, y un texto que indique el número de página actual.</p>
<p><font color="#ff0000">Nota: Hay un issue con Silverlight 4 Beta que impide la impresión de imágenes creadas dinámicamente.</font></p>
<h4>Lista de Elementos</h4>
<p>Esta es la parte más importante de este escenario.&#160; Necesitamos soportar el hecho de que habrá más de una página al momento de imprimir.&#160; En el ejemplo de este artículo tenemos un objeto llamado Albumes (List&lt;Album&gt;) el cual tiene un total de 42 álbumes.</p>
<p>Para la impresión personalizada crearemos otro <strong>DataTemplate</strong> llamado ImpresionDataTemplate el cual desplegará de manera diferente los mismos elementos de la pantalla.&#160; Por ejemplo, quitaremos la imagen de cada álbum, además desplegaremos los elementos dentro de un Grid con cuatro columnas.&#160; Las columnas que mostraremos serán Titulo, Banda, Inventario y FechaLanzamiento.&#160; El siguiente fragmento de Xaml muestra el DataTemplate para la impresión:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e94965c4-febe-4694-830d-f52254ea2af8" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">&lt;</span><span style="color:#a31515">DataTemplate</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Key</span><span style="color:#0000ff">=&quot;ImpresionDataTemplate&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Grid</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Grid.ColumnDefinitions</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ColumnDefinition</span><span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;150&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ColumnDefinition</span><span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;120&quot;/&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ColumnDefinition</span><span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;100&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">ColumnDefinition</span><span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;100&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Grid.ColumnDefinitions</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Grid.RowDefinitions</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">RowDefinition</span><span style="color:#ff0000"> Height</span><span style="color:#0000ff">=&quot;40&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Grid.RowDefinitions</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> Text</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:#ff0000"> Titulo}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> FontWeight</span><span style="color:#0000ff">=&quot;Bold&quot;</span><span style="color:#ff0000"> Grid.Column</span><span style="color:#0000ff">=&quot;0&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> Text</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:#ff0000"> Banda}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> Grid.Column</span><span style="color:#0000ff">=&quot;1&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> Text</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:#ff0000"> Inventario}</span><span style="color:#0000ff">&quot;</span><span style="color:#ff0000"> Grid.Column</span><span style="color:#0000ff">=&quot;2&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> Text</span><span style="color:#0000ff">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:#ff0000"> FechaLanzamiento}</span><span style="color:#0000ff">&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Grid.Column</span><span style="color:#0000ff">=&quot;3&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Grid</span><span style="color:#0000ff">&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">DataTemplate</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
<p>En el método de creación del listado por página (método CrearLista()) crearemos un ItemsControl el cual su DataTemplate sea el descrito arriba.&#160; Asimismo, estableceremos como su fuente de datos (propiedad ItemsSource) <em>únicamente los registros que quepan en la página actual</em>.&#160; Con los métodos de extensión Skip() y Take() podemos fácilmente leer cierto número de registros del objeto Albumes según la página actual.&#160; El siguiente fragmento de código muestra este concepto:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c9f689df-357a-4928-8f2a-eaf8b4b5e5c8" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">void</span> <span style="color:#800000">CrearLista</span>(<span style="color:#0000ff">int</span> <span style="color:#800000">pagina</span>)<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">ItemsControl</span> <span style="color:#800000">lista</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">ItemsControl</span>();<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">lista</span>.<span style="color:#800000">Width</span> = <span style="color:#800000">canvas</span>.<span style="color:#800000">Width</span>;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">lista</span>.<span style="color:#800000">ItemTemplate</span> = <span style="color:#0000ff">this</span>.<span style="color:#800000">Resources</span>[<span style="color:#ff0000">&quot;ImpresionDataTemplate&quot;</span>] <span style="color:#0000ff">as</span> <span style="color:#2b91af">DataTemplate</span>;</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Obtiene &#250;nicamente los registros que van en la p&#225;gina actual</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//identificada en el argumento &#39;pagina&#39; del m&#233;todo</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">lista</span>.<span style="color:#800000">ItemsSource</span> = <span style="color:#800000">fuenteDatos</span>.<span style="color:#800000">Skip</span>((<span style="color:#0000ff">int</span>)(<span style="color:#800000">pagina</span>*<span style="color:#800000">lineasPorPagina</span>)).<span style="color:#800000">Take</span>((<span style="color:#0000ff">int</span>)<span style="color:#800000">lineasPorPagina</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Agregamos la lista al Canvas</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">canvas</span>.<span style="color:#800000">Children</span>.<span style="color:#800000">Add</span>(<span style="color:#800000">lista</span>);</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Respetamos el alto del encabezado</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">Canvas</span>.<span style="color:#800000">SetTop</span>(<span style="color:#800000">lista</span>, <span style="color:#800000">altoEncabezado</span>);</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Incrementamos el contador</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">paginaActual</span>++;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</div>
</p></div>
</p></div>
</p>
<p>Finalmente, en el evento PrintPage invocamos los métodos de creación del encabezado y el de la creación de elementos por página:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:207e5298-d453-4dd3-ad4d-d260db18c191" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap">&#8230;<br /> <span style="color:#800000">CrearEncabezado</span>(<span style="color:#ff0000">&quot;Lista de &#193;lbumes&quot;</span>);</p>
<p> <span style="color:#800000">CrearLista</span>(<span style="color:#800000">paginaActual</span>);</p>
<p> <span style="color:#800000">a</span>.<span style="color:#800000">PageVisual</span> = <span style="color:#800000">canvas</span>;</p>
<p> <span style="color:#800000">a</span>.<span style="color:#800000">HasMorePages</span> = !(<span style="color:#800000">paginaActual</span> == <span style="color:#800000">totalPaginas</span>);<br /> &#8230;</div>
</p></div>
</p></div>
<p>Muy importante en el código anterior es el valor de la propiedad HasMorePages.&#160; En este ejemplo estamos indicando si hay más páginas siempre y cuando el contador (variable <font face="Courier New">paginaActual</font>) sea diferente al número total de páginas (variable <font face="Courier New">totalPaginas</font>).&#160; Recordemos que si HasMorePages es true, el evento PrintPage nuevamente se disparará, provocando así la impresión de todas y cada una de las páginas del listado.</p>
<p>El resultado:</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/31.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Impresión de dos páginas en Silverlight 4 Beta" border="0" alt="Impresión de dos páginas en Silverlight 4 Beta" src="http://rdiazconcha.com/wp-content/uploads/2009/11/3_thumb1.png" width="524" height="314" /></a> </p>
<p>Cabe mencionar que el DataTemplate específico para la impresión no es el único mecanismo para hacerlo de manera personalizada, también pudimos haber hecho un StackPanel o Grid de manera dinámica y leer programáticamente (un foreach tal vez) cada Album de Albumes programáticamente e ir agregando al contenedor los elementos manualmente.</p>
<h3>Resumen</h3>
<p>A través de la propiedad HasMorePages tenemos el control para indicar si nuestro documento a imprimir tiene múltiples páginas.&#160; Asimismo vimos que un DataTemplate específico para la impresión es un mecanismo que nos facilita la manera en la que queremos que se imprima una lista de elementos en un reporte.</p>
<p>Pueden descargar el código fuente <a href="http://rdiazconcha.com/Files/Demo.SL4.ImpresionMultiple.rar" target="_blank">aquí</a></p>
<p>Pueden ver el demo en vivo <a href="http://rdiazconcha.com/demos/SL4_Impresion_Multiple/Demo.SL4.ImpresionMultipleTestPage.aspx" target="_blank">aquí</a> (requieres <a href="http://silverlight.net/getstarted/silverlight-4-beta/" target="_blank">Silverlight 4 Beta</a> instalado)</p>
]]></content:encoded>
			<wfw:commentRss>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin-de-mltiples-pginas/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Silverlight 4 Beta &#8211; Impresi&#243;n</title>
		<link>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin/</link>
		<comments>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 07:44:12 +0000</pubDate>
		<dc:creator>Rodrigo</dc:creator>
				<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[Impresion]]></category>
		<category><![CDATA[PrintDocument]]></category>

		<guid isPermaLink="false">http://rdiazconcha.com/?p=424</guid>
		<description><![CDATA[Una de las características más esperadas y solicitadas por la comunidad de Silverlight es el soporte de impresión dentro de las aplicaciones, esto es, la capacidad de poder mandar a imprimir el contenido que estemos viendo en la aplicación ya sea una lista de datos de un DataGrid o ListBox o simplemente tomarle una “foto” [...]]]></description>
			<content:encoded><![CDATA[<p>Una de las características más esperadas y solicitadas por la comunidad de Silverlight es el soporte de impresión dentro de las aplicaciones, esto es, la capacidad de poder mandar a imprimir el contenido que estemos viendo en la aplicación ya sea una lista de datos de un DataGrid o ListBox o simplemente tomarle una “foto” a la pantalla y enviarla a papel o alguna de las impresoras virtuales instaladas en nuestros equipos (por ejemplo: OneNote, XPS, PDF, etc.).&#160; Silverlight 4 Beta incluye esta característica a través de un modelo de clases muy sencillo.&#160; De hecho este modelo me recuerda al modelo de impresión que tenemos en las aplicaciones Windows Forms desde hace ya mucho tiempo.</p>
<h2>Clase System.Windows.Printing.PrintDocument</h2>
<p>Las clases para el soporte de impresión dentro de Silverlight han sido incorporadas al espacio de nombres <strong>System.Windows.Printing</strong>, el cual contiene entre otras cosas la clase PrintDocument.&#160; Esta clase PrintDocument es la clase responsable de mandar a imprimir el contenido que deseemos.</p>
<p>La clase PrintDocument no expone alguna propiedad para que podamos determinar el contenido que deseemos imprimir.&#160; No obstante, el método Print() al ser ejecutado abre la caja de diálogo de impresión de nuestro sistema operativo.</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="1" border="0" alt="1" src="http://rdiazconcha.com/wp-content/uploads/2009/11/1_thumb.png" width="500" height="452" /></a> </p>
<p>Una vez seleccionada la impresora destino (física o virtual), disparará la siguiente secuencia de eventos:</p>
<ol>
<li>StartPrint </li>
<li>PrintPage </li>
<li>EndPrint </li>
</ol>
<h4>Evento StartPrint</h4>
<p>El evento StartPrint se dispara una vez cerrada la caja de diálogo de selección de impresora.&#160; Inmediatamente después se dispara el siguiente evento: PrintPage.</p>
<h4>Evento PrintPage</h4>
<p>Seguido del evento StartPrint se disparará el evento PrintPage.&#160; Este es el evento más importante en el modelo de impresión ya que es en los argumentos donde debemos establecer efectivamente <em>el contenido</em> que deseamos imprimir.&#160; Esto es definido en la propiedad <strong>PageVisual</strong> que es de tipo UIElement.&#160; Ya que es de tipo UIElement podemos establecer como contenido en realidad cualquier cosa: ya sea que esté presente en el árbol de Xaml o que sea contenido creado de manera dinámica (a través de código o usando XamlReader.Load(), etc.).&#160; Por ejemplo, si deseamos tomar una “fotografía” a la aplicación actual e imprimirla, podemos establecer como PageVisual el objeto contenedor raíz (ej. LayoutRoot).&#160; O de lo contrario, si queremos crear una impresión personalizada podemos generar contenedores y elementos de manera dinámica tal y como lo muestra este artículo.</p>
<p>Además de la propiedad <strong>PageVisual</strong> contamos con un par de propiedades: HasMorePages y PrintableArea.</p>
<p><strong>HasMorePages</strong> es una propiedad que indica si todavía hay más páginas que imprimir, por ejemplo en el caso de documentos largos, listas, catálogos etc.&#160; Si establecemos esta propiedad a true, entonces el evento PrintPage volverá a dispararse.</p>
<p><strong>PrintableArea</strong> indica el tamaño que tiene el área de impresión según la impresora seleccionada.&#160; Por ejemplo no es el mismo tamaño si mandamos a imprimir a OneNote que a un PDF o a una impresora física.&#160; Esta propiedad es de tipo Size y nos servirá también para calcular márgenes, posiciones de elementos, etc.</p>
<p>Cabe mencionar que, si la impresora seleccionada requiere todavía de la interacción del usuario, esta continuará (para indicar el nombre de un archivo .xps o .pdf por ejemplo).</p>
<h4>Evento EndPrint</h4>
<p>El evento EndPrint se disparará una vez terminado el proceso de impresión.</p>
<h2>Ejemplo</h2>
<p>Comencemos con crear un nuevo proyecto de Silverlight 4 a través de Visual Studio 2010 (Beta 2 es la versión más reciente en el momento de escribir este artículo).&#160; A este proyecto le pondremos de nombre Demo.SL4.Impresion.&#160; Visual Studio creará la solución junto con el proyecto adecuado tal y como lo esperamos.</p>
<p>Agregaremos a nuestro MainPage.xaml el siguiente código:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:92db6a53-f699-4099-aaf0-4fd05b2a6eed" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Grid</span><span style="color:#ff0000"> x</span><span style="color:#0000ff">:</span><span style="color:#ff0000">Name</span><span style="color:#0000ff">=&quot;LayoutRoot&quot;</span><span style="color:#ff0000"> Background</span><span style="color:#0000ff">=&quot;White&quot;&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Button</span><span style="color:#ff0000"> Content</span><span style="color:#0000ff">=&quot;Una p&#225;gina&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Height</span><span style="color:#0000ff">=&quot;47&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Left&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Margin</span><span style="color:#0000ff">=&quot;12,12,0,0&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Name</span><span style="color:#0000ff">=&quot;button1&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> VerticalAlignment</span><span style="color:#0000ff">=&quot;Top&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;115&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Click</span><span style="color:#0000ff">=&quot;button1_Click&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">Button</span><span style="color:#ff0000"> Content</span><span style="color:#0000ff">=&quot;Muchas p&#225;ginas&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Height</span><span style="color:#0000ff">=&quot;47&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Left&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Margin</span><span style="color:#0000ff">=&quot;133,12,0,0&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Name</span><span style="color:#0000ff">=&quot;button2&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> VerticalAlignment</span><span style="color:#0000ff">=&quot;Top&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;115&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Click</span><span style="color:#0000ff">=&quot;button2_Click&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;</span><span style="color:#a31515">TextBlock</span><span style="color:#ff0000"> Height</span><span style="color:#0000ff">=&quot;30&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> HorizontalAlignment</span><span style="color:#0000ff">=&quot;Left&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Margin</span><span style="color:#0000ff">=&quot;14,70,0,0&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Name</span><span style="color:#0000ff">=&quot;textBlock1&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> VerticalAlignment</span><span style="color:#0000ff">=&quot;Top&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> Width</span><span style="color:#0000ff">=&quot;372&quot;</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000"> FontSize</span><span style="color:#0000ff">=&quot;14&quot; /&gt;</span><br /> &#160;&#160;&#160;&#160;<span style="color:#ff0000"></span><span style="color:#0000ff">&lt;/</span><span style="color:#a31515">Grid</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
<p>Los botones nos servirán para mandar a imprimir por medio de la ejecución del método Print() de la clase PrintDocument y el TextBlock nos servirá para identificar el estatus de la impresión.</p>
<p>En el clic del primer botón crearemos una nueva instancia de PrintDocument y estableceremos su propiedad DocumentName de manera adecuada.&#160; El valor de esta propiedad será lo que aparezca en la ventana de estado de la impresora.</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8fd0fefa-decf-4746-9c39-c4f6fd0f1120" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px;"><span style="color:#2b91af">PrintDocument</span> <span style="color:#800000">pd</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">PrintDocument</span>() { <span style="color:#800000">DocumentName</span> = <span style="color:#ff0000">&quot;Ejemplo de Impresi&#243;n&quot;</span> };</div>
</p></div>
</p></div>
</p>
<p>Ahora bien, manejaremos el evento PrintPage.&#160; En este caso usaremos una expresión lambda pero pudiera ser también un método:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1f5e9a05-f129-4aba-a68f-4034729c5273" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px;"><span style="color:#800000">pd</span>.<span style="color:#800000">PrintPage</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//canvas es una variable a nivel de clase</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Establecemos el ancho y alto de canvas a usando la propiedad PrintableArea</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">canvas</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Canvas</span>() { <span style="color:#800000">Width</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Width</span>, <span style="color:#800000">Height</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Height</span> };</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Agregamos el t&#237;tulo deseado para la p&#225;gina</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">CrearTitulo</span>(<span style="color:#ff0000">&quot;Esto es una Prueba de Impresi&#243;n&quot;</span>);</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Establecemos el contenido en la propiedad PageVisual</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">a</span>.<span style="color:#800000">PageVisual</span> = <span style="color:#800000">canvas</span>;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;};</div>
</p></div>
</p></div>
<p>En el código anterior suceden varias cosas:</p>
<p>Creamos un objeto de tipo Canvas, el cual nos servirá como lienzo para dibujar en él los elementos visuales que queramos imprimir (en este ejemplo el contenido será un TextBlock creado en el método CrearTitulo()).&#160; El ancho y alto del objeto Canvas los establecemos a partir de la propiedad PrintableArea, la cual, nos indica el tamaño de impresión según la impresora seleccionada (por ejemplo, el área de impresión es diferente en OneNote que un .xps).</p>
<p>Finalmente, establecemos este contenido que creamos dinámicamente en la propiedad PageVisual.&#160; Esta propiedad indica el contenido visual que vamos a imprimir.&#160; El siguiente fragmento de código contiene todo el código completo de este primer ejemplo:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f3453abf-9326-4160-98c3-a3bd1f3601b1" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#2b91af">Canvas</span> <span style="color:#800000">canvas</span> = <span style="color:#0000ff">null</span>;<br /> <span style="color:#0000ff">int</span> <span style="color:#800000">pagina</span> = <span style="color:#a52a2a">0</span>;</p>
<p> <span style="color:#0000ff">private</span> <span style="color:#0000ff">void</span> <span style="color:#800000">button1_Click</span>(<span style="color:#0000ff">object</span> <span style="color:#800000">sender</span>, <span style="color:#2b91af">RoutedEventArgs</span> <span style="color:#800000">e</span>)<br /> {&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<br /> &#160;&#160;&#160;&#160;<span style="color:#2b91af">PrintDocument</span> <span style="color:#800000">pd</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">PrintDocument</span>() { <span style="color:#800000">DocumentName</span> = <span style="color:#ff0000">&quot;Ejemplo de Impresi&#243;n&quot;</span> };<br /> &#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">PrintPage</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt;<br /> &#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//canvas es una variable a nivel de clase</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Establecemos el ancho y alto de canvas a usando la propiedad PrintableArea</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">canvas</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Canvas</span>() { <span style="color:#800000">Width</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Width</span>, <span style="color:#800000">Height</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Height</span> };</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Agregamos el t&#237;tulo deseado para la p&#225;gina</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">CrearTitulo</span>(<span style="color:#ff0000">&quot;Esto es una Prueba de Impresi&#243;n&quot;</span>);</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Establecemos el contenido en la propiedad PageVisual</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">a</span>.<span style="color:#800000">PageVisual</span> = <span style="color:#800000">canvas</span>;<br /> &#160;&#160;&#160;&#160;};<br /> &#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">StartPrint</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt; <span style="color:#800000">textBlock1</span>.<span style="color:#800000">Text</span> = <span style="color:#ff0000">&quot;Iniciando impresi&#243;n&#8230;&quot;</span>;<br /> &#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">EndPrint</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt; <span style="color:#800000">textBlock1</span>.<span style="color:#800000">Text</span> = <span style="color:#ff0000">&quot;Impresi&#243;n finalizada&quot;</span>;<br /> &#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">Print</span>();<br /> }</p>
<p> <span style="color:#0000ff">void</span> <span style="color:#800000">CrearTitulo</span>(<span style="color:#0000ff">string</span> <span style="color:#800000">titulo</span>)<br /> {<br /> &#160;&#160;&#160;&#160;<span style="color:#008000">//Crea un TextBlock que servir&#225; como encabezado</span><br /> &#160;&#160;&#160;&#160;<span style="color:#2b91af">TextBlock</span> <span style="color:#800000">contenido</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">TextBlock</span>() { <span style="color:#800000">Text</span> = <span style="color:#800000">titulo</span>, <span style="color:#800000">FontSize</span> = <span style="color:#a52a2a">20</span> };<br /> &#160;&#160;&#160;&#160;<span style="color:#800000">contenido</span>.<span style="color:#800000">Effect</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">DropShadowEffect</span>();<br /> &#160;&#160;&#160;&#160;<span style="color:#800000">canvas</span>.<span style="color:#800000">Children</span>.<span style="color:#800000">Add</span>(<span style="color:#800000">contenido</span>);<br /> &#160;&#160;&#160;&#160;<span style="color:#2b91af">Canvas</span>.<span style="color:#800000">SetLeft</span>(<span style="color:#800000">contenido</span>, <span style="color:#800000">canvas</span>.<span style="color:#800000">Width</span> / <span style="color:#a52a2a">2</span> &#8211; (<span style="color:#800000">contenido</span>.<span style="color:#800000">ActualWidth</span> / <span style="color:#a52a2a">2</span>));<br /> }</div>
</p></div>
</p></div>
<p>Y el resultado, tanto en un archivo .xps como en OneNote:</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="2" border="0" alt="2" src="http://rdiazconcha.com/wp-content/uploads/2009/11/2_thumb.png" width="504" height="110" /></a> <a href="http://rdiazconcha.com/wp-content/uploads/2009/11/3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="3" border="0" alt="3" src="http://rdiazconcha.com/wp-content/uploads/2009/11/3_thumb.png" width="504" height="206" /></a> </p>
<p>Noten cómo también soporta Efectos visuales.&#160; Tal es el caso del DropShadowEffect que se está estableciendo en el título.</p>
<p>En el segundo botón tendremos una ligera variante: mandaremos a imprimir varias páginas.&#160; Esto es logrado a partir de establecer la propiedad HasMorePages = true.&#160; En el caso de este ejemplo hemos establecido una variable a nivel de clase llamada pagina que nos servirá de bandera.&#160; Esta bandera la estaremos revisando hasta que se completen 4 páginas en total.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6fe52f36-9efd-4817-bf02-87481eee5f9f" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px;"><span style="color:#800000">pd</span>.<span style="color:#800000">PrintPage</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">canvas</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Canvas</span>() { <span style="color:#800000">Width</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Width</span>, <span style="color:#800000">Height</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Height</span> };<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">CrearTitulo</span>(<span style="color:#ff0000">&quot;Esto es una Prueba de Impresi&#243;n&quot;</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">CrearSubtitulo</span>(<span style="color:#0000ff">string</span>.<span style="color:#800000">Format</span>(<span style="color:#ff0000">&quot;P&#225;gina: {0}&quot;</span>, <span style="color:#800000">pagina</span>+<span style="color:#a52a2a">1</span>));<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">a</span>.<span style="color:#800000">PageVisual</span> = <span style="color:#800000">canvas</span>;</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">a</span>.<span style="color:#800000">HasMorePages</span> = !(<span style="color:#800000">pagina</span> == <span style="color:#a52a2a">3</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">pagina</span>++;</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;};</p></div>
</p></div>
</p></div>
<p>Cabe recordar que al establecer la propiedad HasMorePages a true, el evento PrintPage se vuelve a disparar, de esta manera, podemos tener el control del contenido a imprimir <em>en cada página</em>.&#160; El código completo se muestra a continuación:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4c386e98-f054-4b29-a10a-3cad193e87f3" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">void</span> <span style="color:#800000">CrearSubtitulo</span>(<span style="color:#0000ff">string</span> <span style="color:#800000">titulo</span>)<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000">//Crea un TextBlock que servir&#225; como subtitulo</span><br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">TextBlock</span> <span style="color:#800000">contenido</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">TextBlock</span>() { <span style="color:#800000">Text</span> = <span style="color:#800000">titulo</span>, <span style="color:#800000">FontSize</span> = <span style="color:#a52a2a">14</span> };<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">canvas</span>.<span style="color:#800000">Children</span>.<span style="color:#800000">Add</span>(<span style="color:#800000">contenido</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">Canvas</span>.<span style="color:#800000">SetLeft</span>(<span style="color:#800000">contenido</span>, <span style="color:#800000">canvas</span>.<span style="color:#800000">Width</span> / <span style="color:#a52a2a">2</span> &#8211; (<span style="color:#800000">contenido</span>.<span style="color:#800000">ActualWidth</span> / <span style="color:#a52a2a">2</span>));<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">Canvas</span>.<span style="color:#800000">SetTop</span>(<span style="color:#800000">contenido</span>, <span style="color:#a52a2a">40</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff">private</span> <span style="color:#0000ff">void</span> <span style="color:#800000">button2_Click</span>(<span style="color:#0000ff">object</span> <span style="color:#800000">sender</span>, <span style="color:#2b91af">RoutedEventArgs</span> <span style="color:#800000">e</span>)<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af">PrintDocument</span> <span style="color:#800000">pd</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">PrintDocument</span>() { <span style="color:#800000">DocumentName</span> = <span style="color:#ff0000">&quot;Ejemplo de Impresi&#243;n&quot;</span> };<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">PrintPage</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">canvas</span> = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Canvas</span>() { <span style="color:#800000">Width</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Width</span>, <span style="color:#800000">Height</span> = <span style="color:#800000">a</span>.<span style="color:#800000">PrintableArea</span>.<span style="color:#800000">Height</span> };<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">CrearTitulo</span>(<span style="color:#ff0000">&quot;Esto es una Prueba de Impresi&#243;n&quot;</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">CrearSubtitulo</span>(<span style="color:#0000ff">string</span>.<span style="color:#800000">Format</span>(<span style="color:#ff0000">&quot;P&#225;gina: {0}&quot;</span>, <span style="color:#800000">pagina</span>+<span style="color:#a52a2a">1</span>));<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">a</span>.<span style="color:#800000">PageVisual</span> = <span style="color:#800000">canvas</span>;</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">a</span>.<span style="color:#800000">HasMorePages</span> = !(<span style="color:#800000">pagina</span> == <span style="color:#a52a2a">3</span>);<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">pagina</span>++;</p>
<p> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;};<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">StartPrint</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt; <span style="color:#800000">textBlock1</span>.<span style="color:#800000">Text</span> = <span style="color:#ff0000">&quot;Iniciando impresi&#243;n&#8230;&quot;</span>;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">EndPrint</span> += (<span style="color:#800000">s</span>, <span style="color:#800000">a</span>) =&gt; <span style="color:#800000">textBlock1</span>.<span style="color:#800000">Text</span> = <span style="color:#ff0000">&quot;Impresi&#243;n finalizada&quot;</span>;<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000">pd</span>.<span style="color:#800000">Print</span>();<br /> &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</div>
</p></div>
</p></div>
<p>El resultado final de la impresión de este segundo botón se muestra en la siguiente figura (las flechas rojas se añadieron posteriormente para indicar el número de cada página):</p>
<p><a href="http://rdiazconcha.com/wp-content/uploads/2009/11/4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="4" border="0" alt="4" src="http://rdiazconcha.com/wp-content/uploads/2009/11/4_thumb.png" width="504" height="158" /></a> </p>
<h2>Resumen</h2>
<p>Silverlight 4 Beta incluye la funcionalidad de impresión, a través de la clase System.Windows.Printing.PrintDocument.&#160; Esta clase incluye el método Print(), que al ser ejecutado dispara una serie de eventos, entre ellos el evento PrintPage en donde podemos establecer el contenido deseado para imprimir, conocer el tamaño del área de impresión así como determinar si hay más páginas para imprimir (para escenarios de documentos de más de 1 página).</p>
<p>Puedes descargar el código <a href="http://rdiazconcha.com/Files/Demo.SL4.Impresion.rar" target="_blank">aquí</a>.</p>
<p>Puedes ver el demo en vivo <a href="http://rdiazconcha.com/demos/SL4_Impresion/Demo.SL4.ImpresionTestPage.aspx" target="_blank">aquí</a> (requiere Silverlight 4 Beta instalado).</p>
]]></content:encoded>
			<wfw:commentRss>http://rdiazconcha.com/2009/11/silverlight-4-beta-impresin/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

