Convert DOCX to PDF file (set custom font, size and line spacing) in C# and .NET


Our customer has sent a request to us: How convert DOCX to PDF document. Furthermore, he also asked to change the all font data in the document to 'Times New Roman, 8pt' and specify the single line spacing as 0.8 for all paragraphs.


Complete code

using System;
using System.IO;
using SautinSoft.Document;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            SetCustomFontAndSize();
        }
        /// <summary>
        /// Convert DOCX document to PDF file (set custom font, size and line spacing).
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/from-customers-word-to-pdf-set-custom-font-and-size-csharp-vb-net.php
        /// </remarks>
        public static void SetCustomFontAndSize()
        {
            // Path to a loadable document.
            string inpFile = @"..\..\..\example.docx";
            string outFile = @"result set custom font.pdf";

            DocumentCore dc = DocumentCore.Load(inpFile);

            string singleFontName = "Times New Roman";
            float singleFontSize = 8.0f;
            float singleLineSpacing = 0.8f;

            dc.DefaultCharacterFormat.FontName = singleFontName;
            dc.DefaultCharacterFormat.Size = singleFontSize;

            foreach (Element element in dc.GetChildElements(true, ElementType.Run, ElementType.Paragraph))
            {
                if (element is Run)
                {
                    (element as Run).CharacterFormat.FontName = singleFontName;
                    (element as Run).CharacterFormat.Size = singleFontSize;
                }
                else if (element is Paragraph)
                {
                    (element as Paragraph).ParagraphFormat.LineSpacing = singleLineSpacing;
                }
            }
            dc.Save(outFile);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document

Namespace Sample
	Friend Class Sample
		Shared Sub Main(ByVal args() As String)
			SetCustomFontAndSize()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Convert DOCX document to PDF file (set custom font, size and line spacing).
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/from-customers-word-to-pdf-set-custom-font-and-size-csharp-vb-net.php
		''' </remarks>
		Public Shared Sub SetCustomFontAndSize()
			' Path to a loadable document.
			Dim inpFile As String = "..\..\..\example.docx"
			Dim outFile As String = "result set custom font.pdf"

			Dim dc As DocumentCore = DocumentCore.Load(inpFile)

			Dim singleFontName As String = "Times New Roman"
			Dim singleFontSize As Single = 8.0F
			Dim singleLineSpacing As Single = 0.8F

			dc.DefaultCharacterFormat.FontName = singleFontName
			dc.DefaultCharacterFormat.Size = singleFontSize

			For Each element As Element In dc.GetChildElements(True, ElementType.Run, ElementType.Paragraph)
				If TypeOf element Is Run Then
					TryCast(element, Run).CharacterFormat.FontName = singleFontName
					TryCast(element, Run).CharacterFormat.Size = singleFontSize
				ElseIf TypeOf element Is Paragraph Then
					TryCast(element, Paragraph).ParagraphFormat.LineSpacing = singleLineSpacing
				End If
			Next element
			dc.Save(outFile)

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(inpFile) With {.UseShellExecute = True})
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.