2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert a Word document to PDF in Azure App Service on Linux. Steps to convert Word document to PDF in Azure App service on Linux:
// [C# Code] private IHostingEnvironment _env; public HomeController(IHostingEnvironment env) { _env = env; } /// <summary> /// Convert Word document to PDF /// </summary> /// <param name="button"></param> /// <returns></returns> public IActionResult WordToPDF(string button) { string fileLoadTime = ""; string domLoadTime = ""; string conversionTime = ""; string saveTime = ""; if (button == null) return View("Index"); if (Request.Form.Files != null) { if (Request.Form.Files.Count == 0) { ViewBag.Message = string.Format("Browse a Word document and then click the button to convert as a PDF document"); return View("Index"); } // Gets the extension from file. string extension = Path.GetExtension(Request.Form.Files[0].FileName).ToLower(); // Compares extension with supported extensions. if (extension == ".doc" || extension == ".docx" || extension == ".dot" || extension == ".dotx" || extension == ".dotm" || extension == ".docm" || extension == ".xml" || extension == ".rtf") { MemoryStream stream = new MemoryStream(); Request.Form.Files[0].CopyTo(stream); try { //Open using Syncfusion WordDocument document = new WordDocument(stream, Syncfusion.DocIO.FormatType.Automatic); stream.Dispose(); stream = null; //Hooks the font substitution event document.FontSettings.SubstituteFont += FontSettings_SubstituteFont; // Creates a new instance of DocIORenderer class. DocIORenderer render = new DocIORenderer(); // Converts Word document into PDF document. PdfDocument pdf = render.ConvertToPDF(document); //Unhooks the font substitution event after converting to PDF document.FontSettings.SubstituteFont -= FontSettings_SubstituteFont; MemoryStream memoryStream = new MemoryStream(); // Save the PDF document pdf.Save(memoryStream); memoryStream.Position = 0; ViewBag.OS = string.Format(System.Environment.OSVersion.ToString()); ViewBag.Load = string.Format("FileLoadTime\t" + fileLoadTime); ViewBag.DomLoad = "DomLoadTime\t" + domLoadTime; ViewBag.Conversion = "ConversionTime\t" + conversionTime; ViewBag.Save = "SaveTime\t" + saveTime; return File(memoryStream, "application/pdf", "WordToPDF.pdf"); } catch (Exception ex) { ViewBag.Message = ex.ToString(); } } else { ViewBag.Message = string.Format("Please choose Word format document to convert to PDF"); } } else { ViewBag.Message = string.Format("Browse a Word document and then click the button to convert as a PDF document"); } return View("Index"); } /// <summary> /// Sets the alternate font when a specified font is not installed in the production environment /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args) { string filePath = string.Empty; FileStream fileStream = null; //Sets the alternate font when a specified font is not installed in the production environment if (args.OriginalFontName == "Calibri") { filePath = _env.WebRootPath + @"/Fonts/calibri.ttf"; fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); args.AlternateFontStream = fileStream; } else if (args.OriginalFontName == "Arial") { filePath = _env.WebRootPath + @"/Fonts/arial.ttf"; fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); args.AlternateFontStream = fileStream; } else { filePath = _env.WebRootPath + @"/Fonts/times.ttf"; fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); args.AlternateFontStream = fileStream; } } Refer to the following steps to publish as Azure App Linux:
A complete work sample for converting an Word document to PDF in Azure App service on Linux can be downloaded from AzureAppServiceOnLinux Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples. Explore more about the rich set of Syncfusion Word Framework features. Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
|
2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.