Beispiel #1
0
        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.";
        }
Beispiel #2
0
        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.";
        }
Beispiel #3
0
        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.";
        }
Beispiel #4
0
        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.";
        }
Beispiel #5
0
        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.";
        }
Beispiel #6
0
        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.";
        }
Beispiel #7
0
        private void _btTable_Click(object sender, System.EventArgs e)
        {
            // create document
            C1WordDocument c1Word = new C1WordDocument();

            c1Word.Info.Title = "Sample with tables";
            _statusBar.Text   = "Creating document...";

            int      rows  = 4;
            int      cols  = 2;
            RtfTable table = new RtfTable(rows, cols);

            table.BottomBorderStyle = table.LeftBorderStyle = table.RightBorderStyle = table.TopBorderStyle = RtfBorderStyle.Single;
            table.BottomBorderWidth = table.LeftBorderWidth = table.RightBorderWidth = table.TopBorderWidth = 1;
            c1Word.Add(table);
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    var cell = table.Rows[row].Cells[col];
                    cell.BottomBorderStyle = cell.LeftBorderStyle = cell.RightBorderStyle = cell.TopBorderStyle = RtfBorderStyle.Single;
                    cell.BottomBorderWidth = cell.LeftBorderWidth = cell.RightBorderWidth = cell.TopBorderWidth = 1;
                    RtfParagraph paragraph = new RtfParagraph();
                    paragraph.Content.Add(new RtfString(string.Format("table cell {0}:{1}.", row, col)));
                    cell.Content.Add(paragraph);
                }
            }

            Font font = new Font("Arial", 10, FontStyle.Regular);

            c1Word.AddParagraph("Simple table.", font, Color.Blue);

            _statusBar.Text = "Saving document...";
            string fileName = GetFileName(c1Word, "table.rtf");

            c1Word.Save(fileName);
            Process.Start(fileName);
            _statusBar.Text = "Ready.";
        }
Beispiel #8
0
        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);
#if DEBUGx
            foreach (RtfObject obj in c1Word.MainSection.Content)
            {
                if (obj is RtfParagraph)
                {
                    var paragraph = obj as RtfParagraph;
                    foreach (var o in paragraph.Content)
                    {
                        if (o is RtfString)
                        {
                            ((RtfString)o).Text = "<skip>";
                        }
                    }
                }
            }
#endif
            _statusBar.Text = "Saving document...";
            string fileName = GetFileName(c1Word, "reading.rtf");
            c1Word.Save(fileName);
            Process.Start(fileName);
            _statusBar.Text = "Ready.";
        }
Beispiel #9
0
        public MainWindow()
        {
            InitializeComponent();

            // ドキュメントクラスを作成
            C1WordDocument word = new C1WordDocument();

            word.Clear();

            // ドキュメント情報を設定します
            word.Info.Author  = "CodeZine";
            word.Info.Subject = "C1Wordサンプル";
            word.Info.Title   = "サンプル";

            /**
             * テキストの表示処理
             */
            this.drawText(word);

            /**
             * 画像の表示処理
             */
            this.drawImage(word);

            /**
             * 図形の描画
             */
            this.drawCircle(word);

            // ドキュメントフォルダーのパス
            string document_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            // ファイル名
            string file_name = "sample.docx";

            // 保存処理
            word.Save(document_path + System.IO.Path.DirectorySeparatorChar + file_name);
        }
Beispiel #10
0
        //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);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // get stream to save to
            var dlg = new SaveFileDialog();

            dlg.DefaultExt = ".rtf";
            var dr = dlg.ShowDialog();

            if (!dr.HasValue || !dr.Value)
            {
                return;
            }

            // get sender button
            var btn = sender as Button;

            // create document
            var rtf = new C1WordDocument();

            rtf.Clear();

            // set document info
            var di = rtf.Info;

            di.Author  = "ComponentOne";
            di.Subject = "C1.WPF.Word demo.";
            di.Title   = "Experimental VisualTree Exporter for RTF";
            var count = 5;

            // walk visual tree
            CreateDocumentVisualTree(rtf, content);

            // render footers
            // 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 < count; page++)
            {
                //rtf.CurrentPage = page;
                var sz   = rtf.PageSize;
                var rc   = new Rect(72, 72, sz.Width - 144, sz.Height - 144);
                var text = string.Format("C1.WPF.Rtf: {0}, page {1} of {2}",
                                         di.Title,
                                         page + 1,
                                         count);
                var r = new Rect(72, 36, sz.Width - 144, sz.Height - 72);
                rtf.DrawString(
                    text,
                    font,
                    Colors.DarkGray,
                    r,
                    fmt);
            }

            // save document
            using (var stream = dlg.OpenFile())
            {
                rtf.Save(stream, FileFormat.Rtf);
            }
            MessageBox.Show("Word Document saved to " + dlg.SafeFileName);
        }
