0

I am trying to allow users to annotate a document using a touchscreen before saving or printing the document. Essentially, I am assembling the document in code with three parts - an ImageBrush with a "picture" of some dynamic text created in a third party application, an ImageBrush with a blank form (transparent background), and a stroke collection representing the input from the touchscreen. I know this is not the preferred way of creating an XPS, however I have no runtime access to the dynamic text to generate it as TextBlocks, etc. The document is defined in XAML like this:

<Viewbox x:Name="viewboxDocView" Grid.Row="0">
      <FixedPage x:Name="fixedPage" Width="720" Height="960" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10">
          <InkCanvas x:Name="inkCanvas" Width="720" Height="960" HorizontalAlignment="Center" VerticalAlignment="Center" ></InkCanvas>
      </FixedPage>
</Viewbox>

In code behind, I am setting the FixedPage background to show the dynamic text, and the InkCanvas background to show the transparent form.

inkCanvas.Background = myImageBrush;
fixedPage.Background = myOtherImageBrush;

It shows up beautifully in my application, saves fine as an XPS file, and displays beautifully in both XPS Viewer and Microsoft Reader. The only problem is that when I try to print from any of these applications, the page image is tiny. The two images print in their entirety and scale down, and the Strokes appear full size but get cropped on most printers so that only the upper-left portion is visible.

I explicitly define the page dimensions as 720 x 960 because 960 * 1/96" = 10 inches and 720 * 1/96" = 7.5 inches, forcing a minimum of 0.5" margin on an 8.5x11" page. When I print, the image is exactly 2.4" x 3.2", which means that the printer resolution is 960 / 3.2 = 300 dpi, not 96 dpi as expected. I know that printers normally do use 300 dpi, so this isn't entirely surprising, but I thought that the whole point of the FixedDocument was the WYSIWYG concept as described here.

Is there a way to explictly define the page as 7.5" x 10" (or 8.5" x 11" with centered content) so that it will print properly? It seems that when I start resizing things in XAML or trying to use transforms, it screws up the on-screen display, which is ultimately more important to the application.

Community
  • 1
  • 1
Dave Smash
  • 2,941
  • 1
  • 18
  • 38
  • are you printing the `Viewbox` or the `FixedPage`? – Kcvin Jan 14 '15 at 23:17
  • I am creating a FixedDocument with the FixedPage as a child and printing that. – Dave Smash Jan 15 '15 at 20:51
  • To be more specific, I am saving the FixedDocument as an .xps file and attempting to print that from either Microsoft XPS Viewer (Windows 7) or Microsoft Reader (Windows 8) – Dave Smash Jan 15 '15 at 20:54
  • maybe show some code you were using? There are a lot of misconceptions with PrintDialog as well as document setup that I'd be willing to look at. Also, if you want to dig into it more you could read the XPS specification. You can see your content in XML form if you change the `.xps` to `.zip` after it is created and view document contents. – Kcvin Jan 20 '15 at 16:56

1 Answers1

0

I would still like to figure out how I can make my files compatible with the standard Microsoft programs, but for now I have created a simple one-window WPF .xps file viewer application that accepts a file path as a command line argument and is printing these correctly. Obviously I could have done this using another window in my existing program except that I can't seem to work around the thread-safety issue of the Windows print dialog. In XAML:

<Window x:Class="Viewer.ViewerWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Document Viewer">
    <Grid>
        <DocumentViewer x:Name="Viewer" />
    </Grid>
</Window>

And in code-behind:

public partial class ViewerWindow : Window
    {
        XpsDocument doc;
        string fileName;

        public ViewerWindow()
        {
            InitializeComponent();

            // get file name from command line
            string[] args = System.Environment.GetCommandLineArgs();
            fileName = args[1];

            // size window
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            this.Width = ((System.Windows.SystemParameters.PrimaryScreenHeight * 8.5) / 11);

            // Open File and Create XpsDocument
            if (!File.Exists(fileName) || fileName == null || fileName == "") 
            {
                System.Windows.MessageBox.Show("File Not Found!");
                this.Close();
            }
            else if (!fileName.EndsWith(".xps") && !fileName.EndsWith(".oxps"))
            {
                System.Windows.MessageBox.Show("File not in XPS format");
                this.Close();
            }
            else 
            {
                try
                {
                    doc = new XpsDocument(fileName, System.IO.FileAccess.Read);
                }
                catch (UnauthorizedAccessException)
                {
                    System.Windows.MessageBox.Show("Unable to open file - check user permission settings and make sure that the file is not open in another program.");
                    this.Close();
                }
            }

            // Display XpsDocument in Viewer
            Viewer.Document = doc.GetFixedDocumentSequence();
        }
    }

This is intended to be a temporary workaround, so please still feel free to contribute!

Dave Smash
  • 2,941
  • 1
  • 18
  • 38