Categories: WPF Posted by toddpi314 on 3/14/2009 12:36 PM | Comments (0)

The XPS FixedDocument could become the new standard in portable documents. We just need few developer’s to build a strong API and get Microsoft to ramp up!

In the meantime, here is a little help on Printing your [Fixed/Flow]Document:

Creating a Document

First of all, you must create a Package before a document can come into play. This is not clear, since the XPS API has been updated between the Beta, 3.0, and 3.5 release.

Here is one of the most common examples online (http://www.ericsink.com/wpf3d/B_Printing.html):

   1: public static void PrintXPS(FixedDocument doc)
   2: {
   3:     string tempPath = "";
   4:     try
   5:     {
   6:         tempPath = System.IO.Path.GetTempFileName();
   7:         Package package = Package.Open(tempPath);
   8:         XpsDocument xpsd = new XpsDocument(package);
   9:         XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
  10:         xw.Write(doc);
  11:         xpsd.Close();
  12:  
  13:         PrintXPS(tempPath); 
  14:     }
  15:     finally
  16:     {
  17:         if (tempPath.Length > 0)
  18:             System.IO.File.Delete(tempPath);
  19:     }
  20: }

Of course, this now throws: Archive file cannot be size 0.

Digging through the XPSCreate Sample shows a quick workaround:

Excert from XpsCreate.cs in XpsCreate Sample Provided by MS:

   1: if (File.Exists(packageName))
   2:     File.Delete(packageName);
   3:  
   4: // Create an XpsDocument package (without PrintTicket).
   5: using (Package package = Package.Open(packageName))
   6: {
   7:     XpsDocument xpsDocument = new XpsDocument(package);
   8:  
   9:     // Add the package content (false=without PrintTicket).
  10:     AddPackageContent(xpsDocument, false);
  11:  
  12:     // Close the package.
  13:     xpsDocument.Close();
  14: }

Integrating this back into our code, we have:

   1: tempPath = System.IO.Path.GetTempFileName() + ".xps";
   2: Package package = Package.Open(tempPath);
   3: XpsDocument xpsd = new XpsDocument(package);
   4: XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
   5: xw.Write(doc);
   6: xpsd.Close();
   7:  
   8: PrintXPS(tempPath); 

Its that simple!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , | Categories: WPF Posted by toddpi314 on 3/14/2009 12:34 PM | Comments (0)

Lets flex our WPF Muscles and figure out how to render a list of Images:

   1: <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="ImagePanel" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
   2:     <ListBox.ItemsPanel>
   3:         <ItemsPanelTemplate>
   4:             <WrapPanel />
   5:         </ItemsPanelTemplate>
   6:     </ListBox.ItemsPanel>
   7:     <ListBox.ItemTemplate>
   8:         <DataTemplate>
   9:             <TextBlock Text="{Binding ContextKey}" Width="100" />
  10:         </DataTemplate>
  11:     </ListBox.ItemTemplate>
  12:  </ListBox>

Notice, The System.Windows.DataTemplate requires us to set the width on the item in order to allow ‘line breaks’ where we want them. Otherwise, we will just get a ton of items running off the view.

The above code manufactures this view:

image

There are a few theories about using System.Windows.Data.RelativeSource to target the WrapPanel’s Width:

   1: <ListBox.ItemsPanel>
   2:     <ItemsPanelTemplate>
   3:        <WrapPanel Width="{Binding ActualWidth,
   4:             RelativeSource={RelativeSource Mode=FindAncestor,     
   5:             AncestorType={x:Type ScrollContentPresenter}, AncestorLevel=1}}" />
   6:     </ItemsPanelTemplate>
   7: </ListBox.ItemsPanel>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5