Наш клиент прислал нам запрос: Как преобразовать документ DOCX в PDF. Кроме того, он также попросил изменить все шрифтовые данные в документе на "Times New Roman, 8pt" и указать одинарный межстрочный интервал равным 0,8 для всех абзацев.
Полный код
using System;
using System.IO;
using SautinSoft.Document;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Get your free 100-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 });
}
}
}
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 100-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
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: