将 XPS 文档附加到现有文档

发布于 2024-10-22 01:39:18 字数 2159 浏览 11 评论 0原文

伙计们,这就是我被困住的地方。

我需要从一大堆微小的 XPS 文件创建一个 XPS 文件。问题是当我尝试执行此操作时,内存总是不足。

我在下面展示了代码(取自 MSDN),但本质上它所做的只是这样:

  1. 它读取每个微小的 XPS 文件
  2. 并从中提取页面。
  3. 将这些页面添加到固定文档序列中。
  4. 当所有文档完成后,它会将此序列写入合并的 XPS 文档中。

IMO,我的固定文档序列变得太大了。所以,我在想,也许我可以一点一点地做这件事——即将微小的 XPS 文档一一附加到合并的 XPS 文档中。

现在,我不知道该怎么做。有什么指点吗?

代码:

            //Create new xps package
            Package combinedXPS = Package.Open(filename, FileMode.Create);
            XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(new XpsDocument(combinedXPS));

            FixedDocumentSequence combinedSequence = new FixedDocumentSequence();

            //Go through each file given
            foreach (string file in filenames)
            {
                //Load Xps Package
                XpsDocument singleXPS = new XpsDocument(file, FileAccess.Read);
                FixedDocumentSequence singleSequence = singleXPS.GetFixedDocumentSequence();

                //Go through each document in the file
                foreach (DocumentReference docRef in singleSequence.References)
                {
                    FixedDocument oldDoc = docRef.GetDocument(false);
                    FixedDocument newDoc = new FixedDocument();
                    DocumentReference newDocReference = new DocumentReference();

                    newDocReference.SetDocument(newDoc);

                    //Go through each page
                    foreach (PageContent page in oldDoc.Pages)
                    {
                        PageContent newPage = new PageContent();
                        newPage.Source = page.Source;
                        (newPage as IUriContext).BaseUri = ((IUriContext)page).BaseUri;
                        newPage.GetPageRoot(true);
                        newDoc.Pages.Add(newPage);
                    }

                    //Add the document to package
                    combinedSequence.References.Add(newDocReference);
                }
                singleXPS.Close();
            }

            xpsWriter.Write(combinedSequence);
            combinedXPS.Close();

Guys, here's where I am stuck.

I have a need to create a single XPS file from a huge bunch of tiny XPS files. The problem is that I keep running out of memory when I try to do this.

I present below the code (taken from MSDN), but essentially all it does is this:

  1. It reads each tiny XPS file
  2. Extracts the pages from it.
  3. Adds these pages to a FixedDocumentSequence.
  4. When all docs are done, it writes this sequence out to the combined XPS doc.

IMO, my FixedDocumentSequence is getting too big. So, I am thinking, that maybe I can do this piece by piece - i.e. append the tiny XPS docs to the combined XPS docs one by one.

Now, I don't know how to do that. Any pointers?

Code:

            //Create new xps package
            Package combinedXPS = Package.Open(filename, FileMode.Create);
            XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(new XpsDocument(combinedXPS));

            FixedDocumentSequence combinedSequence = new FixedDocumentSequence();

            //Go through each file given
            foreach (string file in filenames)
            {
                //Load Xps Package
                XpsDocument singleXPS = new XpsDocument(file, FileAccess.Read);
                FixedDocumentSequence singleSequence = singleXPS.GetFixedDocumentSequence();

                //Go through each document in the file
                foreach (DocumentReference docRef in singleSequence.References)
                {
                    FixedDocument oldDoc = docRef.GetDocument(false);
                    FixedDocument newDoc = new FixedDocument();
                    DocumentReference newDocReference = new DocumentReference();

                    newDocReference.SetDocument(newDoc);

                    //Go through each page
                    foreach (PageContent page in oldDoc.Pages)
                    {
                        PageContent newPage = new PageContent();
                        newPage.Source = page.Source;
                        (newPage as IUriContext).BaseUri = ((IUriContext)page).BaseUri;
                        newPage.GetPageRoot(true);
                        newDoc.Pages.Add(newPage);
                    }

                    //Add the document to package
                    combinedSequence.References.Add(newDocReference);
                }
                singleXPS.Close();
            }

            xpsWriter.Write(combinedSequence);
            combinedXPS.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫弦 2024-10-29 01:39:18

XPS 文档只是压缩 XAML 文件,我敢打赌您可以直接从所有内容中写出 XML源文件而不将其全部保留在内存中,然后将其压缩。甚至还有 .NET API 可以帮助您直接处理此类文件。

XPS documents are just zipped up XAML files, I bet you could just directly write out an XML from all of the source files without keeping it all in memory, then zip it up. There's even .NET APIs that help you deal with these kinds of files directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文