public static bool GeneratePDF(string htmlContent, string fullPath) { HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); WebKitConverterSettings settings = new WebKitConverterSettings(); settings.Margin.All = 8; //Set WebKit path if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) { settings.WebKitPath = Path.Combine(Configurations.Environment.ContentRootPath, "lib", "QtBinariesDotNetCore"); } else { settings.WebKitPath = Path.Combine(Configurations.Environment.ContentRootPath, "lib", "QtBinaries"); } //Assign WebKit settings to HTML converter htmlConverter.ConverterSettings = settings; //Convert URL to PDF Syncfusion.Pdf.PdfDocument document = htmlConverter.Convert(htmlContent, ""); //Save and close the PDF document new FileInfo(fullPath).Directory.Create(); using (var streamWriter = File.Create(fullPath)) { document.Save(streamWriter); } document.Close(true); return(true); }
public async System.Threading.Tasks.Task <ActionResult> Index(string button) { if (button == null) { return(View()); } ViewBag.Message = string.Empty; if (Request.Form.Files != null) { string extension = Path.GetExtension(Request.Form.Files[0].FileName).ToLower(); if (extension == ".doc" || extension == ".docx" || extension == ".docm" || extension == ".xml") { MemoryStream stream = new MemoryStream(); Request.Form.Files[0].CopyTo(stream); try { WordDocument document = new WordDocument(stream, FormatType.Automatic); stream.Dispose(); stream = null; DocIORenderer render = new DocIORenderer(); Syncfusion.Pdf.PdfDocument pdf = render.ConvertToPDF(document); MemoryStream memoryStream = new MemoryStream(); pdf.Save(memoryStream); render.Dispose(); document.Close(); pdf.Close(); memoryStream.Position = 0; return(File(memoryStream, "application/pdf", "WordToPDF.pdf")); } catch (Exception ex) { ViewBag.Message = string.Format("The input document could not be processed completely."); } } else { ViewBag.Message = string.Format("Choose a Word format document to convert to PDF"); } } return(View()); }
public void RunSynfusionBenchmark(string fileToSplit, int splitByPagesNumber, int?pagesCountToProcess = null) { var srcFile = Path.Combine(_rootFolder, fileToSplit); var file = new FileInfo(srcFile); var name = file.Name.Substring(0, file.Name.LastIndexOf(".", StringComparison.Ordinal)); var stopwatch = new Stopwatch(); stopwatch.Start(); var srcStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read); var docToSplit = new PdfLoadedDocument(srcStream) { EnableMemoryOptimization = true }; var pagesCount = pagesCountToProcess ?? docToSplit.PageCount; var iteration = 1; var currentPageIndex = 0; //Split the pages into separate documents. while (currentPageIndex < pagesCount) { var newDocument = new Syncfusion.Pdf.PdfDocument { EnableMemoryOptimization = true }; int endPage = currentPageIndex + splitByPagesNumber < pagesCount ? currentPageIndex + splitByPagesNumber - 1 : pagesCount - 1; newDocument.ImportPageRange(docToSplit, currentPageIndex, endPage, false); using (var fileStream = File.Create(Path.Combine(_resultsSyncfusionFolder, $"splitted_{name}_{iteration}.pdf"))) { newDocument.Save(fileStream); newDocument.Close(); } currentPageIndex += splitByPagesNumber; iteration++; } srcStream.Dispose(); docToSplit.Close(); }
private void ConvertHtmlToPdf(string directoryPath) { try { var mainUrl = Path.Combine(_env.ContentRootPath, "wwwroot"); HtmlToPdfConverter converter = new HtmlToPdfConverter(); WebKitConverterSettings settings = new WebKitConverterSettings(); settings.WebKitPath = Path.Combine(mainUrl, "QtBinariesWindows"); converter.ConverterSettings = settings; //test settings.TempPath = Path.Combine(mainUrl, "QtBinariesWindows", "temp"); // string[] filePaths = Directory.GetFiles(directoryPath); foreach (string file in filePaths) { FileInfo info = new FileInfo(file); if (File.Exists(info.FullName)) { if (info.Extension.Equals(".html")) { Syncfusion.Pdf.PdfDocument document = converter.Convert(info.FullName); MemoryStream ms = new MemoryStream(); document.Save(ms); document.Close(true); ms.Position = 0; FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); var fileName = info.Name.Split(".")[0] + ".pdf"; var destSource = Path.Combine(directoryPath, fileName); var fileStream = new FileStream(destSource, FileMode.Create, FileAccess.Write); fileStreamResult.FileStream.CopyTo(fileStream); fileStream.Close(); } } } } catch (Exception ex) { DeleteDirectory(directoryPath); throw ex; } }
byte[] HtmlByteToPdfByte(byte[] htmlByte) { string html = Encoding.ASCII.GetString(htmlByte); //SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf(); //SelectPdf.PdfDocument doc = converter.ConvertHtmlString(html); Syncfusion.HtmlConverter.HtmlToPdfConverter con = new Syncfusion.HtmlConverter.HtmlToPdfConverter(); Syncfusion.Pdf.PdfDocument pdfData = con.Convert(html); MemoryStream stream = new MemoryStream(); pdfData.Save(stream); pdfData.Close(true); // Converts the PdfDocument object to byte form. byte[] docBytes = stream.ToArray(); return(docBytes); }
/// <summary> /// 获得Word文件的预览图 /// </summary> /// <param name="path">文件路径</param> /// <returns>Word的预览</returns> public static async Task <IRandomAccessStream> GetWordPreviewAsync(string path) { StorageFile file = await StorageFile.GetFileFromPathAsync(path); var res = await file.OpenAsync(FileAccessMode.Read); WordDocument wordDocument = new WordDocument(); wordDocument.Open(res.AsStream(), Syncfusion.DocIO.FormatType.Automatic); DocIORenderer render = new DocIORenderer(); Syncfusion.Pdf.PdfDocument pdfDocument = render.ConvertToPDF(wordDocument); render.Dispose(); wordDocument.Dispose(); InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream(); pdfDocument.Save(ms.AsStream()); currentFileType = PreviewModel.FileType.Office; return(await GetPDFPreviewFromStreamAsync(ms)); }
private async void SavePDF_Click() { try { using (Syncfusion.Pdf.PdfDocument PDFdocument = new Syncfusion.Pdf.PdfDocument()) { Syncfusion.Pdf.PdfPage page = PDFdocument.Pages.Add(); PdfGraphics graphics = page.Graphics; Syncfusion.Pdf.Graphics.PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); graphics.DrawString(textFile, font, PdfBrushes.Black, new System.Drawing.PointF(0, 0)); MemoryStream ms = new MemoryStream(); await PDFdocument.SaveAsync(ms).ConfigureAwait(true); //SaveFilePdf.Save(ms, "New PDF file.pdf"); SaveFilePdf.Save(ms, nameFile.Text, pathSaveFile.Text); PDFdocument.Save(ms); PDFdocument.Close(true); ms.Dispose(); } } catch (ArgumentNullException) { } }
public ActionResult TableofContents(string Group1, string UpdateTOC) { if (Group1 == null) { return(View()); } WordDocument doc = new WordDocument(); doc.EnsureMinimal(); WParagraph para = doc.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = doc.LastSection.AddParagraph() as WParagraph; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); if (UpdateTOC != "Update") { para.AppendText("Select TOC and press F9 to update the Table of Contents").CharacterFormat.HighlightColor = Syncfusion.Drawing.Color.Yellow; } para = doc.LastSection.AddParagraph() as WParagraph; //Insert TOC TableOfContent toc = para.AppendTOC(1, 3); para.ApplyStyle(BuiltinStyle.Heading4); //Apply built-in paragraph formatting WSection section = doc.LastSection; #region Default Styles WParagraph newPara = section.AddParagraph() as WParagraph; newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); WTextRange text = newPara.AppendText("Document with Default styles") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading1); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); section.AddParagraph(); section = doc.AddSection() as WSection; section.BreakCode = SectionBreakCode.NewPage; newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); #endregion toc.IncludePageNumbers = true; toc.RightAlignPageNumbers = true; toc.UseHyperlinks = true; toc.LowerHeadingLevel = 1; toc.UpperHeadingLevel = 3; toc.UseOutlineLevels = true; //Updates the table of contents. if (UpdateTOC == "Update") { doc.UpdateTableOfContents(); } #region Document SaveOption string filename = ""; string contenttype = ""; MemoryStream ms = new MemoryStream(); //Save as .docx format if (Group1 == "WordDocx") { filename = "Table of Contents.docx"; contenttype = "application/vnd.ms-word.document.12"; doc.Save(ms, FormatType.Docx); } // Save as .doc format else if (Group1 == "WordDoc") { filename = "Table of Contents.doc"; contenttype = "application/msword"; doc.Save(ms, FormatType.Doc); } //Save as .xml format else if (Group1 == "WordML") { filename = "Table of Contents.xml"; contenttype = "application/msword"; doc.Save(ms, FormatType.WordML); } //Save as .pdf format else if (Group1 == "Pdf") { filename = "Table of Contents.pdf"; contenttype = "application/pdf"; Syncfusion.DocIORenderer.DocIORenderer renderer = new Syncfusion.DocIORenderer.DocIORenderer(); Syncfusion.Pdf.PdfDocument pdfDoc = renderer.ConvertToPDF(doc); pdfDoc.Save(ms); pdfDoc.Close(); } #endregion Document SaveOption doc.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }