Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Combining/Merging XPS files with F#

View combine_XPS_files.fsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#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
Something went wrong with that request. Please try again.