Skip to content

Instantly share code, notes, and snippets.

@MrGung
Created August 27, 2011 18:31
Show Gist options
  • Save MrGung/1175708 to your computer and use it in GitHub Desktop.
Save MrGung/1175708 to your computer and use it in GitHub Desktop.
Combining/Merging XPS files with F#
#r "ReachFramework.dll"
#r "WindowsBase.dll"
#r "PresentationFramework.dll"
#r "PresentationCore.dll"
#r "System.Xaml.dll"
#r "System.Printing.dll"
open System.Windows.Xps.Packaging
open System.Windows.Documents
open System.Windows.Markup
open System.IO
open System.IO.Packaging
(*
The following code was translated from C#.
The original code can be found at http://social.msdn.microsoft.com/Forums/en-US/windowsxps/thread/1dfb532d-f9fc-438d-87e0-7ef764abb847/
*)
let AddXPSDocuments (sourceDocument : string) (seqNew : FixedDocumentSequence) =
let xpsOld = new XpsDocument(sourceDocument, FileAccess.Read)
let seqOld = xpsOld.GetFixedDocumentSequence()
for r in seqOld.References do
let newRef = new DocumentReference()
(*
Background to box and :?>:
http://cs.hubfs.net/forums/thread/3616.aspx Dynamic cast to Interface
*)
((box newRef) :?> IUriContext).BaseUri <- ((box r) :?> IUriContext).BaseUri
newRef.Source <- r.Source;
seqNew.References.Add(newRef)
let CreateXPSStream targetDocument list =
if File.Exists(targetDocument)
then File.Delete(targetDocument)
let container = Package.Open(targetDocument, FileMode.Create)
let xpsDoc = new XpsDocument(container)
let seqNew = new FixedDocumentSequence()
for sourceDocument in list do
AddXPSDocuments sourceDocument seqNew
let xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc)
xpsWriter.Write(seqNew)
xpsDoc.Close()
container.Close()
let list = [|@"d:\FSharp\The F# Survival Guide\00.xps"
@"d:\FSharp\The F# Survival Guide\01.xps"
@"d:\FSharp\The F# Survival Guide\02.xps"
@"d:\FSharp\The F# Survival Guide\03.xps"
@"d:\FSharp\The F# Survival Guide\04.xps"
@"d:\FSharp\The F# Survival Guide\05.xps"
@"d:\FSharp\The F# Survival Guide\06.xps"
@"d:\FSharp\The F# Survival Guide\07.xps"
@"d:\FSharp\The F# Survival Guide\08.xps"
@"d:\FSharp\The F# Survival Guide\09.xps"
@"d:\FSharp\The F# Survival Guide\10.xps"
@"d:\FSharp\The F# Survival Guide\11.xps"
@"d:\FSharp\The F# Survival Guide\12.xps"
@"d:\FSharp\The F# Survival Guide\13.xps"
@"d:\FSharp\The F# Survival Guide\14.xps"
@"d:\FSharp\The F# Survival Guide\15.xps"
@"d:\FSharp\The F# Survival Guide\16.xps"
@"d:\FSharp\The F# Survival Guide\17.xps"|]
CreateXPSStream @"c:\The F# Survival Guide.xps" list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment