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 trial 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 });
}
}
}
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 trial 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
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: