|
| 1 | +using WordToPdf.Models; |
| 2 | +using Microsoft.AspNetCore.Hosting; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Syncfusion.DocIO.DLS; |
| 5 | +using Syncfusion.DocIORenderer; |
| 6 | +using Syncfusion.Pdf; |
| 7 | +using System.IO; |
| 8 | +using System.Reflection.Metadata; |
| 9 | +using Microsoft.AspNetCore.Authorization; |
| 10 | +using Microsoft.AspNetCore.Cors; |
| 11 | +using Microsoft.IdentityModel.Tokens; |
| 12 | + |
| 13 | +namespace WordToPdf.Controllers |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Controller for Word to PDF conversion. |
| 17 | + /// </summary> |
| 18 | + [Route("api/pdf/")] |
| 19 | + [ApiController] |
| 20 | + [EnableCors("MyPolicy")] |
| 21 | + public class ConversionController : ControllerBase |
| 22 | + { |
| 23 | + #region Fields |
| 24 | + /// <summary> |
| 25 | + /// Hosting environment information. |
| 26 | + /// </summary> |
| 27 | + private readonly IWebHostEnvironment _webHostEnvironment; |
| 28 | + #endregion |
| 29 | + |
| 30 | + #region Constructor |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance of the ConversionController class with the specified hosting environment. |
| 33 | + /// </summary> |
| 34 | + /// <param name="webHostEnvironment">Represents the hosting environment of the application.</param> |
| 35 | + public ConversionController(IWebHostEnvironment webHostEnvironment) |
| 36 | + { |
| 37 | + _webHostEnvironment = webHostEnvironment; |
| 38 | + } |
| 39 | + #endregion |
| 40 | + |
| 41 | + #region Web API |
| 42 | + /// <summary> |
| 43 | + /// Convert Word document to PDF. |
| 44 | + /// </summary> |
| 45 | + /// <param name="settings">Settings to customize the Word to PDF conversion.</param> |
| 46 | + [HttpPost] |
| 47 | + [Route("convertwordtopdf")] |
| 48 | + public IActionResult WordToPDFConversion([FromForm] WordToPdfSettings settings) |
| 49 | + { |
| 50 | + //Convert Word document to PDF |
| 51 | + return WordToPdf(settings); |
| 52 | + } |
| 53 | + #endregion |
| 54 | + |
| 55 | + #region Helper Methods |
| 56 | + /// <summary> |
| 57 | + /// Convert Word document to PDF with the customization settings. |
| 58 | + /// </summary> |
| 59 | + /// <param name="settings"></param> |
| 60 | + /// <returns></returns> |
| 61 | + private IActionResult WordToPdf(WordToPdfSettings settings) |
| 62 | + { |
| 63 | + try |
| 64 | + { |
| 65 | + if (settings.InputFile != null) |
| 66 | + { |
| 67 | + |
| 68 | + //Get input file |
| 69 | + MemoryStream docStream = new MemoryStream(); |
| 70 | + settings.InputFile.CopyTo(docStream); |
| 71 | + docStream.Position = 0; |
| 72 | + |
| 73 | + WordDocument wordDocument; |
| 74 | + //Load Word document. |
| 75 | + if (string.IsNullOrEmpty(settings.Password)) |
| 76 | + { |
| 77 | + wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic, settings.Password); |
| 82 | + } |
| 83 | + docStream.Dispose(); |
| 84 | + |
| 85 | + //Instantiation of DocIORenderer for Word to PDF conversion. |
| 86 | + DocIORenderer render = new DocIORenderer(); |
| 87 | + |
| 88 | + //Apply settings for Word to PDF conversion. |
| 89 | + ApplyWordToPDFSettings(render.Settings, settings, wordDocument); |
| 90 | + |
| 91 | + //Converts Word document into PDF document |
| 92 | + PdfDocument pdfDocument = render.ConvertToPDF(wordDocument); |
| 93 | + |
| 94 | + //Dispose the resources. |
| 95 | + render.Dispose(); |
| 96 | + wordDocument.Dispose(); |
| 97 | + |
| 98 | + //Saves the PDF document to MemoryStream. |
| 99 | + MemoryStream stream = new MemoryStream(); |
| 100 | + pdfDocument.Save(stream); |
| 101 | + stream.Position = 0; |
| 102 | + //Dispose the PDF resources. |
| 103 | + pdfDocument.Close(true); |
| 104 | + PdfDocument.ClearFontCache(); |
| 105 | + |
| 106 | + //Return the PDF document. |
| 107 | + return File(stream, "application/pdf", "OutputFile.pdf"); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + catch (Exception ex) |
| 112 | + { |
| 113 | + return StatusCode(500, $"An error occurred during the conversion process: {ex.Message}"); |
| 114 | + } |
| 115 | + |
| 116 | + return StatusCode(500, "An error occurred during Word to PDF conversion."); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Applies the settings for Word to PDF conversion from the Word to PDF API options. |
| 121 | + /// </summary> |
| 122 | + /// <param name="rendererSettings">The settings for the DocIORenderer.</param> |
| 123 | + /// <param name="settings">The Word to PDF API options.</param> |
| 124 | + /// <param name="document">The Word document to be converted.</param> |
| 125 | + internal static void ApplyWordToPDFSettings(DocIORendererSettings rendererSettings, WordToPdfSettings settings, WordDocument document) |
| 126 | + { |
| 127 | + //Set the flag to optimize identical images during Word to PDF conversion. |
| 128 | + rendererSettings.OptimizeIdenticalImages = true; |
| 129 | + |
| 130 | + //Set the flag to generate tagged PDF from the Word document. |
| 131 | + if (settings.TaggedPDF.HasValue) |
| 132 | + rendererSettings.AutoTag = settings.TaggedPDF.Value; |
| 133 | + |
| 134 | + //Set the revision markups to include the revision of tracked changes in the Word document in the converted PDF. |
| 135 | + if (settings.IncludeRevisionsMarks.HasValue && settings.IncludeRevisionsMarks.Value) |
| 136 | + document.RevisionOptions.ShowMarkup = RevisionType.Deletions | RevisionType.Formatting | RevisionType.Insertions; |
| 137 | + |
| 138 | + //Set the comment display mode as balloons to render Word document comments in the converted PDF. |
| 139 | + if (settings.IncludeComments.HasValue && settings.IncludeComments.Value) |
| 140 | + document.RevisionOptions.CommentDisplayMode = CommentDisplayMode.ShowInBalloons; |
| 141 | + |
| 142 | + //Set the flag to preserve text form fields as editable PDF form fields. |
| 143 | + if (settings.EditablePDF.HasValue) |
| 144 | + rendererSettings.PreserveFormFields = settings.EditablePDF.Value; |
| 145 | + |
| 146 | + //Set the flag to embed fonts in the converted PDF. |
| 147 | + if (settings.EmbedFontsInPDF.HasValue && settings.EmbedFontsInPDF.Value) |
| 148 | + rendererSettings.EmbedCompleteFonts = settings.EmbedFontsInPDF.Value; |
| 149 | + |
| 150 | + //Set the flag to auto-detect complex script during Word to PDF conversion. |
| 151 | + if (settings.AutoDetectComplexScript.HasValue && settings.AutoDetectComplexScript.Value) |
| 152 | + rendererSettings.AutoDetectComplexScript = settings.AutoDetectComplexScript.Value; |
| 153 | + |
| 154 | + //Set the PDF conformance level. |
| 155 | + rendererSettings.PdfConformanceLevel = GetPdfConformanceLevel(settings.PdfConformanceLevel); |
| 156 | + |
| 157 | + //Set the option to generate a PDF document with bookmarks for Word document paragraphs with a heading style and outline level. |
| 158 | + if (settings.HeadingsAsPdfBookmarks.HasValue) |
| 159 | + rendererSettings.ExportBookmarks = settings.HeadingsAsPdfBookmarks.Value ? Syncfusion.DocIO.ExportBookmarkType.Headings | Syncfusion.DocIO.ExportBookmarkType.Bookmarks: |
| 160 | + Syncfusion.DocIO.ExportBookmarkType.Bookmarks; |
| 161 | + } |
| 162 | + /// <summary> |
| 163 | + /// Convert the PDF conformance from Web API to its equivalent <see cref="Syncfusion.Pdf.PdfConformanceLevel"/> |
| 164 | + /// </summary> |
| 165 | + /// <param name="pdfConformance">PDF conformance from Web API.</param> |
| 166 | + /// <returns>Returns <see cref="Syncfusion.Pdf.PdfConformanceLevel"/></returns> |
| 167 | + private static PdfConformanceLevel GetPdfConformanceLevel(PDFConformance? pdfConformance) |
| 168 | + { |
| 169 | + if (!pdfConformance.HasValue) |
| 170 | + return PdfConformanceLevel.None; |
| 171 | + |
| 172 | + switch (pdfConformance) |
| 173 | + { |
| 174 | + case PDFConformance.None: |
| 175 | + return PdfConformanceLevel.None; |
| 176 | + case PDFConformance.Pdf_A1B: |
| 177 | + return PdfConformanceLevel.Pdf_A1B; |
| 178 | + case PDFConformance.Pdf_A2B: |
| 179 | + return PdfConformanceLevel.Pdf_A2B; |
| 180 | + case PDFConformance.Pdf_A3B: |
| 181 | + return PdfConformanceLevel.Pdf_A3B; |
| 182 | + case PDFConformance.Pdf_A1A: |
| 183 | + return PdfConformanceLevel.Pdf_A1A; |
| 184 | + case PDFConformance.Pdf_A2A: |
| 185 | + return PdfConformanceLevel.Pdf_A2A; |
| 186 | + case PDFConformance.Pdf_A2U: |
| 187 | + return PdfConformanceLevel.Pdf_A2U; |
| 188 | + case PDFConformance.Pdf_A3A: |
| 189 | + return PdfConformanceLevel.Pdf_A3A; |
| 190 | + case PDFConformance.Pdf_A3U: |
| 191 | + return PdfConformanceLevel.Pdf_A3U; |
| 192 | + case PDFConformance.Pdf_A4: |
| 193 | + return PdfConformanceLevel.Pdf_A4; |
| 194 | + case PDFConformance.Pdf_A4E: |
| 195 | + return PdfConformanceLevel.Pdf_A4E; |
| 196 | + case PDFConformance.Pdf_A4F: |
| 197 | + return PdfConformanceLevel.Pdf_A4F; |
| 198 | + default: |
| 199 | + return PdfConformanceLevel.None; |
| 200 | + } |
| 201 | + } |
| 202 | + #endregion |
| 203 | + } |
| 204 | +} |
0 commit comments