/// <summary> /// The SplitPdfFile /// </summary> /// <param name="v1">The <see cref="string"/></param> /// <param name="v2">The <see cref="string"/></param> /// <returns>The <see cref="Task"/></returns> public async Task SplitFile(string filePath) { await Task.Factory.StartNew(() => { var outputDirectoryPath = DirectoryDataFactory.CreateChildDirectory(filePath, $"pages{ DateTime.Now.ToString("ddHHmm")}"); PdfReader reader = new PdfReader(filePath);; Document sourceDocument = null; PdfCopy pdfCopyProvider = null; PdfImportedPage importedPage = null; var fileName = Path.GetFileName(filePath); this.args.totalFiles = reader.NumberOfPages; for (int i = 1; i < reader.NumberOfPages; i++) { var outPath = $"{outputDirectoryPath}\\{fileName.Replace(".pdf", "")}_{i}_page.pdf"; sourceDocument = new Document(reader.GetPageSizeWithRotation(i)); pdfCopyProvider = new PdfCopy(sourceDocument, new FileStream(outPath, FileMode.Create)); sourceDocument.Open(); importedPage = pdfCopyProvider.GetImportedPage(reader, i); pdfCopyProvider.AddPage(importedPage); sourceDocument.Close(); this.args.currentFile++; this.eventHandler?.Invoke(this.args); } reader.Close(); }); }
/// <summary> /// The CreatePdfFromHtmlFileAsync /// </summary> /// <param name="fileList">The <see cref="string[]"/></param> /// <returns>The <see cref="Task"/></returns> public async Task CreatePdfFromHtmlFileAsync(string[] fileList, string outputPath = null) { try { await Task.Factory.StartNew(() => { string directoryPath; if (outputPath != null) { directoryPath = outputPath; } else { directoryPath = DirectoryDataFactory.CreateChildDirectory(fileList[0], $"pdfy{ DateTime.Now.ToString("ddHHmm")}"); } this.args.totalFiles = fileList.Length; Parallel.For(0, this.args.totalFiles, new ParallelOptions() { MaxDegreeOfParallelism = 5 }, (i) => { this.args.currentFileName = fileList[i]; byte[] pdf; var html = File.ReadAllText(fileList[i]); using (var memoryStream = new MemoryStream()) { var document = new Document(PageSize.A4, 50, 50, 60, 60); var writer = PdfWriter.GetInstance(document, memoryStream); StringReader sr = new StringReader(html); document.Open(); using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html))) { XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr); } document.Close(); pdf = memoryStream.ToArray(); var pdfFileName = Path.GetFileName(fileList[i]).Replace(".html", ".pdf"); File.WriteAllBytes($"{directoryPath}\\{pdfFileName}", pdf); this.args.currentFile++; this.eventHandler?.Invoke(this.args); } }); }); } catch (Exception) { throw new ProcessFileException(this.args); } }
/// <summary> /// The DeleteFilesAsync /// </summary> /// <returns>The <see cref="Task"/></returns> public async Task DeleteFilesAsync(KindOfFileEnum fileType = KindOfFileEnum.Any) { await Task.Factory.StartNew(() => { var fileList = DirectoryDataFactory.GetFilesFromPattern(this.filePath, fileType); Parallel.For(0, fileList.Length, (i) => { File.Delete(fileList[i]); }); }); }
public void OpenFileDialogSplitPdf(TextBox sender) { this.fileList = new string[] { DirectoryDataFactory.GetFilePathFromDialog(KindOfFileEnum.Pdf) }; if (string.IsNullOrEmpty(this.fileList[0])) { MessageBox.Show("Error: please input correct .pdf file"); return; } this.txtBoxEventHandler?.Invoke(sender, Path.GetDirectoryName(this.fileList[0])); }
internal void OpenFileDialogXslt(TextBox sender) { this.fileXslt = DirectoryDataFactory.GetFilePathFromDialog(KindOfFileEnum.Xslt); if (string.IsNullOrEmpty(fileXslt)) { return; } else { MessageBox.Show($"Files found: {fileXslt}"); } this.txtBoxEventHandler(sender, Path.GetDirectoryName(fileXslt)); }
internal async Task GenerateXmlToPdfFile() { var timer = StatisticsTools.TimerFactory(); var directoryXMLPath = DirectoryDataFactory.CreateChildDirectory(this.fileList[0], $"xml{ DateTime.Now.ToString("ddHHmm")}"); var directoryPdfPath = DirectoryDataFactory.CreateChildDirectory(this.fileList[0], $"htmle{ DateTime.Now.ToString("ddHHmm")}"); await xml.TransformXML(this.fileList, this.fileXslt, directoryXMLPath); await pdf.CreatePdfFromHtmlFileAsync(DirectoryDataFactory.GetDirectoryFilesFromPath(directoryXMLPath), directoryPdfPath); DirectoryDataFactory.DeleteDirectory(directoryXMLPath); StatisticsTools.ShowTaskCompleted(timer); }
internal void OpenFileDialogConvertHtml(TextBox sender) { this.fileList = DirectoryDataFactory.GetDirectoryFilesFromBrowserDialog("*html"); if (fileList != null) { var count = fileList.Length; MessageBox.Show($"Files found: {count.ToString()} files"); if (count == 0) { return; } } else { return; } this.txtBoxEventHandler?.Invoke(sender, Path.GetDirectoryName(fileList[0])); }