/
Form1.cs
109 lines (84 loc) · 3.41 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Aspose.Words;
using Aspose.Words.Replacing;
namespace asposeWords
{
public partial class Form1 : Form
{
public enum StyleNames { None, Heading1, Heading2, Quote }
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ProcessMarkdown();
}
private void ProcessMarkdown()
{
var docPath = "C:\\temp\\aspose\\";
var fileToLoad = "snippet.md";
var doc = new Document(Path.Combine(docPath, fileToLoad));
var options = new FindReplaceOptions();
//options.ApplyFont.Bold = false;
options.ApplyFont.Italic = true;
_ = doc.Range.Replace("demonstration", "code demonstration", options);
var paragraph0 = CreateParagraph(doc, StyleNames.Heading1);
_ = paragraph0.AppendChild(CreateRun(doc, "This is a Heading 1"));
var paragraph1 = CreateParagraph(doc);
_ = paragraph1.AppendChild(CreateRun(doc, "This is some bold text", true));
var paragraph2 = CreateParagraph(doc, StyleNames.Heading2);
_ = paragraph2.AppendChild(CreateRun(doc, "This is a Heading 2"));
var paragraph3 = CreateParagraph(doc);
_ = paragraph3.AppendChild(CreateRun(doc, "This is some Italic text", false, true));
var paragraph4 = CreateParagraph(doc, StyleNames.Quote);
_ = paragraph4.AppendChild(CreateRun(doc, "This is a quote or something profound"));
_ = doc.Save(Path.Combine(docPath, "snippetModified.md"), SaveFormat.Markdown);
//doc.Protect(ProtectionType.ReadOnly, "password");
//_ = doc.Save(Path.Combine(docPath, "snippetModified.docx"), SaveFormat.Docx);
//doc.Print("Foxit Reader PDF Printer");
}
private Paragraph CreateParagraph(Document doc, StyleNames styleName = StyleNames.None)
{
var section = new Section(doc);
_ = doc.AppendChild(section);
var body = new Body(doc);
_ = section.AppendChild(body);
var paragraph = new Paragraph(doc);
_ = body.AppendChild(paragraph);
switch (styleName)
{
case StyleNames.Heading1:
_ = paragraph.ParagraphFormat.StyleName = "Heading 1";
break;
case StyleNames.Quote:
_ = paragraph.ParagraphFormat.StyleName = "Quote";
break;
case StyleNames.Heading2:
_ = paragraph.ParagraphFormat.StyleName = "Heading 2";
break;
default:
break;
}
return paragraph;
}
private Run CreateRun(Document doc, string text, bool isBold = false, bool isItalic = false)
{
var textRun = new Run(doc);
textRun.Text = text;
textRun.Font.Bold = isBold;
textRun.Font.Italic = isItalic;
return textRun;
}
}
}