将XPS文档拆分为多个XPS文档

时间:2013-01-13 14:50:55

标签: c# split xps

我正在尝试将XPS文档拆分为其他XPS文档。我已经使用以下代码开始,它适用于单个页面而不是更多(http://stackoverflow.com/questions/5328596/extract-a-single-page-from-an-xps-document)。在下面的代码中,我试图将父文档的前3页提取到子文档中。一旦我想出这个,我就会将其余的页面提取到其他文档中。

我遇到的问题是将父页面添加到子节点,所以我尝试使用内存流创建每个页面的副本,但是现在我在调用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 个答案:

答案 0 :(得分:1)

我在将子页面添加到子文档时使用PageContent Source属性修复了我的问题。我还添加了循环来将每n页提取到另一个孩子。