Skip to content

Commit b1f6ea8

Browse files
committed
Improve sample app
1 parent 4c8b4e7 commit b1f6ea8

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

samples/WpfApp1/MainWindow.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
<Button
4545
Click="DocxToMarkdown_Click"
4646
Content="Docx to Markdown" />
47+
<Button
48+
Click="DocxToMarkdownAppend_Click"
49+
Content="Docx to Markdown (Append)" />
4750
<Button
4851
Click="DocxToTxt_Click"
4952
Content="Docx to Txt" />

samples/WpfApp1/MainWindow.xaml.cs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private void DocxToRtf_Click(object sender, RoutedEventArgs e)
117117
{
118118
var ofd = new OpenFileDialog()
119119
{
120-
Filter = "Word OpenXML document|*.docx;*.dotx",
120+
Filter = "Word OpenXML document|*.docx;*.dotx;*.docm;*.dotm",
121121
Multiselect = false,
122122
};
123123
if (ofd.ShowDialog(this) == true)
@@ -133,8 +133,7 @@ private void DocxToRtf_Click(object sender, RoutedEventArgs e)
133133
{
134134
var converter = new DocxToRtfConverter()
135135
{
136-
ImageConverter = new ImageSharpConverter(),
137-
// Converts TIFF, GIF and other formats which are not supported in RTF.
136+
ImageConverter = new ImageSharpConverter(), // Converts TIFF, GIF and other formats which are not supported in RTF.
138137
OriginalFolderPath = Path.GetDirectoryName(ofd.FileName), // converts sub-documents (if any)
139138
OutputFolderPath = Path.GetDirectoryName(sfd.FileName)
140139
};
@@ -152,7 +151,7 @@ private void DocxToHtml_Click(object sender, RoutedEventArgs e)
152151
{
153152
var ofd = new OpenFileDialog()
154153
{
155-
Filter = "Word OpenXML document|*.docx;*.dotx",
154+
Filter = "Word OpenXML document|*.docx;*.dotx;*.docm;*.dotm",
156155
Multiselect = false,
157156
};
158157
if (ofd.ShowDialog(this) == true)
@@ -171,7 +170,7 @@ private void DocxToHtml_Click(object sender, RoutedEventArgs e)
171170
ExportHeaderFooter = true,
172171
ExportFootnotesEndnotes = true,
173172
ImageConverter = new SystemDrawingConverter(), // Converts TIFF, WMF and EMF
174-
// (ImageSharp does not support WMF / EMF yet)
173+
// (ImageSharp does not support WMF / EMF yet)
175174
OriginalFolderPath = Path.GetDirectoryName(ofd.FileName) // converts sub-documents (if any)
176175
};
177176
converter.Convert(ofd.FileName, sfd.FileName);
@@ -188,14 +187,14 @@ private void DocxToMarkdown_Click(object sender, RoutedEventArgs e)
188187
{
189188
var ofd = new OpenFileDialog()
190189
{
191-
Filter = "Word OpenXML document|*.docx;*.dotx",
190+
Filter = "Word OpenXML document|*.docx;*.dotx;*.docm;*.dotm",
192191
Multiselect = false,
193192
};
194193
if (ofd.ShowDialog(this) == true)
195194
{
196195
var sfd = new SaveFileDialog()
197196
{
198-
Filter = "Markdown|*.md;*.markdown;*.mkd;*.mkdn;*.mkdwn; *.mdwn;*.mdown;*.markdn;*.mdtxt;*.mdtext",
197+
Filter = "Markdown|*.md;*.markdown;*.mkd;*.mkdn;*.mkdwn;*.mdwn;*.mdown;*.markdn;*.mdtxt;*.mdtext",
199198
FileName = Path.GetFileNameWithoutExtension(ofd.FileName) + ".md"
200199
};
201200
if (sfd.ShowDialog(this) == true)
@@ -206,8 +205,6 @@ private void DocxToMarkdown_Click(object sender, RoutedEventArgs e)
206205
{
207206
ImagesOutputFolder = Path.GetDirectoryName(sfd.FileName),
208207
ImagesBaseUriOverride = "",
209-
//ImagesBaseUriOverride = "..",
210-
//ImagesBaseUriOverride = "images/",
211208
ImageConverter = new SystemDrawingConverter(), // Converts TIFF, WMF and EMF
212209
// (ImageSharp does not support WMF / EMF yet)
213210
OriginalFolderPath = Path.GetDirectoryName(ofd.FileName) // converts sub-documents (if any)
@@ -222,6 +219,42 @@ private void DocxToMarkdown_Click(object sender, RoutedEventArgs e)
222219
}
223220
}
224221

222+
private void DocxToMarkdownAppend_Click(object sender, RoutedEventArgs e)
223+
{
224+
var ofd = new OpenFileDialog()
225+
{
226+
Filter = "Word OpenXML document|*.docx;*.dotx;*.docm;*.dotm",
227+
Multiselect = false,
228+
};
229+
if (ofd.ShowDialog(this) == true)
230+
{
231+
var sfd = new SaveFileDialog()
232+
{
233+
Filter = "Markdown|*.md;*.markdown;*.mkd;*.mkdn;*.mkdwn; *.mdwn;*.mdown;*.markdn;*.mdtxt;*.mdtext",
234+
FileName = Path.GetFileNameWithoutExtension(ofd.FileName) + ".md"
235+
};
236+
if (sfd.ShowDialog(this) == true)
237+
{
238+
try
239+
{
240+
var converter = new DocxToMarkdownConverter()
241+
{
242+
ImagesOutputFolder = Path.GetDirectoryName(sfd.FileName),
243+
ImagesBaseUriOverride = "",
244+
ImageConverter = new SystemDrawingConverter(), // Converts TIFF, WMF and EMF
245+
// (ImageSharp does not support WMF / EMF yet)
246+
OriginalFolderPath = Path.GetDirectoryName(ofd.FileName) // converts sub-documents (if any)
247+
};
248+
converter.Append(ofd.FileName, sfd.FileName);
249+
}
250+
catch (Exception ex)
251+
{
252+
MessageBox.Show(ex.Message);
253+
}
254+
}
255+
}
256+
}
257+
225258
private void DocxToTxt_Click(object sender, RoutedEventArgs e)
226259
{
227260
var ofd = new OpenFileDialog()
@@ -265,7 +298,7 @@ private void MarkdownToDocx_Click(object sender, RoutedEventArgs e)
265298
{
266299
var sfd = new SaveFileDialog()
267300
{
268-
Filter = "Word OpenXML document|*.docx",
301+
Filter = "Word OpenXML document|*.docx;*.dotx;*.docm;*.dotm",
269302
FileName = Path.GetFileNameWithoutExtension(ofd.FileName) + ".docx"
270303
};
271304
if (sfd.ShowDialog(this) == true)
@@ -279,7 +312,7 @@ private void MarkdownToDocx_Click(object sender, RoutedEventArgs e)
279312
ImageConverter = new ImageSharpConverter() // Convert WEBP images which are not supported in DOCX
280313
// (possibly AVIF and JXL too in a future release)
281314
};
282-
converter.ToDocx(markdown, sfd.FileName);
315+
converter.ToDocx(markdown, sfd.FileName, FileFormatHelpers.ExtensionToDocumentType(Path.GetExtension(sfd.FileName)));
283316
}
284317
catch (Exception ex)
285318
{
@@ -294,14 +327,15 @@ private void MarkdownToDocxAppend_Click(object sender, RoutedEventArgs e)
294327
var ofd = new OpenFileDialog()
295328
{
296329
Filter = "Markdown|*.md;*.markdown;*.mkd;*.mkdn;*.mkdwn; *.mdwn;*.mdown;*.markdn;*.mdtxt;*.mdtext",
330+
Title = "Choose the Markdown document to convert",
297331
Multiselect = false,
298332
};
299333
if (ofd.ShowDialog(this) == true)
300334
{
301335
var ofd2 = new OpenFileDialog()
302336
{
303-
Filter = "Word OpenXML document|*.docx",
304-
FileName = Path.GetFileNameWithoutExtension(ofd.FileName) + ".docx"
337+
Filter = "Word OpenXML document|*.docx;*.dotx;*.docm;*.dotm",
338+
Title = "Choose the target Word document"
305339
};
306340
if (ofd2.ShowDialog(this) == true)
307341
{

src/DocSharp.Docx/DocxToStringWriterBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public virtual void Append(WordprocessingDocument inputDocument, string outputFi
5151
encoding ??= Encodings.UTF8NoBOM;
5252
using (var sw = new StreamWriter(outputFilePath, append: true, encoding))
5353
{
54+
sw.WriteLine();
5455
sw.WriteLine();
5556
Convert(inputDocument, sw);
5657
}

src/DocSharp.Docx/Formats/FileFormatHelpers.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using DocumentFormat.OpenXml;
67

78
namespace DocSharp.Docx;
89

@@ -68,4 +69,20 @@ public static SaveFormat ExtensionToSaveFormat(string ext)
6869
throw new NotImplementedException("Unsupported save format.");
6970
}
7071
}
72+
73+
public static WordprocessingDocumentType ExtensionToDocumentType(string ext)
74+
{
75+
switch (ext.ToUpperInvariant())
76+
{
77+
case ".DOTX":
78+
return WordprocessingDocumentType.Template;
79+
case ".DOCM":
80+
return WordprocessingDocumentType.MacroEnabledDocument;
81+
case ".DOTM":
82+
return WordprocessingDocumentType.MacroEnabledTemplate;
83+
case ".DOCX":
84+
default:
85+
return WordprocessingDocumentType.Document;
86+
}
87+
}
7188
}

src/DocSharp.Markdown/MarkdownConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MarkdownConverter
4949
/// </summary>
5050
/// <param name="markdown">The input markdown source.</param>
5151
/// <param name="outputStream">The output stream.</param>
52-
/// <param name="openXmlDocumentType">The Open XML document type (Document by default).</param>
52+
/// <param name="openXmlDocumentType">The Open XML document type (Document by default). Has no effect when appending to an existing document.</param>
5353
/// <param name="append">If true, adds the converted Markdown to the DOCX content rather than replacing it (false by default).
5454
/// The default styles are automatically inserted if the document does not override them.</param>
5555
public void ToDocx(MarkdownSource markdown, Stream outputStream, WordprocessingDocumentType openXmlDocumentType = WordprocessingDocumentType.Document, bool append = false)
@@ -65,7 +65,7 @@ public void ToDocx(MarkdownSource markdown, Stream outputStream, WordprocessingD
6565
/// </summary>
6666
/// <param name="markdown">The input markdown source.</param>
6767
/// <param name="outputFilePath">The output file path.</param>
68-
/// <param name="openXmlDocumentType">The Open XML document type (Document by default).</param>
68+
/// <param name="openXmlDocumentType">The Open XML document type (Document by default). Has no effect when appending to an existing document.</param>
6969
/// <param name="append">If true and the output file exists, adds the converted Markdown to the DOCX content rather than replacing it (false by default).
7070
/// The default styles are automatically inserted if the document does not override them.</param>
7171
public void ToDocx(MarkdownSource markdown, string outputFilePath, WordprocessingDocumentType openXmlDocumentType = WordprocessingDocumentType.Document, bool append = false)
@@ -96,7 +96,7 @@ public byte[] ToDocxBytes(MarkdownSource markdown, WordprocessingDocumentType op
9696
/// </summary>
9797
/// <param name="markdown">The markdown source.</param>
9898
/// <param name="outputStream">The output stream.</param>
99-
/// <param name="openXmlDocumentType">The Open XML document type (Document by default).</param>
99+
/// <param name="openXmlDocumentType">The Open XML document type (Document by default). Has no effect when appending to an existing document.</param>
100100
/// <param name="append">If true, adds the converted Markdown to the DOCX content rather than replacing it (false by default).
101101
/// The default styles are automatically inserted if the document does not override them.</param>
102102
/// <returns>A <see cref="WordprocessingDocument"/> object.</returns>
@@ -130,7 +130,7 @@ public WordprocessingDocument ToWordprocessingDocument(MarkdownSource markdown,
130130
/// </summary>
131131
/// <param name="markdown">The markdown source.</param>
132132
/// <param name="outputFilePath">The output file path.</param>
133-
/// <param name="openXmlDocumentType">The Open XML document type (Document by default).</param>
133+
/// <param name="openXmlDocumentType">The Open XML document type (Document by default). Has no effect when appending to an existing document.</param>
134134
/// <param name="append">If true and the output file exists, adds the converted Markdown to the DOCX content rather than replacing it (false by default).
135135
/// The default styles are automatically inserted if the document does not override them.</param>
136136
/// <returns>A <see cref="WordprocessingDocument"/> object.</returns>

0 commit comments

Comments
 (0)