private void drawImage(C1WordDocument word) { // ドキュメントフォルダーのパス string document_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); /** * 画像の表示処理 */ BitmapImage bitmap = new BitmapImage(); FileStream stream = File.OpenRead(document_path + System.IO.Path.DirectorySeparatorChar + "sample.png"); // BitmapImageの初期化 bitmap.BeginInit(); bitmap.StreamSource = stream; bitmap.EndInit(); var wb = new WriteableBitmap(bitmap); // 描画領域の指定 Rect rect = new Rect(30, 30, 120, 120); // 画像の描画 word.DrawImage(wb, rect); }
static Rect RenderTableHeader(C1WordDocument word, Font font, Rect rc, string[] fields) { // calculate cell width (same for all columns) Rect rcCell = rc; rcCell.Width = rc.Width / fields.Length; rcCell.Height = 0; // calculate cell height (max of all columns) foreach (string field in fields) { var height = word.MeasureString(field, font, rcCell.Width).Height; rcCell.Height = Math.Max(rcCell.Height, height); } rcCell.Height += 6; // add 6 point margin // render header cells var fmt = new StringFormat(); fmt.LineAlignment = VerticalAlignment.Center; foreach (string field in fields) { word.FillRectangle(Colors.Black, rcCell); word.DrawString(field, font, Colors.White, rcCell, fmt); rcCell = WordUtils.Offset(rcCell, rcCell.Width, 0); } // update rectangle and return it return(WordUtils.Offset(rc, 0, rcCell.Height)); }
// *********************************************************************** // C1WordDocuments for measure a paragraph // *********************************************************************** //public static double MeasureText(this C1WordDocument doc, string text, Font font, Rect rcPage, double height, RtfHorizontalAlignment align = RtfHorizontalAlignment.Undefined) //{ // // if it won't fit this page, do a page break // var sf = new StringFormat(); // switch (align) // { // case RtfHorizontalAlignment.Center: // sf.Alignment = HorizontalAlignment.Center; // break; // case RtfHorizontalAlignment.Justify: // sf.Alignment = HorizontalAlignment.Stretch; // break; // case RtfHorizontalAlignment.Right: // sf.Alignment = HorizontalAlignment.Right; // break; // } // //sf.FormatFlags |= StringFormatFlags. // rc.Height = doc.MeasureString(text, font, rcPage.Width, sf).Height; // if (rc.Bottom > rcPage.Bottom) // { // doc.PageBreak(); // rc.Y = rcPage.Top; // } // // add the paragraph // doc.AddParagraph(text, font, clr, align); // //doc.DrawString(text, font, Colors.Black, rc); // // add headings to outline // if (outline) // { // // top line // var paragraph = (RtfParagraph)doc.Current; // paragraph.TopBorderColor = clr; // paragraph.TopBorderStyle = RtfBorderStyle.Single; // paragraph.TopBorderWidth = 1f; // //doc.DrawLine(Colors.Black, rc.X, rc.Y, rc.Right, rc.Y); // doc.AddBookmark(text); // } // // add link target // if (linkTarget) // { // //doc.AddLink(text); // } // // update rectangle for next time // rc = Offset(rc, 0, rc.Height); // return rc; //} // *********************************************************************** // C1WordDocuments for Rect // *********************************************************************** // measure a paragraph, skip a page if it won't fit, render it into a rectangle, // and update the rectangle for the next paragraph. // // optionally mark the paragraph as an outline entry and as a link target. // // this routine will not break a paragraph across pages. for that, see the Text Flow sample. // public static Rect RenderParagraph(this C1WordDocument doc, string text, Font font, Rect rcPage, Rect rc, bool outline, bool linkTarget) { // if it won't fit this page, do a page break rc.Height = doc.MeasureString(text, font, rc.Width).Height; if (rc.Bottom > rcPage.Bottom) { doc.PageBreak(); rc.Y = rcPage.Top; } // draw the string doc.DrawString(text, font, Colors.Black, rc); // show bounds (to check word wrapping) //var p = Pen.GetPen(Colors.Orange); //doc.DrawRectangle(p, rc); // add headings to outline if (outline) { doc.DrawLine(Colors.Black, rc.X, rc.Y, rc.Right, rc.Y); //doc.AddBookmark(text, 0, rc.Y); } // add link target if (linkTarget) { //doc.AddTarget(text, rc); } // update rectangle for next time rc = Offset(rc, 0, rc.Height); return(rc); }
public static async void Save(this C1WordDocument doc) { FileSavePicker picker = new FileSavePicker(); picker.FileTypeChoices.Add(Strings.FileTypeChoicesTip, new List <string>() { ".docx" }); picker.FileTypeChoices.Add(Strings.AlternateFileTypeChoicesTip, new List <string>() { ".rtf" }); picker.DefaultFileExtension = ".docx"; picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; StorageFile file = await picker.PickSaveFileAsync(); if (file != null) { await doc.SaveAsync(file, file.Name.ToLower().EndsWith(".rtf")?FileFormat.Rtf : FileFormat.OpenXml); MessageDialog dlg = new MessageDialog(Strings.SaveLocationTip + " " + file.Path, "WordSamples"); dlg.Commands.Add(new UICommand("Open", new UICommandInvokedHandler((args) => { // to open the created file (using file extension) var success = Launcher.LaunchFileAsync(file); }))); dlg.Commands.Add(new UICommand("Cancel")); dlg.CancelCommandIndex = 2; await dlg.ShowAsync(); } }
//--------------------------------------------------------------------------------- #region ** text flow static void CreateDocumentTextFlow(C1WordDocument word) { // load long string from resource file string text = "Resource not found..."; using (var sr = new StreamReader(DataAccess.GetStream("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("Text Flow", titleFont, Colors.DarkOliveGreen, RtfHorizontalAlignment.Center); 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); } }
private void _btRead_Click(object sender, System.EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = GetFileFilter(); dlg.FilterIndex = 0; dlg.RestoreDirectory = true; if (dlg.ShowDialog() != DialogResult.OK) { return; } // create document C1WordDocument c1Word = new C1WordDocument(); _statusBar.Text = "Loading RTF document..."; c1Word.Load(dlg.FileName); _statusBar.Text = "Saving document..."; string fileName = GetFileName(c1Word, "reading.rtf"); c1Word.Save(fileName); Process.Start(fileName); _statusBar.Text = "Ready."; }
public BasicTextPage() { this.InitializeComponent(); this.Loaded += BasicText_Loaded; word = new C1WordDocument(PaperKind.Letter); word.Clear(); }
private void drawCircle(C1WordDocument word) { // 図形を描く範囲を指定 Rect rect = new Rect(30, 180, 120, 120); // 円を描く word.FillPie(Colors.Red, rect, 0, 360); }
public GraphicsPage() { this.InitializeComponent(); this.Loaded += GraphicsPage_Loaded; word = new C1WordDocument(PaperKind.Letter); word.Clear(); }
public TOCPage() { this.InitializeComponent(); this.Loaded += TOCPage_Loaded; _doc = new C1WordDocument(PaperKind.Letter); _doc.Clear(); }
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); } }
private void drawText(C1WordDocument word) { // テキストとフォントの設定 string text = "C1WordでWord形式のファイルを保存するサンプル"; Font font = new Font("Segoe UI Light", 20, RtfFontStyle.Bold); // パラグラフを追加 word.AddParagraph(text, font, Colors.Blue, RtfHorizontalAlignment.Justify); }
//--------------------------------------------------------------------------------- #region ** paper sizes 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(word.Info.Title, titleFont); var paragraph = (RtfParagraph)word.Current; paragraph.BottomBorderColor = Colors.Purple; paragraph.BottomBorderStyle = RtfBorderStyle.Dotted; paragraph.BottomBorderWidth = 3.0f; // 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("By default used Landscape A4", font); word.AddParagraph(string.Empty, font); // create one page with each paper size foreach (var fi in typeof(PaperKind).GetFields(BindingFlags.Static | BindingFlags.Public)) { // Silverlight/Phone doesn't have Enum.GetValues PaperKind pk = (PaperKind)fi.GetValue(null); // skip custom 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("PaperKind: [{0}];\r\nLandscape: [{1}];\r\nFont: [Tahoma 18pt]", pk, word.Landscape); word.AddParagraph(text); paragraph = (RtfParagraph)word.Current; paragraph.SetRectBorder(RtfBorderStyle.DashSmall, Colors.Aqua, 2.0f); word.AddParagraph(string.Empty); } }
public static async Task <MemoryStream> SaveToStream(this C1WordDocument doc) { var ms = new MemoryStream(); using (var imas = new InMemoryRandomAccessStream()) { await doc.SaveAsync(imas.AsStream(), FileFormat.OpenXml); } return(ms); }
public static Rect PageRectangle(this C1WordDocument doc, Thickness pageMargins) { var rc = new Rect(new Point(0, 0), doc.PageSize); //double left = Math.Min(rc.Width, rc.Left + pageMargins.Left); //double top = Math.Min(rc.Height, rc.Top + pageMargins.Top); double width = Math.Max(0, rc.Width - (pageMargins.Left + pageMargins.Right)); double height = Math.Max(0, rc.Height - (pageMargins.Top + pageMargins.Bottom)); return(new Rect(rc.Left, rc.Top, width, height)); }
public static Rect PageRectangle(this C1WordDocument doc, Thickness pageMargins) { var sz = doc.PageSize; double left = Math.Min(sz.Width, pageMargins.Left); double top = Math.Min(sz.Height, pageMargins.Top); double width = Math.Max(0, sz.Width - (pageMargins.Left + pageMargins.Right)); double height = Math.Max(0, sz.Height - (pageMargins.Top + pageMargins.Bottom)); return(new Rect(left, top, width, height)); }
static void CreateDocumentGraphics(C1WordDocument word) { // set up to draw word.Landscape = true; Rect rc = new Rect(0, 100, 300, 300); string text = Strings.DocumentGraphicsText; Font font = new Font("Segoe UI Light", 16, RtfFontStyle.Italic); // add warning paragraph word.AddParagraph(Strings.GraphicsWarning, font, Colors.DarkGray, RtfHorizontalAlignment.Left); var paragraph = (RtfParagraph)word.Current; paragraph.BottomBorderColor = Colors.Red; paragraph.BottomBorderStyle = RtfBorderStyle.Dotted; paragraph.BottomBorderWidth = 1f; word.AddParagraph(string.Empty); // draw to word document int penWidth = 0; byte penRGB = 0; word.FillPie(Colors.DarkRed, rc, 0, 20f); word.FillPie(Colors.Green, rc, 20f, 30f); word.FillPie(Colors.Teal, rc, 60f, 12f); word.FillPie(Colors.Orange, rc, -80f, -20f); for (float startAngle = 0; startAngle < 360; startAngle += 40) { Color penColor = Color.FromArgb(0xff, penRGB, penRGB, penRGB); Pen pen = new Pen(penColor, penWidth++); penRGB = (byte)(penRGB + 20); word.DrawArc(pen, rc, startAngle, 40f); } word.DrawRectangle(Colors.Red, rc); word.DrawString(text, font, Colors.Black, rc); // show a Bezier curve var pts = new Point[] { new Point(400, 100), new Point(420, 30), new Point(500, 140), new Point(530, 20), }; // draw Bezier word.DrawBeziers(new Pen(Colors.Green, 4), pts); // show Bezier control points word.DrawPolyline(Colors.Gray, pts); foreach (Point pt in pts) { word.FillRectangle(Colors.Orange, pt.X - 2, pt.Y - 2, 4, 4); } // title word.DrawString(Strings.Bezier, font, Colors.Black, new Rect(500, 150, 100, 100)); }
static Rect RenderTableRow(C1WordDocument word, Font font, Font hdrFont, Rect rcPage, Rect rc, string[] fields, DataRow dr) { // calculate cell width (same for all columns) Rect rcCell = rc; rcCell.Width = rc.Width / fields.Length; rcCell.Height = 0; // calculate cell height (max of all columns) rcCell = WordUtils.Inflate(rcCell, -4, 0); foreach (string field in fields) { string text = dr[field].ToString(); var height = word.MeasureString(text, font, rcCell.Width).Height; rcCell.Height = Math.Max(rcCell.Height, height); } rcCell = WordUtils.Inflate(rcCell, 4, 0); // add 4 point margin rcCell.Height += 2; // break page if we have to if (rcCell.Bottom > rcPage.Bottom) { word.PageBreak(); rc = RenderTableHeader(word, hdrFont, rcPage, fields); rcCell.Y = rc.Y; } // center vertically just to show how StringFormat fmt = new StringFormat(); fmt.LineAlignment = VerticalAlignment.Center; // render data cells foreach (string field in fields) { // get content string text = dr[field].ToString(); // set horizontal alignment double d; fmt.Alignment = (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out d)) ? HorizontalAlignment.Right : HorizontalAlignment.Left; // render cell word.DrawRectangle(Colors.LightGray, rcCell); rcCell = WordUtils.Inflate(rcCell, -4, 0); word.DrawString(text, font, Colors.Black, rcCell, fmt); rcCell = WordUtils.Inflate(rcCell, 4, 0); rcCell = WordUtils.Offset(rcCell, rcCell.Width, 0); } // update rectangle and return it return(WordUtils.Offset(rc, 0, rcCell.Height)); }
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); } }
private void _btFonts_Click(object sender, System.EventArgs e) { // create document C1WordDocument c1Word = new C1WordDocument(); c1Word.Info.Title = "All Fonts"; _statusBar.Text = "Creating document..."; // add title c1Word.AddParagraph(c1Word.Info.Title, new Font("Tahoma", 24, FontStyle.Bold), Color.Black); // draw text in many fonts Font font = new Font("Tahoma", 9); InstalledFontCollection ifc = new InstalledFontCollection(); foreach (FontFamily ff in ifc.Families) { // create font Font sample = null; foreach (FontStyle fs in Enum.GetValues(typeof(FontStyle))) { if (ff.IsStyleAvailable(fs)) { sample = new Font(ff.Name, 9, fs); break; } } if (sample == null) { continue; } // show font c1Word.AddParagraph(ff.Name, font, Color.Black); c1Word.AddParagraph("The quick brown fox jumped over the lazy dog. 1234567890!", sample, Color.Black); sample.Dispose(); // TODO: add split bar or line // TODO: add new page if necessary // c1Word.PageBreak(); } // save and show document _statusBar.Text = "Saving document..."; string fileName = GetFileName(c1Word, "fonts.rtf"); c1Word.Save(fileName); Process.Start(fileName); _statusBar.Text = "Ready."; }
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 void _btMetafile_Click(object sender, System.EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Extended metafile (*.emf)|*.emf|Windows metafile (*.wmf)|*.wmf|All files (*.*)|*.*"; dlg.FilterIndex = 1; dlg.RestoreDirectory = true; if (dlg.ShowDialog() != DialogResult.OK) { return; } // create document C1WordDocument c1Word = new C1WordDocument(); c1Word.Info.Title = "Convert metafile to RTF example"; _statusBar.Text = "Creating document..."; Image img; string ext = Path.GetExtension(dlg.FileName); if (ext == ".wmf" || ext == ".emf") { img = Metafile.FromFile(dlg.FileName); } else { throw new FormatException("Not metafile."); } c1Word.DrawMetafile((Metafile)img); c1Word.PageBreak(); c1Word.AddPicture(img, RtfHorizontalAlignment.Left); c1Word.LineBreak(); Font font = new Font("Arial", 10, FontStyle.Regular); c1Word.AddParagraph(dlg.FileName, font, Color.Black); _statusBar.Text = "Saving document..."; string fileName = GetFileName(c1Word, "metafile.rtf"); c1Word.Save(fileName); Process.Start(fileName); _statusBar.Text = "Ready."; }
private void _btCurve_Click(object sender, System.EventArgs e) { // create document C1WordDocument c1Word = new C1WordDocument(); c1Word.Info.Title = "Various curves sample"; _statusBar.Text = "Creating document..."; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; RectangleF rc = new RectangleF(250, 100, 150, 20); Font font = new Font("Tachoma", 12, FontStyle.Italic | FontStyle.Underline); c1Word.DrawString("Curves sample", font, Color.Black, rc, sf); //// curve //PointF[] pts = new PointF[7]; //pts[0] = new PointF(191.1f, 172.3f); //pts[1] = new PointF(229.1f, 205.3f); //pts[2] = new PointF(267.15f, 238.3f); //pts[3] = new PointF(296.4f, 235.3f); //pts[4] = new PointF(325.65f, 232.3f); //pts[5] = new PointF(346.1f, 193.3f); //pts[6] = new PointF(366.6f, 154.3f); //c1Word.DrawBeziers(Pens.HotPink, pts); //// pie //rc = new RectangleF(120, 100, 150, 80); //c1Word.FillPie(Brushes.Yellow, rc, 0, 90); //c1Word.DrawPie(Pens.Indigo, rc, 0, 90); // arc rc = new RectangleF(120, 100, 150, 80); c1Word.DrawArc(Pens.Red, rc, 0, 90); //// arc //rc = new RectangleF(320, 300, 90, 45); //c1Word.DrawArc(Pens.Blue, rc, 270, 90); _statusBar.Text = "Saving document..."; string fileName = GetFileName(c1Word, "curves.rtf"); c1Word.Save(fileName); Process.Start(fileName); _statusBar.Text = "Ready."; }
private void _tbPicture_Click(object sender, System.EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Gif (*.gif)|*.gif|Png (*.png)|*.png|Jpeg (*.jpg)|*.jpg|Bitmap (*.bmp)|*.bmp|Windows metafile (*.wmf)|*.wmf|All files (*.*)|*.*"; dlg.FilterIndex = 6; dlg.RestoreDirectory = true; if (dlg.ShowDialog() != DialogResult.OK) { return; } // create document C1WordDocument c1Word = new C1WordDocument(); c1Word.Info.Title = "Show any picture sample"; _statusBar.Text = "Creating document..."; Font font = new Font("Arial", 14, FontStyle.Bold); c1Word.AddParagraph("Picture:", font, Color.Chocolate); Image img; string ext = Path.GetExtension(dlg.FileName); if (ext == ".wmf" || ext == ".emf") { img = Metafile.FromFile(dlg.FileName); } else { img = new Bitmap(dlg.FileName); } c1Word.AddPicture(img, RtfHorizontalAlignment.Left); c1Word.LineBreak(); font = new Font("Arial", 10, FontStyle.Regular); c1Word.AddParagraph(dlg.FileName, font, Color.Blue); _statusBar.Text = "Saving document..."; string fileName = GetFileName(c1Word, "picture.rtf"); c1Word.Save(fileName); Process.Start(fileName); _statusBar.Text = "Ready."; }
string GetFileName(C1WordDocument c1Word, string fileName) { c1Word.Info.Author = "C1WordCreator"; c1Word.Info.Company = "GrapeCity"; if (_format.Equals(".rtf")) { return(Path.GetDirectoryName(Application.ExecutablePath) + '\\' + fileName); } if (_check2007.Checked) { c1Word.ShapesWord2007Compatible = true; } return(Path.GetDirectoryName(Application.ExecutablePath) + '\\' + Path.GetFileNameWithoutExtension(fileName) + _format); }
//--------------------------------------------------------------------------------- #region ** images static void CreateDocumentImages(C1WordDocument word) { // calculate page rect (discounting margins) Rect rcPage = WordUtils.PageRectangle(word); // load image into writeable bitmap BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = DataAccess.GetStream("borabora.jpg"); bi.EndInit(); var wb = new WriteableBitmap(bi); // center image on page preserving aspect ratio //word.DrawImage(wb, rcPage, ContentAlignment.MiddleCenter, Stretch.Uniform); word.DrawImage(wb, rcPage); }
string GetFileName(C1WordDocument c1Word, string fileName) { c1Word.Info.Author = "C1WordCreator"; c1Word.Info.Company = "GrapeCity"; if (_format.Equals(".rtf")) { return(GetFullFileName(fileName)); } if (_check2007.Checked) { c1Word.ShapesWord2007Compatible = true; } return(GetFullFileName(Path.GetFileNameWithoutExtension(fileName) + _format)); }
void CreateDocumentVisualTree(C1WordDocument rtf, FrameworkElement targetElement) { // set up to render var font = new Font("Courier", 14); var img = new WriteableBitmap(CreateBitmap(targetElement)); // go render bool firstPage = true; foreach (Stretch stretch in new Stretch[] { Stretch.Fill, Stretch.None, Stretch.Uniform, Stretch.UniformToFill }) { // add page break if (!firstPage) { //rtf.NewPage(); } firstPage = false; // set up to render var alignment = ContentAlignment.TopLeft; //var rc = WordUtils.Inflate(rtf.PageRectangle, -72, -72); var sz = rtf.PageSize; var rc = new Rect(72, 72, sz.Width - 144, sz.Height - 144); rc.Height /= 2; // render element as image rtf.DrawString("Element as Image, Stretch: " + stretch.ToString(), font, Colors.Black, rc); rc = WordUtils.Inflate(rc, -20, -20); //rtf.DrawImage(img, rc, alignment, stretch); rtf.DrawImage(img, rc); rtf.DrawRectangle(Colors.Green, rc); rc = WordUtils.Inflate(rc, +20, +20); rtf.DrawRectangle(Colors.Green, rc); // move to bottom of the page rc = WordUtils.Offset(rc, 0, rc.Height + 20); // render element rtf.DrawString("Element as VisualTree, Stretch: " + stretch.ToString(), font, Colors.Black, rc); rc = WordUtils.Inflate(rc, -20, -20); rtf.DrawElement(targetElement, rc, alignment, stretch); rtf.DrawRectangle(Colors.Green, rc); rc = WordUtils.Inflate(rc, +20, +20); rtf.DrawRectangle(Colors.Green, rc); } }
//--------------------------------------------------------------------------------- #region ** graphics static void CreateDocumentGraphics(C1WordDocument rtf) { // set up to draw Rect rc = new Rect(100, 100, 300, 200); string text = "Hello world of .NET Graphics and Word/RTF.\r\nNice to meet you."; Font font = new Font("Times New Roman", 12, RtfFontStyle.Italic | RtfFontStyle.Underline); // draw to pdf document int penWidth = 0; byte penRGB = 0; rtf.FillPie(Colors.Red, rc, 0, 20f); rtf.FillPie(Colors.Green, rc, 20f, 30f); rtf.FillPie(Colors.Blue, rc, 60f, 12f); rtf.FillPie(Colors.Orange, rc, -80f, -20f); for (float startAngle = 0; startAngle < 360; startAngle += 40) { Color penColor = Color.FromArgb(0xff, penRGB, penRGB, penRGB); Pen pen = new Pen(penColor, penWidth++); penRGB = (byte)(penRGB + 20); rtf.DrawArc(pen, rc, startAngle, 40f); } rtf.DrawRectangle(Colors.Red, rc); rtf.DrawString(text, font, Colors.Black, rc); // show a Bezier curve var pts = new Point[] { new Point(400, 200), new Point(420, 130), new Point(500, 240), new Point(530, 120), }; // draw Bezier rtf.DrawBeziers(new Pen(Colors.Blue, 4), pts); // show Bezier control points rtf.DrawPolyline(Colors.Gray, pts); foreach (Point pt in pts) { rtf.FillRectangle(Colors.Red, pt.X - 2, pt.Y - 2, 4, 4); } // title rtf.DrawString("Simple Bezier", font, Colors.Black, new Rect(500, 150, 100, 100)); }
private void btSimple_Click(object sender, System.EventArgs e) { // create document C1WordDocument c1Word = new C1WordDocument(); c1Word.Info.Title = "Simple sample"; _statusBar.Text = "Creating document..."; Font font = new Font("Tahoma", 24, FontStyle.Italic); c1Word.AddParagraph("Hello World!", font, Color.Blue); _statusBar.Text = "Saving document..."; string fileName = GetFileName(c1Word, "simple.rtf"); c1Word.Save(fileName); Process.Start(fileName); _statusBar.Text = "Ready."; }