async void GraphicsPage_Loaded(object sender, RoutedEventArgs e) { progressRing.IsActive = true; CreateDocumentGraphics(word); WordUtils.SetDocumentInfo(word, Strings.GraphicsDocumentTitle); c1RichTextBox1.Document = new RtfFilter().ConvertToDocument(word.ToRtfText()); progressRing.IsActive = false; }
async void TOCPage_Loaded(object sender, RoutedEventArgs e) { progressRing.IsActive = true; CreateDocumentTOC(_doc); WordUtils.SetDocumentInfo(_doc, Strings.TableOfContentsDocumentTitle); c1RichTextBox1.Document = new RtfFilter().ConvertToDocument(_doc.ToRtfText()); progressRing.IsActive = false; }
static void CreateDocumentPaperSizes(C1WordDocument word) { // landscape for first page bool landscape = true; word.Landscape = landscape; // add title Font titleFont = new Font("Tahoma", 24, RtfFontStyle.Bold); Rect rc = WordUtils.PageRectangle(word); word.AddParagraph(Strings.PaperSizesFirstPage, titleFont); var paragraph = (RtfParagraph)word.Current; paragraph.BottomBorderColor = Colors.Purple; paragraph.BottomBorderStyle = RtfBorderStyle.Dotted; paragraph.BottomBorderWidth = 3.0f; // view warning Font warningFont = new Font("Arial", 14); word.AddParagraph(Strings.PaperSizesWarning, warningFont, Colors.Blue); // create constant font and StringFormat objects Font font = new Font("Tahoma", 18); StringFormat sf = new StringFormat(); sf.Alignment = HorizontalAlignment.Center; sf.LineAlignment = VerticalAlignment.Center; word.AddParagraph(string.Empty, font, Colors.Black); // create one page with each paper size foreach (PaperKind pk in Enum.GetValues(typeof(PaperKind))) { // skip custom page size if (pk == PaperKind.Custom) { continue; } // add new page for every page after the first one word.PageBreak(); // set paper kind and orientation var section = new RtfSection(pk); word.Add(section); landscape = !landscape; word.Landscape = landscape; // add some content on the page string text = string.Format(Strings.StringFormatTwoArg, pk, word.Landscape); word.AddParagraph(text); paragraph = (RtfParagraph)word.Current; paragraph.SetRectBorder(RtfBorderStyle.DashSmall, Colors.Aqua, 2.0f); word.AddParagraph(string.Empty); } }
static void CreateDocumentQuotes(C1WordDocument word) { // calculate page rect (discounting margins) Rect rcPage = WordUtils.PageRectangle(word); var height = rcPage.Top; // initialize fonts Font hdrFont = new Font("Arial", 14, RtfFontStyle.Bold); Font titleFont = new Font("Arial", 20, RtfFontStyle.Bold); Font txtFont = new Font("Times New Roman", 10, RtfFontStyle.Italic); // add title paragraph word.AddParagraph(Strings.QuotesAuthors, titleFont, Colors.DeepPink, RtfHorizontalAlignment.Center); word.AddParagraph(string.Empty); height += 2 * word.MeasureString(Strings.QuotesAuthors, titleFont, rcPage.Width).Height; // build document foreach (string s in GetQuotes()) { // quote var authorQuote = s.Split('\t'); var author = authorQuote[0]; var text = authorQuote[1]; // if it won't fit this page, do a page break var h = word.MeasureString(author, hdrFont, rcPage.Width).Height; var sf = new StringFormat(); sf.Alignment = HorizontalAlignment.Stretch; h += word.MeasureString(text, txtFont, rcPage.Width, sf).Height; h += 20; if (height + h > rcPage.Bottom) { word.PageBreak(); height = rcPage.Top; } height += h; // render header (author) word.AddParagraph(author, hdrFont, Colors.Black, RtfHorizontalAlignment.Left); var paragraph = (RtfParagraph)word.Current; paragraph.TopBorderColor = Colors.Black; paragraph.TopBorderStyle = RtfBorderStyle.Single; paragraph.TopBorderWidth = 1f; // render body text (quote) word.AddParagraph(text, txtFont, Colors.DarkSlateGray, RtfHorizontalAlignment.Left); paragraph = (RtfParagraph)word.Current; paragraph.LeftIndent = 40.0f; word.AddParagraph(string.Empty); } }
async Task CreateDocumentImages(C1WordDocument word) { // add first image paragraph Font font = new Font("Segoe UI Light", 16, RtfFontStyle.Italic); word.AddParagraph(Strings.ImageFirstParagraph, font, Colors.DarkGray, RtfHorizontalAlignment.Left); var paragraph = (RtfParagraph)word.Current; paragraph.BottomBorderColor = Colors.Gray; paragraph.BottomBorderStyle = RtfBorderStyle.Single; paragraph.BottomBorderWidth = 1f; word.AddParagraph(string.Empty); // calculate page rect (discounting margins) Rect rcPage = WordUtils.PageRectangle(word); InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream(); #if !true // load image into C1 bitmap var wb = new C1.Xaml.Bitmap.C1Bitmap(); var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///WordSamplesLib/Assets/pic.jpg")); await wb.LoadAsync(file); #else // load image into writeable bitmap WriteableBitmap wb = new WriteableBitmap(880, 660); var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///WordSamplesLib/Assets/pic.jpg")); wb.SetSource(await file.OpenReadAsync()); #endif // add image without scaling by center word.AddPicture(wb, RtfHorizontalAlignment.Center); word.LineBreak(); word.AddParagraph(string.Empty); // add first image paragraph font = new Font("Segoe UI Light", 12, RtfFontStyle.Regular); word.AddParagraph(Strings.ImageSecondParagraph, font, Colors.DeepSkyBlue, RtfHorizontalAlignment.Left); paragraph = (RtfParagraph)word.Current; paragraph.BottomBorderColor = Colors.BlueViolet; paragraph.BottomBorderStyle = RtfBorderStyle.Dotted; paragraph.BottomBorderWidth = 2f; word.AddParagraph(string.Empty); // add image with scaling by right word.AddPicture(wb, RtfHorizontalAlignment.Right); var picture = (RtfPicture)word.Current; picture.Height = 33; picture.Width = 44; }
private async void btnRender_Click(object sender, RoutedEventArgs e) { // document parameters _doc.Clear(); progressRing.IsActive = true; _doc.Landscape = true; var sz = _doc.PageSize; var rc = new Rect(0, 0, sz.Width - _doc.MainSection.LeftMargin - _doc.MainSection.RightMargin, sz.Height - _doc.MainSection.TopMargin - _doc.MainSection.BottomMargin); // draw every UI elements inside the panel in word document. await _doc.DrawElement(panel, rc); // save document WordUtils.SetDocumentInfo(_doc, Strings.RenderUIDocumentTitle); WordUtils.Save(_doc); progressRing.IsActive = false; }
static void CreateDocumentTextFlow(C1WordDocument word) { // load long string from resource file string text = Strings.ResourceNotFound; using (var sr = new StreamReader(typeof(BasicTextPage).GetTypeInfo().Assembly.GetManifestResourceStream("WordSamples.Resources.flow.txt"))) { text = sr.ReadToEnd(); } // content rectangle Rect rcPage = WordUtils.PageRectangle(word); // create two columns for the text var columnWidth = (float)(rcPage.Width - 30) / 2; word.MainSection.Columns.Clear(); word.MainSection.Columns.Add(new RtfColumn(columnWidth)); word.MainSection.Columns.Add(new RtfColumn(columnWidth)); // add title Font titleFont = new Font("Tahoma", 14, RtfFontStyle.Bold); Font bodyFont = new Font("Tahoma", 11); word.AddParagraph(Strings.MsWordFormats, titleFont, Colors.DarkOliveGreen, RtfHorizontalAlignment.Center); word.AddParagraph(string.Empty); // first paragraph about C1Word object model var paragraph = word.GetParagraph(string.Empty, bodyFont); var hyperlink = new RtfHyperlink(Strings.WordSamplesWordLink, Colors.Blue, RtfUnderlineStyle.Single); hyperlink.Add(new RtfString(Strings.WordSamplesWordTitle)); paragraph.Add(hyperlink); word.Add(paragraph); word.AddParagraph(string.Empty); // render string spanning columns and pages foreach (var part in text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)) { word.AddParagraph(part, bodyFont, Colors.Black, RtfHorizontalAlignment.Justify); } }
static void CreateDocumentTOC(C1WordDocument doc) { // calculate page rect (discounting margins) Rect rcPage = WordUtils.PageRectangle(doc); var height = rcPage.Top; // initialize fonts Font titleFont = new Font("Tahoma", 20, RtfFontStyle.Bold); Font headerFont = new Font("Arial", 14, RtfFontStyle.Bold); Font bodyFont = new Font("Times New Roman", 11); // add title doc.AddParagraph(Strings.TableOfContentsDocumentTitle, titleFont, Colors.DeepPink, RtfHorizontalAlignment.Center); doc.AddParagraph(string.Empty); // create context of the document var pageIndex = 2; var data = new List <List <string> >(); for (int i = 0; i < 30; i++) { // iniialization data var list = new List <string>(); data.Add(list); // create it header (as a link target and outline entry) string header = string.Format("{0}. {1}", i + 1, BuildRandomTitle()); var h = 2 * doc.MeasureString(header, headerFont, rcPage.Width).Height; // create some text for (int j = 0; j < 3 + _rnd.Next(20); j++) { // some paragraph string text = BuildRandomParagraph(); var sf = new StringFormat(); sf.Alignment = HorizontalAlignment.Stretch; h += doc.MeasureString(text, bodyFont, rcPage.Width, sf).Height; h += 6; // test next page if (height + h > rcPage.Bottom) { pageIndex++; height = rcPage.Top; } height += h; h = 0; // page index for header if (j == 0) { list.Add(pageIndex.ToString()); list.Add(header.ToString()); } // page index for paragraph list.Add(pageIndex.ToString()); list.Add(text.ToString()); } // last line height += 20; } // render Table of Contents RtfTable table = new RtfTable(data.Count, 2); doc.Add(table); for (int row = 0; row < data.Count; row++) { // get bookmark info var list = data[row]; var page = list[0]; var header = list[1]; // add cells table.Rows[row].Cells[0].Content.Add(new RtfString(header)); //var hlink = new RtfHyperlink(string.Format("hdr{0}", 1 + row)); //hlink.Content.Add(new RtfString(page)); //table.Rows[row].Cells[1].Content.Add(hlink); table.Rows[row].Cells[1].Content.Add(new RtfString(page)); // set attributes table.Rows[row].Cells[0].Alignment = ContentAlignment.BottomLeft; table.Rows[row].Cells[1].Alignment = ContentAlignment.BottomRight; table.Rows[row].BottomBorderWidth = 1; table.Rows[row].BottomBorderStyle = RtfBorderStyle.Dotted; } // next page doc.PageBreak(); pageIndex = 1; // render contents foreach (var list in data) { for (int i = 0; i < list.Count; i += 2) { int page = int.Parse(list[i]); if (page != pageIndex) { doc.PageBreak(); pageIndex = page; } var text = list[i + 1]; if (i == 0) { // header //doc.AddBookmark(string.Format("hdr{0}", 1 + data.IndexOf(list))); doc.AddParagraph(text, headerFont, Colors.DarkGray, RtfHorizontalAlignment.Center); var paragraph = (RtfParagraph)doc.Current; paragraph.TopBorderColor = Colors.DarkGray; paragraph.TopBorderStyle = RtfBorderStyle.Single; paragraph.TopBorderWidth = 1f; doc.AddParagraph(string.Empty, bodyFont, Colors.Black); } else { // context doc.AddParagraph(text, bodyFont, Colors.Black, RtfHorizontalAlignment.Justify); } } doc.AddParagraph(string.Empty, bodyFont, Colors.Black); } }
private void btnSave_Click(object sender, RoutedEventArgs e) { WordUtils.Save(_doc); }
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); } }