//static string _extension = ".docx"; public static void HandleButtonClick(object sender, RoutedEventArgs e) { // get stream to save to var dlg = new SaveFileDialog(); dlg.FileName = "document"; dlg.DefaultExt = ".docx"; dlg.Filter = WordUtils.GetFileFilter(_extension); var dr = dlg.ShowDialog(); if (!dr.HasValue || !dr.Value) { return; } // get sender button var btn = sender as Button; // create document var word = new C1WordDocument(); word.Clear(); // set document info var di = word.Info; di.Author = "ComponentOne"; di.Subject = "C1.WPF.Word demo."; di.Title = (string)btn.Content; // create document switch (di.Title) { case "Quotes": CreateDocumentQuotes(word); break; case "Tables": CreateDocumentTables(word); break; case "Images": CreateDocumentImages(word); break; case "Paper Sizes": CreateDocumentPaperSizes(word); break; case "Table of Contents": CreateDocumentTOC(word); break; case "Text Flow": CreateDocumentTextFlow(word); break; case "Text": CreateDocumentText(word); break; case "Graphics": CreateDocumentGraphics(word); break; } // render footers // standard footer var text = string.Format("C1.WPF.Word: {0}, page {1} of {2}", di.Title, "|", "|"); var paragraph = new RtfParagraph(word.CurrentSection.Footer); paragraph.Alignment = RtfHorizontalAlignment.Right; int count = 0; foreach (var part in text.Split('|')) { if (!string.IsNullOrEmpty(part)) { paragraph.Add(new RtfString(part)); } switch (count) { case 0: paragraph.Add(new RtfPageField()); break; case 1: paragraph.Add(new RtfNumPagesField()); break; } count++; } word.CurrentSection.Footer.Add(paragraph); // this reopens each page and adds content to them (now we know the page count). //var font = new Font("Arial", 8, RtfFontStyle.Bold); //var fmt = new StringFormat(); //fmt.Alignment = HorizontalAlignment.Right; //fmt.LineAlignment = VerticalAlignment.Bottom; //for (int page = 0; page < rtf.Pages.Count; page++) //{ // rtf.CurrentPage = page; // var text = string.Format("C1.WPF.Word: {0}, page {1} of {2}", // di.Title, // page + 1, // rtf.Pages.Count); // rtf.DrawString( // text, // font, // Colors.DarkGray, // WordUtils.Inflate(rtf.PageRectangle, -72, -36), // fmt); //} // save document using (var stream = dlg.OpenFile()) { word.Save(stream, dlg.FileName.ToLower().EndsWith(".docx") ? FileFormat.OpenXml : FileFormat.Rtf); } MessageBox.Show("Word Document saved to " + dlg.SafeFileName); }
public static void SetDocumentInfo(this C1WordDocument doc, string title, bool graphicFooter = false) { // set document info var di = doc.Info; di.Author = Strings.DocumentAuthor; di.Subject = Strings.DocumentSubject; di.Title = title; // footer font var font = new Font("Arial", 8, RtfFontStyle.Bold); var fmt = new StringFormat(); fmt.Alignment = HorizontalAlignment.Right; fmt.LineAlignment = VerticalAlignment.Bottom; // render footers if (graphicFooter) { // this reopens each page and adds content to them (now we know the page count). for (int page = 0; page < doc.PageCount(); page++) { doc.CurrentPage(page); var text = string.Format(Strings.Documentfooter, di.Title, page + 1, doc.PageCount()); doc.DrawString( text, font, Colors.DarkGray, WordUtils.Inflate(doc.PageRectangle(), -72, -36), fmt); } } else { // standard footer var text = string.Format(Strings.Documentfooter, di.Title, "|", "|"); var paragraph = new RtfParagraph(doc.CurrentSection.Footer); paragraph.Alignment = RtfHorizontalAlignment.Right; int count = 0; foreach (var part in text.Split('|')) { if (!string.IsNullOrEmpty(part)) { paragraph.Add(new RtfString(part)); } switch (count) { case 0: paragraph.Add(new RtfPageField()); break; case 1: paragraph.Add(new RtfNumPagesField()); break; } count++; } doc.CurrentSection.Footer.Add(paragraph); } }