日期:2014-05-18 浏览次数:20882 次
private void MergerPrintFiles(string targetDocument, string[] appendFiles)
        {
            if (File.Exists(targetDocument))
            {
                File.Delete(targetDocument);
            }
            Package container = Package.Open(targetDocument, FileMode.Create);
            XpsDocument xpsDoc = new XpsDocument(container);
            XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
            FixedDocumentSequence seqNew = new FixedDocumentSequence();
            foreach (string sourceDocument in appendFiles)
            {
                XpsDocument xpsOld = null;
                try
                {
                    xpsOld = new XpsDocument(sourceDocument, FileAccess.Read);
                    FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
                    foreach (DocumentReference r in seqOld.References)
                    {
                        DocumentReference newRef = new DocumentReference();
                        (newRef as IUriContext).BaseUri = (r as IUriContext).BaseUri;
                        newRef.Source = r.Source;
                        seqNew.References.Add(newRef);
                    }
                }
                catch { }
                finally
                {
                    xpsOld.Close();
                }
            }
            FixedPage page = (seqNew.DocumentPaginator.GetPage(0).Visual) as FixedPage;
            page.UpdateLayout();
            xpsWriter.Write(seqNew);
            xpsDoc.Close();
            container.Close();
            foreach (string fileFullName in appendFiles)
            {
                FileInfo fileInfo = new FileInfo(fileFullName);
                if (fileInfo.Exists) fileInfo.Delete();
            }
           
        }