Beispiel #12
0
        private void _btGraphics_Click(object sender, System.EventArgs e)
        {
            // create document
            C1WordDocument c1Word = new C1WordDocument();

            c1Word.Info.Title = "Graphics primitives sample";
            _statusBar.Text   = "Creating document...";

            RectangleF rc    = new RectangleF(250, 100, 200, 200);
            Bitmap     image = new Bitmap(GetManifestResource("Word.picture.jpg"));

            c1Word.DrawImage(image, rc);

            StringFormat sf = new StringFormat();

            sf.Alignment     = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            rc = new RectangleF(250, 100, 150, 20);
            Font font = new Font("Arial", 14, FontStyle.Italic);

            c1Word.DrawString(c1Word.Info.Title, font, Color.DeepPink, rc, sf);

            c1Word.DrawLine(Pens.Green, 200, 190, 400, 190);

            rc = new RectangleF(150, 150, 190, 80);
            using (Pen pen = new Pen(Brushes.Blue, 5.0f))
            {
                c1Word.DrawRectangle(pen, rc);
            }
            c1Word.FillRectangle(Color.Gold, rc);
            c1Word.ShapeFillOpacity(50);
            c1Word.ShapeRotation(25);

            rc = new RectangleF(300, 150, 80, 80);
            c1Word.DrawEllipse(Pens.Red, rc);
            c1Word.FillEllipse(Color.Pink, rc);
            c1Word.ShapeFillOpacity(70);

            PointF[] pts = new PointF[4];
            pts[0] = new PointF(200, 200);
            pts[1] = new PointF(250, 300);
            pts[2] = new PointF(330, 250);
            pts[3] = new PointF(340, 140);
            c1Word.DrawPolyline(Pens.BlueViolet, pts);

            sf               = new StringFormat();
            sf.Alignment     = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Far;
            sf.FormatFlags  |= StringFormatFlags.DirectionVertical;
            rc               = new RectangleF(450, 150, 25, 75);
            font             = new Font("Verdana", 12, FontStyle.Bold);
            c1Word.DrawString("Vertical", font, Color.Black, rc, sf);

            pts    = new PointF[4];
            pts[0] = new PointF(372, 174);
            pts[1] = new PointF(325, 174);
            pts[2] = new PointF(325, 281);
            pts[3] = new PointF(269, 281);
            c1Word.DrawBeziers(Pens.HotPink, pts);

            _statusBar.Text = "Saving document...";
            string fileName = GetFileName(c1Word, "graphics.rtf");

            c1Word.Save(fileName);
            Process.Start(fileName);
            _statusBar.Text = "Ready.";
        }
Beispiel #13
0
        private void _btComplex_Click(object sender, System.EventArgs e)
        {
            // create document
            C1WordDocument c1Word = new C1WordDocument();

            c1Word.Info.Title = "Complex sample";
            _statusBar.Text   = "Creating document...";

            // add title
            c1Word.AddParagraph(c1Word.Info.Title, new Font("Tahoma", 24, FontStyle.Italic), Color.BlueViolet);

            // add image
            c1Word.AddParagraph("picture:", new Font("Courier New", 9, FontStyle.Regular), Color.Black);
            Bitmap img = new Bitmap(GetManifestResource("picture.jpg"));

            c1Word.AddPicture(img, RtfHorizontalAlignment.Center);

            // add table
            c1Word.LineBreak();
            int      rows  = 7;
            int      cols  = 2;
            RtfTable table = new RtfTable(rows, cols);

            table.BottomBorderStyle = table.LeftBorderStyle = table.RightBorderStyle = table.TopBorderStyle = RtfBorderStyle.Single;
            table.BottomBorderWidth = table.LeftBorderWidth = table.RightBorderWidth = table.TopBorderWidth = 1;
            c1Word.Add(table);
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    var cell = table.Rows[row].Cells[col];
                    cell.BottomBorderStyle = cell.LeftBorderStyle = cell.RightBorderStyle = cell.TopBorderStyle = RtfBorderStyle.Single;
                    cell.BottomBorderWidth = cell.LeftBorderWidth = cell.RightBorderWidth = cell.TopBorderWidth = 1;
                    RtfParagraph paragraph = new RtfParagraph();
                    paragraph.Content.Add(new RtfString(string.Format("table cell {0}:{1}.", row, col)));
                    cell.Content.Add(paragraph);
                }
            }

            // add graphics
            c1Word.LineBreak();
            c1Word.DrawLine(Pens.Green, 200, 90, 400, 90);

            var rc = new RectangleF(150, 170, 90, 40);

            using (Pen pen = new Pen(Brushes.Blue, 5.0f))
            {
                c1Word.DrawRectangle(pen, rc);
            }
            c1Word.FillRectangle(Color.Gold, rc);
            c1Word.ShapeFillOpacity(50);
            c1Word.ShapeRotation(25);

            rc = new RectangleF(300, 120, 80, 80);
            c1Word.DrawEllipse(Pens.Red, rc);
            c1Word.FillEllipse(Color.Pink, rc);
            c1Word.ShapeFillOpacity(70);

            _statusBar.Text = "Saving document...";
            string fileName = GetFileName(c1Word, "complex.rtf");

            c1Word.Save(fileName);
            Process.Start(fileName);
            _statusBar.Text = "Ready.";
        }