2009-11-02 217 views
3

可以將多個xps文檔合併爲WPF中的一個xps文檔並顯示在DocumentViewer
一個應用程序有4個小的xps文檔,每個文檔都單獨顯示,但在其中一個地方,所有4個文檔應該顯示爲一個文檔。我該如何解決它?可以將多個xps文檔合併爲一個WPF文件嗎?

回答

2

這裏,targetDocument是新文件的目標路徑,list是要合併的所有文檔的列表。

public void CreateXPSStreamPages(string targetDocument, List<string> list) 
    { 
     Package container = Package.Open(targetDocument, FileMode.Create); 
     XpsDocument xpsDoc = new XpsDocument(container); 
     XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc); 

     SerializerWriterCollator vxpsd = writer.CreateVisualsCollator(); 
     vxpsd.BeginBatchWrite(); 
     foreach (string sourceDocument in list) 
     { 
      AddXPSDocument(sourceDocument, vxpsd); 
     } 
     vxpsd.EndBatchWrite(); 
     container.Close();    
    } 

    public void AddXPSDocument(string sourceDocument, SerializerWriterCollator vxpsd) 
    { 
     XpsDocument xpsOld = new XpsDocument(sourceDocument, FileAccess.Read); 
     FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence(); 
     foreach (DocumentReference r in seqOld.References) 
     { 
      FixedDocument d = r.GetDocument(false); 
      foreach (PageContent pc in d.Pages) 
      { 
       FixedPage fixedPage = pc.GetPageRoot(false); 
       double width = fixedPage.Width; 
       double height = fixedPage.Height; 
       Size sz = new Size(width, height); 
       fixedPage.Width = width; 
       fixedPage.Height = height; 
       fixedPage.Measure(sz); 
       fixedPage.Arrange(new Rect(new Point(), sz)); 
       //fixedPage.UpdateLayout(); 

       ContainerVisual newPage = new ContainerVisual(); 
       newPage.Children.Add(fixedPage); 
       //test: add Watermark from Feng Yuan sample 
       //newPage.Children.Add(CreateWatermark(width, height, "Watermark")); 

       vxpsd.Write(newPage); 
      } 
     } 
     xpsOld.Close(); 
    } 
1

如果有人有興趣在VB代碼:

Public Sub CreateXPSStream(targetDocument As String, ListToMerge As List(Of String)) 
    If (File.Exists(targetDocument)) Then 
     File.Delete(targetDocument) 
    End If 
    Dim container As Package = Package.Open(targetDocument, FileMode.Create) 
    Dim xpsDoc = New System.Windows.Xps.Packaging.XpsDocument(container) 
    Dim seqNew As FixedDocumentSequence = New FixedDocumentSequence() 
    For Each sourceDocument As String In ListToMerge 
     AddXPSDocuments(sourceDocument, seqNew) 
    Next 
    Dim xpsWriter As XpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc) 
    xpsWriter.Write(seqNew) 
    xpsDoc.Close() 
    container.Close() 
End Sub 
Public Sub AddXPSDocuments(ByVal sourceDocument As String, ByRef seqNew As FixedDocumentSequence) 
    Try 
     Dim xpsOld As XpsDocument = New XpsDocument(sourceDocument, FileAccess.Read) 
     Dim seqOld As FixedDocumentSequence = xpsOld.GetFixedDocumentSequence() 
     For Each r As DocumentReference In seqOld.References 
      Dim newRef As DocumentReference = New DocumentReference() 
      CType(newRef, IUriContext).BaseUri = CType(r, IUriContext).BaseUri 
      newRef.Source = r.Source 
      seqNew.References.Add(newRef) 
     Next 
    Catch ex As Exception 
     myStatusAdd("Error with " & sourceDocument) 
    End Try 
End Sub 

感謝您的初始代碼。
這也需要引用相當數量的在您的項目:

PresentationCore 
PresentationFramework 
ReachFramwork 
System.Core 
System.Windows.Presentation 
System.Xaml 
System.Printing 
WindowsBase 

我認爲這就是全部。

相關問題