Skip to content

Commit

Permalink
fix: #1 missing images in transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
greyovo committed Feb 19, 2021
1 parent 2bf1d54 commit ddbe4fa
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 49 deletions.
Binary file added images/image-20210219105303359.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 141 additions & 23 deletions resource/default_preset.xml
Original file line number Diff line number Diff line change
@@ -1,89 +1,207 @@
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfParagraphStyle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Heading 1</StyleName>
<Alias>一级标题</Alias>
<StyleId>Heading1</StyleId>
<FontName>黑体</FontName>
<FontSizeLb>14</FontSizeLb>
<FontSize>16</FontSize>
<LineSpacingRuleValues>Exact</LineSpacingRuleValues>
<LineSpacing>20</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>1</OutLineLvl>
<Bold>true</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Heading 2</StyleName>
<Alias>二级标题</Alias>
<StyleId>Heading2</StyleId>
<FontName>黑体</FontName>
<FontSizeLb>12</FontSizeLb>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Exact</LineSpacingRuleValues>
<LineSpacing>20</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>2</OutLineLvl>
<Bold>true</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Heading 3</StyleName>
<Alias>三级标题</Alias>
<StyleId>Heading3</StyleId>
<FontName>楷体</FontName>
<FontSizeLb>12</FontSizeLb>
<LineSpacingLb>18</LineSpacingLb>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Exact</LineSpacingRuleValues>
<LineSpacing>20</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>3</OutLineLvl>
<Bold>true</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Heading 4</StyleName>
<Alias>四级标题</Alias>
<StyleId>4</StyleId>
<FontName>楷体</FontName>
<FontSizeLb>12</FontSizeLb>
<LineSpacingLb>20</LineSpacingLb>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Exact</LineSpacingRuleValues>
<LineSpacing>20</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>3</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleName>FirstParagraph</StyleName>
<StyleId>FirstParagraph</StyleId>
<FontName>宋体</FontName>
<FontSizeLb>12</FontSizeLb>
<LineSpacingLb>20</LineSpacingLb>
<ColorHex>000000</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Body Text</StyleName>
<Alias>正文</Alias>
<StyleId>BodyText</StyleId>
<FontName>宋体</FontName>
<FontSizeLb>12</FontSizeLb>
<LineSpacingLb>20</LineSpacingLb>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Auto</LineSpacingRuleValues>
<LineSpacing>1.15</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>true</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Block Text</StyleName>
<Alias>引用块</Alias>
<StyleId>a9</StyleId>
<FontName>宋体</FontName>
<FontSizeLb>12</FontSizeLb>
<LineSpacingLb>20</LineSpacingLb>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Auto</LineSpacingRuleValues>
<LineSpacing>1.15</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>true</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Source Code</StyleName>
<Alias>代码块</Alias>
<StyleId>SourceCode</StyleId>
<FontName>Consolas</FontName>
<FontSizeLb>11</FontSizeLb>
<LineSpacingLb>20</LineSpacingLb>
<FontSize>11</FontSize>
<LineSpacingRuleValues>Auto</LineSpacingRuleValues>
<LineSpacing>1.15</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>First Paragraph</StyleName>
<Alias>段首行</Alias>
<StyleId>FirstParagraph</StyleId>
<FontName>宋体</FontName>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Auto</LineSpacingRuleValues>
<LineSpacing>1.15</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>true</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Normal</StyleName>
<Alias>基准样式</Alias>
<StyleId>Normal</StyleId>
<FontName>宋体</FontName>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Auto</LineSpacingRuleValues>
<LineSpacing>1.15</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Compact</StyleName>
<Alias>无序列表等</Alias>
<StyleId>Compact</StyleId>
<FontName>宋体</FontName>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Auto</LineSpacingRuleValues>
<LineSpacing>1.15</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>character</StyleType>
<StyleName>Hyper link</StyleName>
<Alias>超链接</Alias>
<StyleId>Hyperlink</StyleId>
<FontName>宋体</FontName>
<FontSize>12</FontSize>
<LineSpacingRuleValues>Auto</LineSpacingRuleValues>
<LineSpacing>1.15</LineSpacing>
<ColorHex>007acc</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
<Underline>true</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Left</JustificationValues>
</ParagraphStyle>
<ParagraphStyle>
<StyleType>paragraph</StyleType>
<StyleName>Image Caption</StyleName>
<Alias>图片注解</Alias>
<StyleId>ImageCaption</StyleId>
<FontName>宋体</FontName>
<FontSize>10</FontSize>
<LineSpacingRuleValues>Auto</LineSpacingRuleValues>
<LineSpacing>1.5</LineSpacing>
<ColorHex>000000</ColorHex>
<OutLineLvl>-1</OutLineLvl>
<Bold>false</Bold>
<Italic>false</Italic>
<Underline>false</Underline>
<FirstLineIndentation>false</FirstLineIndentation>
<JustificationValues>Center</JustificationValues>
</ParagraphStyle>
</ArrayOfParagraphStyle>
2 changes: 1 addition & 1 deletion src/Models/ParagraphStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override string ToString()
//string res = FontName + ", "+ FontSizeLb + "pt, #"+ ColorHex;
//res += Bold ? ", 加粗" : "";
//res += Italic ? ", 斜体" : "";
return Alias;
return string.IsNullOrEmpty(Alias) ? StyleName : Alias;
}

}
Expand Down
40 changes: 31 additions & 9 deletions src/Utility/DocumentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace Md2Word.Utility
{
public class DocumentManager
{
static string tempPath = @".\temp\ref.docx";

static string curRunningDirPath = Environment.CurrentDirectory;
static string tempRefDocxPath = curRunningDirPath + @"\temp\ref.docx";
static string pandocPath = @"pandoc";

public static void Transform2Word (string sourcePath, string outputPath)
Expand Down Expand Up @@ -41,9 +43,10 @@ public static void Transform2Word (string sourcePath, string outputPath)
outputPath = sourcePath.Substring(0, sourcePath.LastIndexOf("\\"));
outputPath += fileName + ".docx";

// 步骤1:新建样式模板docx
CreateNewDocument(tempPath);
WordprocessingDocument word = WordprocessingDocument.Open(tempPath, true);
// 步骤1:在源md文件下新建样式模板docx
// tempRefDocxPath = sourcePath.Substring(0, sourcePath.LastIndexOf("\\") + 1) + "pandoc-ref.docx";
CreateNewDocument(tempRefDocxPath);
WordprocessingDocument word = WordprocessingDocument.Open(tempRefDocxPath, true);

foreach (ParagraphStyle ps in StyleManager.StyleList)
{
Expand All @@ -52,23 +55,42 @@ public static void Transform2Word (string sourcePath, string outputPath)

word.Close();

string args = "-o " + outputPath + " " + sourcePath +
" --reference-doc=" + tempPath;
string cd = "cd " + sourcePath.Substring(0, sourcePath.LastIndexOf("\\"));
string args = "pandoc -o " + outputPath + " " + sourcePath +
" --reference-doc=" + tempRefDocxPath;
Console.WriteLine(cd);
Console.WriteLine(args);

// 步骤2:调用pandoc
using (Process pandoc = new Process())
{
try
{
pandoc.StartInfo.FileName = pandocPath;
pandoc.StartInfo.Arguments = args;
// 禁用操作系统外壳程序
pandoc.StartInfo.UseShellExecute = false; //此处必须为false否则引发异常
pandoc.StartInfo.RedirectStandardOutput = true; //输出开启
pandoc.StartInfo.RedirectStandardInput = true; //输入开启
pandoc.StartInfo.CreateNoWindow = true;
pandoc.StartInfo.FileName = "cmd.exe";
pandoc.Start();

pandoc.StandardInput.AutoFlush = true;
pandoc.StandardInput.WriteLine(cd);
pandoc.StandardInput.WriteLine(args);
pandoc.StandardInput.WriteLine("&exit");

// pandoc.WaitForExit();
MessageBox.Show("转换成功!");
}
catch (Exception)
catch (System.ComponentModel.Win32Exception)
{
// TODO 动态设置pandoc的安装目录
MessageBox.Show("未在'" + pandocPath + "'下找到Pandoc!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception)
{
MessageBox.Show("未知错误!");
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/Utility/StyleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ public static Style GenerateStyle(ParagraphStyle p)
return null;
}

StyleName styleName = new StyleName() { Val = p.StyleName };
style.Append(new StyleName() { Val = p.StyleName });
style.Append(new PrimaryStyle());
style.Append(new UIPriority() { Val = 3 });

// ---------------------------------------------
// 字符样式 -------------------------------------
Expand Down Expand Up @@ -187,11 +189,8 @@ public static Style GenerateStyle(ParagraphStyle p)
if (p.Underline)
charProperties.Append(new Underline() { Val = UnderlineValues.Single } );

// 段落样式和字符设置完成 ---------------------------------
style.Append(styleName);
style.Append(new PrimaryStyle());
style.Append(charProperties);
style.Append(new UIPriority() { Val = 3 });

return style;
}

Expand Down
25 changes: 14 additions & 11 deletions src/Window/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ private void LoadComponentData(object sender, EventArgs e)
StyleManager.LoadStylePreset(presetDirPath);
presetListCmbox.Items.AddRange(StyleManager.PresetList.ToArray());

// 加载时预设不为空,默认加载第一个
if (StyleManager.PresetList.Count > 0)
try
{
// 加载时预设不为空,默认加载第一个
if (StyleManager.PresetList.Count > 0)
{
presetListCmbox.SelectedIndex = 0;
var curGroup = (StyleGroup)presetListCmbox.SelectedItem;
styleListBox.Items.AddRange(curGroup.styles);
styleListBox.SelectedIndex = 0;
}
autoLineSpacingTip.SetToolTip(autoLineSpaceRadioBtn, "设置n倍行距,如1倍、1.15倍和2倍行距等");
}
catch (Exception)
{
presetListCmbox.SelectedIndex = 0;
var curGroup = (StyleGroup)presetListCmbox.SelectedItem;
styleListBox.Items.AddRange(curGroup.styles);
styleListBox.SelectedIndex = 0;
MessageBox.Show("预设文件异常!");
}
autoLineSpacingTip.SetToolTip(autoLineSpaceRadioBtn, "设置n倍行距,如1倍、1.15倍和2倍行距等");
}

/// <summary>
Expand Down Expand Up @@ -194,19 +201,16 @@ private void StyleListBox_SelectedIndexChanged(object sender, EventArgs e)
autoLineSpaceRadioBtn.Checked = true;
numericAutoLineSpacing.Value =
Convert.ToDecimal(curStyle.LineSpacing);
numericExactLineSpacing.Value = 0;
break;
case LineSpacingRuleValues.Exact:
exactLineSpaceRadioBtn.Checked = true;
numericExactLineSpacing.Value =
Convert.ToDecimal(curStyle.LineSpacing);
numericAutoLineSpacing.Value = 0;
break;
default:
// 默认使用1.15倍行距
autoLineSpaceRadioBtn.Checked = true;
numericAutoLineSpacing.Value = Convert.ToDecimal(1.15);
numericExactLineSpacing.Value = 0;
break;
}

Expand Down Expand Up @@ -313,7 +317,6 @@ private void NumericOutlevel_ValueChanged(object sender, EventArgs e)
}



private void BoldCheckBox_CheckedChanged(object sender, EventArgs e)
{
curStyle.Bold = boldCheckBox.Checked;
Expand Down

0 comments on commit ddbe4fa

Please sign in to comment.