private void DownloadBook() { if (File.Exists(_bookInfo.FilePath)) { File.Delete(_bookInfo.FilePath); } FileHelper.CreateFile(_bookInfo.FilePath); using (var fs = new StreamWriter(_bookInfo.FilePath, false, Encoding.UTF8)) { double count = _book.CatalogInfos.Count; var i = 1; foreach (var info in _book.CatalogInfos) { var chapter = new WebBook(info.Url); fs.WriteLine(info.Name); fs.WriteLine(chapter.Content); Dispatcher.Invoke(() => { Downloadbar.Value = i / count * 100; }); i++; } fs.Close(); } Dispatcher.Invoke(() => { if (BookFiles.All(o => !o.FilePath.Equals(_bookInfo.FilePath))) { BookFiles.Add(_bookInfo); } BtnDownload.IsEnabled = true; TbState.Text = "Complated"; }); }
public void SaveAs(WebBook book, string path) { var fileName = string.Format(FileTemplate, $"{path}\\", book.Name); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (new DirectoryInfo(path).GetFiles().Any(f => f.Name.Contains(book.Name))) { return; } DocX doc = DocX.Create(fileName); doc.DifferentFirstPage = true; doc.AddFooters(); InsertImage(doc, _download.DownloadFile(book.ImageUrl, book.Name)); foreach (var page in book.Pages) { foreach (var data in page.WebBookTexts) { switch (data) { case WebBookHeader _: InsertHeader(doc, data.Text); break; case WebBookParagraph _: InsertParagraph(doc, data.Text); break; } } } var even = doc.Footers.Even.InsertParagraph("Page №"); even.Alignment = Alignment.center; even.AppendPageNumber(PageNumberFormat.normal); var odd = doc.Footers.Odd.InsertParagraph("Page №"); odd.Alignment = Alignment.center; odd.AppendPageNumber(PageNumberFormat.normal); doc.Save(); }
private void BtnDownloadOnClick(object sender, RoutedEventArgs e) { var url = TbUrl.Text.Trim(); if (string.IsNullOrEmpty(url)) { TbState.Text = "Url Error"; return; } if (!url.StartsWith("http")) { url = "http://" + url; } _book = new WebBook(url); if (_book.CatalogInfos == null || _book.CatalogInfos.Count == 0) { TbState.Text = "Failed"; return; } var saveFileDialog = new SaveFileDialog { Filter = "(*.txt)|*.txt", FileName = string.IsNullOrEmpty(_book.Title) ? Guid.NewGuid().GetHashCode().ToString() : _book.Title }; if (saveFileDialog.ShowDialog(this) != true) { return; } var name = FileHelper.GetFileNameNoneExt(saveFileDialog.FileName); _bookInfo = new BookInfo { BookName = name, FilePath = saveFileDialog.FileName }; BtnDownload.IsEnabled = false; new Thread(DownloadBook).Start(); }
public void Save(WebBook book) => SaveAs(book, DefaultDownloadPath);