1

这行得通

FixedDocumentSeqeunce fixedDocumentSeqeunce = XPSdoc.GetFixedDocumentSeqeunce();    
DocumentPaginator paginator = fixedDocumentSeqeunce.DocumentPaginator;

printDialog.PrintDocument(paginator, "Title");

这失败了

AnnotationDocumentPaginator adp = new AnnotationDocumentPaginator(paginator, service.Store); 
// same paginator as above    
printDialog.PrintDocument(adp, "Title");

printDialog.PrintDocument(adp, "标题"); 是什么在杀死它
不知何故,它似​​乎正在破坏 XPSdoc

商店看起来不错-我可以得到注释的计数
即使我创建零个注释它也会失败
但是打印本身不会失败-我得到一个有效的打印
上面的尝试捕获不会捕获错误
几秒钟后打印错误开始于
(我有一个未处理异常的处理程序)

指定的视觉对象不是视觉对象的祖先

应用程序崩溃,并且 XPS 文档永远不会在 DocumentViewer 中重新绘制

我可以使用 ScrollViewer 获取 FlowDocuments 以打印注释
并以相同的方式创建存储,所以我认为代码很好

.NET 4.5

4

1 回答 1

0

不知道我的大型程序中发生了什么,但我现在无法在较小的程序上重现它

弄清楚
在我没有使用的失败代码中

FixedDocumentSeqeunce fixedDocumentSeqeunce = XPSdoc.GetFixedDocumentSeqeunce();

在 printDialog.PrintDocument 中,我使用了与控件绑定到的完全相同的 fixedDocumentSeqeunce。Print 必须改变 fixedDocumentSeqeunce,因此它需要一个新的 fixedDocumentSeqeunce。

<Window x:Class="APSannotate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:ann="clr-namespace:System.Windows.Annotations;assembly=PresentationFramework"
        Loaded="Window_Loaded"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button Grid.Row="0" Grid.Column="0" Click="click" Content="Print"/>
        <Button Grid.Row="0" Grid.Column="1" Content="Sticky" Width="50" HorizontalAlignment="Left"
                Command="ann:AnnotationService.CreateTextStickyNoteCommand"
                CommandTarget="{Binding ElementName=docViewer}"/>
        <DocumentViewer Grid.Row="1" Grid.ColumnSpan="2" Document="{Binding Path=FixedDocSeq}" Name="docViewer"/>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Xps;
using System.Windows.Annotations;
using System.IO;
using System.Xml;



namespace APSannotate
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        AnnotationService _annotService;
        //FileStream _annotStream;
        System.Windows.Annotations.Storage.XmlStreamStore _annotStore;
        System.Windows.Xps.Packaging.XpsDocument xpsDoc;
        //MemoryStream _streamAnnoMem;
        public MainWindow()
        {
            this.DataContext = this;
            xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(@"c:\temp\ann.xps", System.IO.FileAccess.Read);
            InitializeComponent();

        }
        public FixedDocumentSequence FixedDocSeq
        {
            get { return xpsDoc.GetFixedDocumentSequence(); }
        }

        private void click(object sender, RoutedEventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();
            bool? result = printDialog.ShowDialog();
            if (result != null && result.Value)
            {
                DocumentPaginator documentPaginator = FixedDocSeq.DocumentPaginator;
                printDialog.PrintDocument(documentPaginator, "documentPaginator");

                AnnotationService service = AnnotationService.GetService(docViewer);
                AnnotationDocumentPaginator annotationDocumentPaginator = new AnnotationDocumentPaginator(documentPaginator, service.Store);
                printDialog.PrintDocument(annotationDocumentPaginator, "annotationDocumentPaginator");
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            StartAnnotations();
        }
        private void StartAnnotations()
        {
            // If there is no AnnotationService yet, create one.
            if (_annotService == null)
                // docViewer is a document viewing control named in Window1.xaml.
                _annotService = new AnnotationService(docViewer);

            // If the AnnotationService is currently enabled, disable it.
            if (_annotService.IsEnabled == true)
                _annotService.Disable();

            // Open a stream to the file for storing annotations.
            //_annotStream = new FileStream(@"c:\temp\annoStore", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            // Create an AnnotationStore using the file stream.
            //_annotStore = new System.Windows.Annotations.Storage.XmlStreamStore(_annotStream);

            MemoryStream _streamAnnoMem = new MemoryStream();
            _annotStore = new System.Windows.Annotations.Storage.XmlStreamStore(_streamAnnoMem);

            // Enable the AnnotationService using the new store.
            _annotService.Enable(_annotStore);
        }// end:StartAnnotations()
    }
}
于 2016-05-22T19:48:51.460 回答