1 // XpsFlatFile SDK Sample - FileHelper.cs
2 // Copyright (c) Microsoft Corporation. All rights reserved.
5 using System
.Collections
.Generic
;
13 /// This is a basic file helper class for the Packaging Policy example.
14 /// This class is used to generate file names based on type, creating the
15 /// files, and getting a file extension based on type. This class is not
16 /// required in order to override the BasePackagingPolicy. It is used as
17 /// a resource for the implementation of this sample.</summary>
18 public class FileHelper
21 /// Here we generate the file name based on the Xps File Type. We also
22 /// create the directory where the file will end up being saved.</summary>
23 /// <param name="fileType">Type of Xps part</param>
24 /// <param name="outputDirectory">Directory where the file will be stored</param>
25 /// <returns>Name of file</returns>
26 public static String
GenerateFileName(XpsFileType fileType
, String outputDirectory
)
32 case XpsFileType
.FixedDocumentSequence
:
33 //There will be only 1 FixedDocumentSequence and it will be at the root
35 fileName
= _documentSequenceName
+ "." + _documentSequenceExtension
;
37 case XpsFileType
.FixedDocument
:
39 fileName
= path
+ _fixedDocumentName
+ "_" + _fixedDocumentCount
+ "." + _fixedDocumentExtension
;
40 _fixedDocumentCount
++;
42 case XpsFileType
.FixedPage
:
44 fileName
= path
+ _fixedPageName
+ "_" + _fixedPageCount
+ "." + _fixedPageExtension
;
47 case XpsFileType
.ImagePng
:
48 path
= "Resources/Images/";
49 fileName
= path
+ _imageName
+ "_" + _imageCount
+ "." + _imagePngExtension
;
52 case XpsFileType
.Font
:
53 Guid fontguid
= Guid
.NewGuid();
54 String fontString
= fontguid
.ToString();
56 path
= "Resources/Fonts/";
57 fileName
= path
+ fontString
+ "." + _obfuscatedFontExtension
;
59 case XpsFileType
.ColorContext
:
60 Guid colorContextGuid
= Guid
.NewGuid();
61 String colorContextString
= colorContextGuid
.ToString();
63 path
= "Resources/ColorContexts/";
64 fileName
= path
+ colorContextString
+ "." + _colorContextExtension
;
66 case XpsFileType
.ResourceDictionary
:
67 Guid resourceDictionaryGuid
= Guid
.NewGuid();
68 String resourceDictionaryString
= resourceDictionaryGuid
.ToString();
70 path
= "Resources/ResourceDictionaries/";
71 fileName
= path
+ resourceDictionaryString
+ "." + _resourceDictionaryExtension
;
73 case XpsFileType
.Relationship
:
78 throw new ArgumentException("File Type is not valid");
83 String fullPath
= outputDirectory
+ "/" + path
;
84 if (Directory
.Exists(fullPath
) != true)
86 Directory
.CreateDirectory(fullPath
);
90 }// end:GenerateFileName
94 /// Creates a file given the filename and path.</summary>
95 /// <param name="fileName">File name of the file to be created</param>
96 /// <param name="outputPath">Path of the file to be created</param>
97 /// <returns></returns>
98 public static FileStream
CreateFile(String fileName
, String outputPath
)
100 int lastChar
= outputPath
.Length
- 1;
101 StringBuilder tmpPath
= new StringBuilder(outputPath
);
102 if (outputPath
[lastChar
] != '/')
106 tmpPath
.Append(fileName
);
107 string path
= tmpPath
.ToString();
108 return File
.Create(path
);
113 /// Returns the extension of the XpsFileType
115 /// <param name="fileType">The type of extension returned</param>
116 /// <returns></returns>
117 public static String
GetExtension(XpsFileType fileType
)
122 case XpsFileType
.FixedDocumentSequence
:
123 extension
= _documentSequenceExtension
;
125 case XpsFileType
.FixedDocument
:
126 extension
= _fixedDocumentExtension
;
128 case XpsFileType
.FixedPage
:
129 extension
= _fixedPageExtension
;
131 case XpsFileType
.Relationship
:
132 extension
= _relationshipExtension
;
134 case XpsFileType
.ImagePng
:
135 extension
= _imagePngExtension
;
137 case XpsFileType
.ImageTif
:
138 extension
= _imageTifExtension
;
140 case XpsFileType
.ImageJpg
:
141 extension
= _imageJpgExtension
;
143 case XpsFileType
.ImageWmp
:
144 extension
= _imageWmpExtension
;
146 case XpsFileType
.Font
:
147 extension
= _fontExtension
;
149 case XpsFileType
.ObfuscatedFont
:
150 extension
= _obfuscatedFontExtension
;
152 case XpsFileType
.ResourceDictionary
:
153 extension
= _resourceDictionaryExtension
;
155 case XpsFileType
.ColorContext
:
156 extension
= _colorContextExtension
;
159 throw new ArgumentException("Invalid XpsFileType");
165 #region Private Members
169 static private string _documentSequenceName
= "FixedDocumentSequence";
170 static private string _fixedDocumentName
= "FixedDocument";
171 static private string _fixedPageName
= "FixedPage";
172 static private string _imageName
= "Image";
176 static private string _documentSequenceExtension
= "fdseq";
177 static private string _fixedDocumentExtension
= "fdoc";
178 static private string _fixedPageExtension
= "fpage";
179 static private string _imagePngExtension
= "png";
180 static private string _imageJpgExtension
= "jpg";
181 static private string _imageWmpExtension
= "wdp";
182 static private string _imageTifExtension
= "tif";
183 static private string _fontExtension
= "ttf";
184 static private string _obfuscatedFontExtension
= "ODTTF";
185 static private string _resourceDictionaryExtension
= "dict";
186 static private string _colorContextExtension
= "icc";
187 static private string _relationshipExtension
= "rels";
189 private static int _fixedDocumentCount
= 1;
190 private static int _fixedPageCount
= 1;
191 private static int _imageCount
= 1;
193 #endregion Private Members
195 }// end:class FileHelper
197 }// end:namespace XpsFlatFile