2013-01-13 120 views
1

我試圖將XPS文檔拆分爲其他XPS文檔。我已經使用下面的代碼來開始,它可以在單個頁面上正常工作,但不會更多(http://stackoverflow.com/questions/5328596/extract-a-single-page-from-an-xps-document)。在下面的代碼中,我試圖將父文檔的前3頁提取到子文檔中。一旦我找出這一個,我將把剩餘的頁面提取到其他文檔中。將XPS文檔拆分爲多個XPS文檔

我遇到的問題是將父頁添加到孩子,所以我嘗試使用內存流創建每個頁面的副本,但現在調用XpsDocumentWriter時會出現以下異常。

例外:「發生I/O錯誤」。

我應該如何將父母的頁面添加到孩子?

解決:此代碼適用於我的WF4項目。

public void Split(InputFile parent, string outputDirectory, int splitPageFrequency, CodeActivityContext context) 
    { 
     // Open the parent XPS package 
     using (Package package = Package.Open(parent.InputFilePath, FileMode.Open, FileAccess.Read)) 
     { 
      string Pack = "pack://temp.xps"; 
      Uri packageUri = new Uri(Pack); 
      PackageStore.AddPackage(packageUri, package); 

      // Get the parent XPS document 
      XpsDocument xpsDocumentParent = new XpsDocument(package, CompressionOption.Maximum, Pack); 
      FixedDocumentSequence fixedDocumentSequenceParent = xpsDocumentParent.GetFixedDocumentSequence(); 
      DocumentReference documentReferenceParent = fixedDocumentSequenceParent.References.First(); 
      FixedDocument fixedDocumentParent = documentReferenceParent.GetDocument(false); 

      for (int pageCounter = 0, fileCounter = 1; pageCounter < fixedDocumentParent.Pages.Count; pageCounter += splitPageFrequency, fileCounter++) 
      { 
       // Generate the child file name 
       Guid documentId = Guid.NewGuid(); 
       string childName = outputDirectory + parent.InputFileId + "_" + fileCounter + "_" + documentId + ".xps"; 

       // Open the child XPS package 
       using (Package packageDest = Package.Open(childName)) 
       { 
        // Create the child XPS document 
        XpsDocument xpsDocumentChild = new XpsDocument(packageDest, CompressionOption.Maximum, childName); 
        FixedDocument fixedDocumentChild = new FixedDocument(); 

        // Add the parent's pages in range to the child 
        for (int pageCounterChild = pageCounter; pageCounterChild < pageCounter + splitPageFrequency; pageCounterChild++) 
        { 
         // Add the copied fixedpage to the child FixedDocument. 
         PageContent pageContentChild = new PageContent(); 
         pageContentChild.Source = fixedDocumentParent.Pages[pageCounterChild].Source; 
         (pageContentChild as IUriContext).BaseUri = ((IUriContext)fixedDocumentParent.Pages[pageCounterChild]).BaseUri; 
         pageContentChild.GetPageRoot(false); 
         fixedDocumentChild.Pages.Add(pageContentChild); 
        } 

        //Write the child FixedDocument to the XPSdocument 
        XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocumentChild); 
        writer.Write(fixedDocumentChild); 
        xpsDocumentChild.Close(); 
       } 

       // Set the OutArgument value 
       NumberOfDocuments.Set(context, fileCounter); 
      } 

      PackageStore.RemovePackage(packageUri); 
      xpsDocumentParent.Close(); 
     } 

    } 

回答

1

我在將父頁面添加到子文檔時使用PageContent Source屬性解決了我的問題。我還添加了循環來提取每個n頁到不同的孩子。