Skip to content

Commit 405a8eb

Browse files
Merge pull request #338 from SindhuRSF4549/main
Added Word to PDF with Conversion settings
2 parents b90af44 + b08121b commit 405a8eb

File tree

86 files changed

+75021
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+75021
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Word to PDF Conversion API
2+
3+
## Overview
4+
5+
This repository contains a Web API application in ASP.NET to convert Word to PDF with conversion settings. You can refer to this to maintain the Web API on your side. Additionally, you can refer to the Blazor, Vue, JavaScript, React, and Angular repositories to learn how to invoke this Web API on different platforms.
6+
7+
## How to run the sample
8+
9+
1. Clone or Download the Repository
10+
11+
2. Build the Application
12+
13+
3. Run the API
14+
15+
This will start the API, and Swagger UI will be available at:
16+
17+
```
18+
http://localhost:<port>/swagger
19+
```
20+
21+
## Using the API
22+
23+
1. Open Swagger UI
24+
25+
Navigate to:
26+
27+
```
28+
http://localhost:<port>/swagger
29+
```
30+
31+
in your browser.
32+
33+
2. Upload a Word Document
34+
35+
- Use the `POST /convert` endpoint.
36+
- Click **Try it out**.
37+
- Select a Word document (`.docx`) in the **InputFile** field.
38+
- Adjust the optional conversion settings as needed.
39+
- Click **Execute** to convert the document.
40+
41+
3. Response
42+
43+
- The API will return a downloadable PDF file.
44+
- If an error occurs, it will return an appropriate error message.
45+
46+
## Word to PDF Conversion Settings
47+
48+
The API supports the following optional settings:
49+
50+
- **Password** – Password to open a protected document.
51+
- **EmbedFontsInPDF** – Embed fonts in the PDF (default: `false`).
52+
- **EditablePDF** – Preserve form fields as editable fields (default: `true`).
53+
- **AutoDetectComplexScript** – Detect complex script text (default: `false`).
54+
- **TaggedPDF** – Convert to tagged PDF (default: `false`).
55+
- **PdfConformanceLevel** – Specify the PDF conformance level.
56+
- **HeadingsAsPdfBookmarks** – Preserve Word headings as bookmarks (default: `false`).
57+
- **IncludeComments** – Include comments in the PDF (default: `false`).
58+
- **IncludeRevisionsMarks** – Include revision marks in the PDF (default: `false`).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using Syncfusion.Pdf;
2+
using System.ComponentModel;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Runtime.InteropServices;
5+
6+
namespace WordToPdf.Models
7+
{
8+
/// <summary>
9+
/// Provides settings for customizing Word to PDF conversion.
10+
/// </summary>
11+
public class WordToPdfSettings
12+
{
13+
/// <summary>
14+
/// The input Word document.
15+
/// </summary>
16+
[Required]
17+
public IFormFile? InputFile { get; set; }
18+
/// <summary>
19+
/// Specifies the password to open the protected input Word document.
20+
/// </summary>
21+
public string? Password { get; set; }
22+
/// <summary>
23+
/// Flag to embed the complete font information in the converted PDF document. The default value is false.
24+
/// </summary>
25+
[DefaultValue("false")]
26+
public bool? EmbedFontsInPDF { get; set; }
27+
/// <summary>
28+
/// Flag to preserve Word form fields as editable PDF form fields.
29+
/// </summary>
30+
[DefaultValue("true")]
31+
public bool? EditablePDF { get; set; } = true;
32+
/// <summary>
33+
/// Flag to automatically detect complex script text present in the Word document during PDF conversion. The default value is false.
34+
/// </summary>
35+
[DefaultValue("false")]
36+
public bool? AutoDetectComplexScript { get; set; } = false;
37+
/// <summary>
38+
/// Flag to convert the PDF document as tagged PDF or not. The default value is false.
39+
/// </summary>
40+
[DefaultValue("false")]
41+
public bool? TaggedPDF { get; set; } = false;
42+
43+
/// <summary>
44+
/// Specifies the PDF document's conformance level.
45+
/// </summary>
46+
public PDFConformance? PdfConformanceLevel { get; set; } = PDFConformance.None;
47+
/// <summary>
48+
/// Flag to preserve the headings of the Word document as PDF bookmarks during the conversion process. The default value is true.
49+
/// </summary>
50+
[DefaultValue("true")]
51+
public bool? HeadingsAsPdfBookmarks { get; set; } = false;
52+
/// <summary>
53+
/// Flag to include comments from the Word document in the PDF.
54+
/// </summary>
55+
[DefaultValue("false")]
56+
public bool? IncludeComments { get; set; } = false;
57+
/// <summary>
58+
/// Flag to include revision of tracked changes Word document in the PDF.
59+
/// </summary>
60+
[DefaultValue("false")]
61+
public bool? IncludeRevisionsMarks { get; set; } = false;
62+
}
63+
/// <summary>
64+
/// Specifies the PDF document's conformance level.
65+
/// </summary>
66+
public enum PDFConformance
67+
{
68+
///<summary>
69+
///Specifies Default / No Conformance.
70+
///</summary>
71+
None,
72+
///<summary>
73+
///This PDF/A ISO standard [ISO 19005-1:2005] is based on Adobe PDF version 1.4 and this Level B conformance indicates minimal compliance to ensure that the rendered visual appearance of a conforming file is preservable over the long term.
74+
///</summary>
75+
Pdf_A1B,
76+
/// <summary>
77+
/// PDF/A-2 Standard is based on a PDF 1.7 (ISO 32000-1) which provides support for transparency effects and layers embedding of OpenType fonts
78+
/// </summary>
79+
Pdf_A2B,
80+
/// <summary>
81+
/// PDF/A-3 Standard is based on a PDF 1.7 (ISO 32000-1) which provides support for embedding the arbitrary file formats (XML, CSV, CAD, Word Processing documents)
82+
/// </summary>
83+
Pdf_A3B,
84+
/// <summary>
85+
/// This PDF/A ISO standard [ISO 19005-1:2005] is based on Adobe PDF version 1.4 and this Level A conformance was intended to increase the accessibility of conforming files for physically impaired users by allowing assistive software, such as screen readers, to more precisely extract and interpret a file's contents.
86+
/// </summary>
87+
Pdf_A1A,
88+
/// <summary>
89+
/// PDF/A-2 Standard is based on a PDF 1.7 (ISO 32000-1) and this Level A conformance was intended to increase the accessibility of conforming files for physically impaired users by allowing assistive software, such as screen readers, to more precisely extract and interpret a file's contents.
90+
/// </summary>
91+
Pdf_A2A,
92+
/// <summary>
93+
/// PDF/A-2 Standard is based on a PDF 1.7 (ISO 32000-1) and this Level U conformance represents Level B conformance (PDF/A-2b) with the additional requirement that all text in the document have Unicode mapping.
94+
/// </summary>
95+
Pdf_A2U,
96+
/// <summary>
97+
///PDF/A-3 Standard is based on a PDF 1.7 (ISO 32000-1) which provides support for embedding the arbitrary file formats (XML, CSV, CAD, Word Processing documents) and This Level A conformance was intended to increase the accessibility of conforming files for physically impaired users by allowing assistive software, such as screen readers, to more precisely extract and interpret a file's contents.
98+
/// </summary>
99+
Pdf_A3A,
100+
/// <summary>
101+
/// PDF/A-3 Standard is based on a PDF 1.7 (ISO 32000-1) and this Level U conformance represents Level B conformance (PDF/A-3b) with the additional requirement that all text in the document have Unicode mapping.
102+
/// </summary>
103+
Pdf_A3U,
104+
/// <summary>
105+
/// PDF/A-4 Standard is based on a PDF 2.0 (ISO 32000-2). The separate conformance levels a, b, and u are not used in PDF/A-4. Instead, PDF/A-4 encourages but does not require the addition of higher-level logical structures, and it requires Unicode mappings for all fonts.
106+
/// </summary>
107+
Pdf_A4,
108+
/// <summary>
109+
/// PDF/A-4E Standard is based on a PDF 2.0 (ISO 32000-2). PDF/A-4e is intended for engineering documents and acts as a successor to the PDF/E-1 standard. PDF/A-4e supports Rich Media and 3D Annotations as well as embedded files.
110+
/// </summary>
111+
Pdf_A4E,
112+
/// <summary>
113+
/// PDF/A-4F Standard is based on a PDF 2.0 (ISO 32000-2). It allows embedding files in any other format.
114+
/// </summary>
115+
Pdf_A4F
116+
}
117+
}

0 commit comments

Comments
 (0)