using Microsoft.Office.Interop.Word; /* Formatter.cs * part of zaaReloaded2 * * Copyright 2015 Daniel Kraus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace zaaReloaded2.Medication { /// /// Formats prescriptions /// public class Formatter { #region Properties public IList Prescriptions { get; set; } #endregion #region Constructor public Formatter() { } public Formatter(IList prescriptions) : this() { Prescriptions = prescriptions; } #endregion #region Methods /// /// Writes a block of prescriptions with one column to a /// Word document. /// /// public void FormatOneColumn(Document document) { DoFormat("Medikation einspaltig formatieren", document, writer => { foreach (Prescription p in Prescriptions) { writer.WriteLine(p.ToString()); } }); } /// /// Writes a block of prescriptions with two columns to a /// Word document. /// /// public void FormatTwoColumns(Document document) { DoFormat("Medikation zweispaltig formatieren", document, writer => { for (int i = 0; i < Prescriptions.Count; i += 2) { writer.Write(Prescriptions[i].ToString()); if (i + 1 < Prescriptions.Count) { writer.Write("\t" + Prescriptions[i+1].ToString()); } writer.WriteLine(); } }); } /// /// Creates a table containing all prescriptions and copies it to /// the clipboard. /// public void CreatePrescriptionsTable() { throw new NotImplementedException(); } #endregion #region Private methods void AddDisclaimer(zaaReloaded2.Formatter.DocumentWriter writer) { writer.WriteLine("Bitte Medikation überprüfen!"); } /// /// Creates a paragraph and character styles in the document. /// void CreateStyles(Document document) { if (document != null) { Style style; // Don't see a better way to check for the existence of a particular // paragraph style than by using a try...catch construction. try { style = document.Styles[Properties.Settings.Default.DrugsParagraph]; } catch { // Add default paragraph style for laboratory style = document.Styles.Add(Properties.Settings.Default.DrugsParagraph); style.Font.Size = 10; // pt style.Font.Bold = 0; style.Font.Italic = 0; style.Font.Underline = 0; style.ParagraphFormat.SpaceAfter = 0; style.ParagraphFormat.SpaceBefore = 0; style.ParagraphFormat.LeftIndent = 0; // pt style.ParagraphFormat.FirstLineIndent = 0; // pt style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; style.ParagraphFormat.TabStops.ClearAll(); int tabStop = 108; // 108 pt = 2.5 in = 3.8 cm int halfWay = 227; // 227 pt = 3.15 in = 8 cm style.ParagraphFormat.TabStops.Add(tabStop); style.ParagraphFormat.TabStops.Add(halfWay); style.ParagraphFormat.TabStops.Add(halfWay + tabStop); } // try // { // style = document.Styles[Properties.Settings.Default.DrugsHeader]; // } // catch // { // // Add header paragraph style for laboratory // style = document.Styles.Add(Properties.Settings.Default.DrugsHeader); // style.Font.Size = 10; // pt // style.Font.Bold = 1; // style.Font.Italic = 0; // style.Font.Underline = WdUnderline.wdUnderlineSingle; // style.ParagraphFormat.SpaceAfter = 0; // style.ParagraphFormat.SpaceBefore = 12; // style.ParagraphFormat.LeftIndent = 36; // pt // style.ParagraphFormat.FirstLineIndent = -36; // pt // style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify; // style.set_NextParagraphStyle(document.Styles[Properties.Settings.Default.DrugsParagraph]); // } } } /// /// Does the heavy lifting in a DRY way. /// void DoFormat(string description, Document document, Action outputAction) { if (document == null) { throw new ArgumentNullException( "Cannot format prescriptions because no document was given."); } Helpers.StartUndo(description); zaaReloaded2.Formatter.DocumentWriter writer = new zaaReloaded2.Formatter.DocumentWriter(document); CreateStyles(document); writer.Write(String.Format("", Properties.Settings.Default.DrugsParagraph)); outputAction(writer); AddDisclaimer(writer); // writer.Write(""); // causes COM exceptions, needs fix writer.Flush(); Helpers.EndUndo(); } #endregion } }