1

我正在尝试将 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();
        }

    }
4

1 回答 1

1

在将父页面添加到子文档时,我使用 PageContent Source 属性解决了我的问题。我还添加了循环以将每个 n 页提取到不同的子级。

于 2013-01-14T16:35:05.840 回答