예제 #1
0
        public void CreateXPS()
        {
            ReplaceCallBackType cb       = new ReplaceCallBackType(this.ReplaceText);
            XPSTemplate         template = new XPSTemplate();

            template.CreateNewXPSFromSource(this.m_Template, this.m_DestinationPath, cb);
        }
예제 #2
0
        public bool CreateNewXPSFromSource(string sourceXpsPath, string destinationXpsPath, ReplaceCallBackType cb)
        {
            this.m_CallBack = cb;
            bool blnResult = false;

            try
            {
                // Delete the old file generated by our code.
                if (File.Exists(destinationXpsPath))
                {
                    File.Delete(destinationXpsPath);
                }

                // Creating the output XPS file.
                XpsDocument document = new XpsDocument(destinationXpsPath,
                                                       FileAccess.ReadWrite, System.IO.Packaging.CompressionOption.NotCompressed);

                IXpsFixedDocumentSequenceWriter docSeqWriter = document.AddFixedDocumentSequence();

                // Loading the source xps files to list to read them for edit.
                List <string> sourceFiles = new List <string>();
                sourceFiles.Add(sourceXpsPath);

                // Looping each source files.
                foreach (string sourceFile in sourceFiles)
                {
                    XpsDocument docToRead = new XpsDocument(sourceFile, FileAccess.ReadWrite);
                    IXpsFixedDocumentSequenceReader docSequenceToRead =
                        docToRead.FixedDocumentSequenceReader;

                    foreach (IXpsFixedDocumentReader fixedDocumentReader in
                             docSequenceToRead.FixedDocuments)
                    {
                        IXpsFixedDocumentWriter fixedDocumentWriter =
                            docSeqWriter.AddFixedDocument();

                        AddStructure(fixedDocumentReader, fixedDocumentWriter);

                        foreach (IXpsFixedPageReader fixedPageReader in
                                 fixedDocumentReader.FixedPages)
                        {
                            IXpsFixedPageWriter pageWriter =
                                fixedDocumentWriter.AddFixedPage();

                            AddImages(fixedPageReader, pageWriter);

                            AddFonts(fixedPageReader, pageWriter);

                            AddContent(fixedPageReader, pageWriter);

                            // Commmit all changes.
                            pageWriter.Commit();
                        }
                        fixedDocumentWriter.Commit();
                    }
                    // Close the current source before openeing next one.
                    docToRead.Close();
                }

                docSeqWriter.Commit();

                // Close newly created XPS document.
                document.Close();
                blnResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return(blnResult);
        }