OoXmlExportBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using System.Reflection;
  6. namespace FastReport.Export.OoXML
  7. {
  8. /// <summary>
  9. /// Base class for Microsoft Office 2007 export objects
  10. /// </summary>
  11. public class OOExportBase : ExportBase
  12. {
  13. private ZipArchive zip;
  14. /// <summary>
  15. /// Default XML header
  16. /// </summary>
  17. #region Constants
  18. protected const string xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
  19. #endregion
  20. #region Properties
  21. internal ZipArchive Zip { get { return zip; } set { zip = value; } }
  22. #endregion
  23. #region Helpers
  24. internal string Quoted(string p)
  25. {
  26. return "\"" + p + "\" ";
  27. }
  28. internal string QuotedRoot(string p)
  29. {
  30. return "\"/" + p + "\" ";
  31. }
  32. #endregion
  33. }
  34. /// <summary>
  35. /// Base class for export Office Open objects
  36. /// </summary>
  37. internal abstract class OoXMLBase
  38. {
  39. #region Private fileds
  40. private ArrayList relations = new ArrayList();
  41. private int id;
  42. #endregion
  43. #region Constants
  44. public const string xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
  45. #endregion
  46. #region Abstract
  47. public abstract string RelationType { get; }
  48. public abstract string ContentType { get; }
  49. public abstract string FileName { get; }
  50. #endregion
  51. #region Helpers
  52. protected string Quoted(string p)
  53. {
  54. return String.Concat("\"", p, "\" ");
  55. }
  56. protected string Quoted(long p)
  57. {
  58. return String.Concat("\"", p.ToString(), "\" ");
  59. }
  60. protected string Quoted(float p)
  61. {
  62. return String.Concat("\"", Converter.ToString(p), "\" ");
  63. }
  64. protected string GetDashStyle(System.Drawing.Drawing2D.DashStyle style)
  65. {
  66. switch (style)
  67. {
  68. case System.Drawing.Drawing2D.DashStyle.Solid: return "<a:prstDash val=\"solid\"/>";
  69. case System.Drawing.Drawing2D.DashStyle.Dot: return "<a:prstDash val=\"dot\"/>";
  70. case System.Drawing.Drawing2D.DashStyle.Dash: return "<a:prstDash val=\"dash\"/>";
  71. case System.Drawing.Drawing2D.DashStyle.DashDot: return "<a:prstDash val=\"dashDot\"/>";
  72. case System.Drawing.Drawing2D.DashStyle.DashDotDot: return "<a:prstDash val=\"sysDashDotDot\"/>";
  73. }
  74. throw new Exception("Unsupported dash style");
  75. }
  76. private string TranslatePath(string source, string dest)
  77. {
  78. int j;
  79. string result = "";
  80. char[] charSeparators = new char[] { '\\', '/' };
  81. string[] rel_dir_name = Path.GetDirectoryName(source).Split(charSeparators);
  82. string[] items_dir_name = Path.GetDirectoryName(dest).Split(charSeparators);
  83. for (int i = 0; ; i++)
  84. {
  85. if (i == rel_dir_name.Length || i == items_dir_name.Length || items_dir_name[i].CompareTo(rel_dir_name[i]) != 0)
  86. {
  87. for (j = i; j < rel_dir_name.Length; j++) result += "../";
  88. for (j = i; j < items_dir_name.Length; j++) result += items_dir_name[j];
  89. break;
  90. }
  91. }
  92. if (result != "") result += "/";
  93. return result;
  94. }
  95. #endregion
  96. #region Properties
  97. internal string rId { get { return "rId" + id.ToString(); } }
  98. public ArrayList RelationList { get { return relations; } }
  99. #endregion
  100. #region Protected methods
  101. protected void ExportRelations(OOExportBase export_base)
  102. {
  103. if (relations.Count != 0)
  104. {
  105. string relation_dir_name = Path.GetDirectoryName(FileName) + "/_rels/";
  106. string relation_file_name = Path.GetFileName(FileName) + ".rels";
  107. string related_path = "";
  108. MemoryStream file = new MemoryStream();
  109. ExportUtils.WriteLn(file, xml_header);
  110. ExportUtils.WriteLn(file, "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">");
  111. foreach (OoXMLBase relation_item in relations)
  112. {
  113. if (relation_item is OoHyperlink)
  114. {
  115. OoHyperlink item = relation_item as OoHyperlink;
  116. ExportUtils.WriteLn(file,
  117. "<Relationship Id=" + Quoted(relation_item.rId) +
  118. "Type=" + Quoted(relation_item.RelationType) +
  119. "Target=" + Quoted(item.URL) +
  120. "TargetMode=" + Quoted(relation_item.FileName) + "/>");
  121. }
  122. else
  123. {
  124. related_path = TranslatePath(FileName, relation_item.FileName) + Path.GetFileName(relation_item.FileName);
  125. ExportUtils.WriteLn(file,
  126. "<Relationship Id=" + Quoted(relation_item.rId) +
  127. "Type=" + Quoted(relation_item.RelationType) +
  128. "Target=" + Quoted(related_path) + "/>");
  129. }
  130. }
  131. ExportUtils.WriteLn(file, "</Relationships>");
  132. file.Position = 0;
  133. export_base.Zip.AddStream(ExportUtils.TruncLeadSlash(relation_dir_name + relation_file_name), file);
  134. }
  135. }
  136. #endregion
  137. #region Internal Methods
  138. internal bool AddRelation(int Id, OoXMLBase related_object)
  139. {
  140. if (!relations.Contains(related_object))
  141. {
  142. related_object.id = Id;
  143. relations.Add(related_object);
  144. return true;
  145. }
  146. return false;
  147. }
  148. #endregion
  149. }
  150. /// <summary>
  151. /// Core document properties
  152. /// </summary>
  153. class OoXMLCoreDocumentProperties : OoXMLBase
  154. {
  155. #region Class overrides
  156. public override string RelationType { get { return "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"; } }
  157. public override string ContentType { get { return "application/vnd.openxmlformats-package.core-properties+xml"; } }
  158. public override string FileName { get { return "docProps/core.xml"; } }
  159. #endregion
  160. public void Export(OOExportBase OoXML)
  161. {
  162. string Title = OoXML.Report.ReportInfo.Name;
  163. string Author = OoXML.Report.ReportInfo.Author;
  164. string Subject = OoXML.Report.ReportInfo.Description;
  165. if (Author.Length == 0)
  166. Author = "FastReport.NET";
  167. if (Title.Length == 0)
  168. Title = Path.GetFileNameWithoutExtension(OoXML.Report.FileName);
  169. MemoryStream file = new MemoryStream();
  170. ExportUtils.WriteLn(file, xml_header);
  171. ExportUtils.WriteLn(file, "<cp:coreProperties xmlns:cp=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:dcmitype=\"http://purl.org/dc/dcmitype/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
  172. //Out.WriteLine("<dcterms:created xsi:type=\"dcterms:W3CDTF\">2009-06-17T07:33:19Z</dcterms:created>");
  173. ExportUtils.WriteLn(file, "<dc:title>" + Title + "</dc:title>");
  174. if (Subject.Length != 0)
  175. ExportUtils.WriteLn(file, "<dc:subject>" + Subject.Replace("<", "&lt;").Replace(">", "&gt;") + "</dc:subject>");
  176. ExportUtils.WriteLn(file, "<dc:creator>" + Author + "</dc:creator>");
  177. ExportUtils.WriteLn(file, "</cp:coreProperties>");
  178. file.Position = 0;
  179. OoXML.Zip.AddStream(ExportUtils.TruncLeadSlash(FileName), file);
  180. }
  181. }
  182. /// <summary>
  183. /// Core document properties
  184. /// </summary>
  185. class OoXMLApplicationProperties : OoXMLBase
  186. {
  187. #region Class overrides
  188. public override string RelationType { get { return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"; } }
  189. public override string ContentType { get { return "application/vnd.openxmlformats-officedocument.extended-properties+xml"; } }
  190. public override string FileName { get { return "docProps/app.xml"; } }
  191. #endregion
  192. public void Export(OOExportBase OoXML)
  193. {
  194. MemoryStream file = new MemoryStream();
  195. ExportUtils.WriteLn(file, xml_header);
  196. ExportUtils.WriteLn(file, "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">");
  197. ExportUtils.WriteLn(file, "<DocSecurity>0</DocSecurity>");
  198. ExportUtils.WriteLn(file, "<ScaleCrop>false</ScaleCrop>");
  199. // Heading description
  200. ExportUtils.WriteLn(file, "<HeadingPairs>");
  201. ExportUtils.WriteLn(file, "<vt:vector size=\"2\" baseType=\"variant\">");
  202. ExportUtils.WriteLn(file, "<vt:variant>");
  203. ExportUtils.WriteLn(file, "<vt:lpstr>Worksheets</vt:lpstr>");
  204. ExportUtils.WriteLn(file, "</vt:variant>");
  205. ExportUtils.WriteLn(file, "<vt:variant>");
  206. ExportUtils.WriteLn(file, "<vt:i4>2</vt:i4>");
  207. ExportUtils.WriteLn(file, "</vt:variant>");
  208. ExportUtils.WriteLn(file, "</vt:vector>");
  209. ExportUtils.WriteLn(file, "</HeadingPairs>");
  210. // Titles description
  211. ExportUtils.WriteLn(file, "<TitlesOfParts>");
  212. ExportUtils.WriteLn(file, "<vt:vector size=\"1\" baseType=\"lpstr\">");
  213. ExportUtils.WriteLn(file, "<vt:lpstr>Page1</vt:lpstr>");
  214. ExportUtils.WriteLn(file, "</vt:vector>");
  215. ExportUtils.WriteLn(file, "</TitlesOfParts>");
  216. ExportUtils.WriteLn(file, "<LinksUpToDate>false</LinksUpToDate>");
  217. ExportUtils.WriteLn(file, "<SharedDoc>false</SharedDoc>");
  218. ExportUtils.WriteLn(file, "<HyperlinksChanged>false</HyperlinksChanged>");
  219. ExportUtils.WriteLn(file, "<AppVersion>12.0000</AppVersion>");
  220. ExportUtils.WriteLn(file, "</Properties>");
  221. file.Position = 0;
  222. OoXML.Zip.AddStream(ExportUtils.TruncLeadSlash(FileName), file);
  223. }
  224. }
  225. internal class OoXMLThemes : OoXMLBase
  226. {
  227. #region Class overrides
  228. public override string RelationType { get { return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"; } }
  229. public override string ContentType { get { return "application/vnd.openxmlformats-officedocument.theme+xml"; } }
  230. public override string FileName { get { return "ppt/theme/theme1.xml"; } }
  231. #endregion
  232. public void Export(OOExportBase OoXML, string ThemeRes, string ThemePath)
  233. {
  234. //ResourceSet set = new ResourceSet();
  235. // get a reference to the current assembly
  236. Assembly a = Assembly.GetExecutingAssembly();
  237. // get a list of resource names from the manifest
  238. //string[] resNames = a.GetManifestResourceNames();
  239. using (Stream o = a.GetManifestResourceStream("FastReport.Resources.OoXML.theme1.xml"))
  240. {
  241. // write the required bytes
  242. MemoryStream fs = new MemoryStream();
  243. const int BUFFER_SIZE = 4096;
  244. o.CopyTo(fs, BUFFER_SIZE);
  245. fs.Position = 0;
  246. OoXML.Zip.AddStream(ExportUtils.TruncLeadSlash(ThemePath), fs);
  247. }
  248. }
  249. }
  250. }