public void AppendChapter(Bible bible, int chapter, int paraNum, IEnumerable <string> chapterContent, CancellationToken token) { var isFirst = true; foreach (var paragraph in chapterContent) { token.ThrowIfCancellationRequested(); var slide = templateSlide.Duplicate(); slide.MoveTo(workingPPT.Slides.Count); foreach (var textShape in slide.Shapes.Cast <PowerPoint.Shape>() .Where(i => i.HasTextFrame == MsoTriState.msoTrue) .Select(i => i.TextFrame.TextRange)) { var text = textShape.Text; text = AddSuffix(text, "CHAP", everyChapter || isFirst, chapter + ""); text = AddSuffix(text, "STITLE", everyShortTitle || isFirst, bible.shortTitle); text = AddSuffix(text, "TITLE", everyLongTitle || isFirst, bible.longTitle); text = text.Replace("[PARA]", paraNum + ""); text = text.Replace("[BODY]", paragraph); textShape.Text = text; } isFirst = false; paraNum++; } }
private void btnMake_Click(object sender, EventArgs e) { if (btnMake.Text == @"PPT 만드는 중...") { CTS.Cancel(); return; } string destination; using (var fd = new FolderBrowserDialog()) { fd.Description = "PPT를 저장할 폴더를 선택하세요."; if (chkFragment.Checked && fd.ShowDialog() != DialogResult.OK) { return; } destination = fd.SelectedPath; } btnMake.Text = @"PPT 만드는 중..."; AlterControl(false, btnMake); CTS = new CancellationTokenSource(); Task.Factory.StartNew(() => { builder.ApplyConfig(cmbChapNum.SelectedIndex == 0, cmbLongTitle.SelectedIndex == 0, cmbShortTitle.SelectedIndex == 0); builder.BeginBuild(); foreach (var data in txtKeyword.Text.Split() .Select(keyword => Regex.Match(keyword, @"(?<bible>[가-힣]+)(?<range>(?<chapFrom>\d+)(?::(?<paraFrom>\d+))?(?:-(?:(?<chapTo>\d+):)?(?<paraTo>\d+))?)?")) .Where(match => match.Success)) { Bible bible = null; Invoke(new MethodInvoker(() => bible = (Bible)lstBible.FindItemWithText(data.Groups["bible"].Value).Tag)); var chapFrom = 1; var paraFrom = 1; var chapTo = bible.chapterLength; var paraTo = -1; if (!string.IsNullOrEmpty(data.Groups["range"].Value)) { chapFrom = Convert.ToInt32(data.Groups["chapFrom"].Value); if (string.IsNullOrEmpty(data.Groups["paraFrom"].Value)) { paraFrom = 1; if (string.IsNullOrEmpty(data.Groups["chapTo"].Value)) { chapTo = string.IsNullOrEmpty(data.Groups["paraTo"].Value) ? chapFrom : Convert.ToInt32(data.Groups["paraTo"].Value); paraTo = -1; } else { chapTo = Convert.ToInt32(data.Groups["chapTo"].Value); paraTo = Convert.ToInt32(data.Groups["paraTo"].Value); } } else { paraFrom = Math.Max(1, Convert.ToInt32(data.Groups["paraFrom"].Value)); chapTo = string.IsNullOrEmpty(data.Groups["chapTo"].Value) ? chapFrom : Convert.ToInt32(data.Groups["chapTo"].Value); paraTo = string.IsNullOrEmpty(data.Groups["paraTo"].Value) ? paraFrom : Convert.ToInt32(data.Groups["paraTo"].Value); } chapTo = Math.Min(chapTo, bible.chapterLength); } for (var chapter = chapFrom; chapter <= chapTo; chapter++) { Invoke(new MethodInvoker(() => Text = "성경2PPT - " + bible.longTitle + " " + chapter + "장")); if (!string.IsNullOrEmpty(destination)) { builder.CommitBuild(); var parent = Path.Combine(destination, bible.longTitle); if (!Directory.Exists(parent)) { Directory.CreateDirectory(parent); } builder.BeginBuild(Path.Combine(parent, chapter.ToString("000\\.pptx"))); } var content = dao.getBibleChapterAsync(bible.shortTitle, chapter, radRevision.Checked).Result; if (chapter == chapTo && paraTo != -1) { content = content.Take(Math.Min(content.Count(), paraTo)); } if (chapter == chapFrom) { content = content.Skip(paraFrom - 1); } builder.AppendChapter(bible, chapter, paraFrom, content, CTS.Token); paraFrom = 1; } } }, CTS.Token) .ContinueWith(t => Invoke(new MethodInvoker(() => { AlterControl(true, btnMake); btnMake.Text = "PPT 만들기"; Text = "성경2PPT"; builder.CommitBuild(); }))) .ContinueWith(t => { builder.OpenLastBuild(); }, TaskContinuationOptions.NotOnFaulted); }