CreateCIDFontFromTrueTypeFont(Stream) Метод (PdfFontManager)
В этом разделе
Создает шрифт CID, основанный на файле шрифта TrueType.
Синтаксис
Parameters
- stream
- Поток, содержащий файл шрифта TrueType.
Return Value
Пример
Вот пример, показывающий, как добавить текст на английском языке, Русский и китайский языки на PDF страницу:
''' <summary>
''' Creates page of PDF document with English, Russian and Chinese text.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
Public Shared Sub CreatePdfPageWithEnRuCh(pdfFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
' create empty PDF page
Dim page As New Vintasoft.Imaging.Pdf.Tree.PdfPage(document, Vintasoft.Imaging.PaperSizeKind.A4)
' add page to PDF document
document.Pages.Add(page)
' text size
Dim fontSize As Single = 24F
' text brush
Dim brush As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black)
' get names of font
Dim fontNames As New System.Collections.Generic.List(Of String)(Vintasoft.Imaging.Fonts.FontProgramsControllerBase.[Default].GetAvailableFontNames())
' create fornt for English text
Dim englishFont As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = Nothing
' if "Microsoft Himalaya" font exists
If fontNames.IndexOf("Microsoft Himalaya") >= 0 Then
Using programSearchResult As Vintasoft.Imaging.Fonts.FontProgramSearchResult = Vintasoft.Imaging.Fonts.FontProgramsControllerBase.[Default].GetTrueTypeFontProgram(New Vintasoft.Imaging.Fonts.FontInfo("Microsoft Himalaya"))
' create PDF font based on "Microsoft Himalaya" font
englishFont = document.FontManager.CreateSimpleFontFromTrueTypeFont(programSearchResult.FontProgramStream)
End Using
Else
' if "Microsoft Himalaya" font does NOT exist
Throw New System.Exception("'Microsoft Himalaya' font is not found in the system.")
End If
' create font for Russian text
Dim russianFont As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = Nothing
' if "Times New Roman" font exists
If fontNames.IndexOf("Times New Roman") >= 0 Then
' create PDF font based on "Times New Roman" font
Using programSearchResult As Vintasoft.Imaging.Fonts.FontProgramSearchResult = Vintasoft.Imaging.Fonts.FontProgramsControllerBase.[Default].GetTrueTypeFontProgram(New Vintasoft.Imaging.Fonts.FontInfo("Times New Roman"))
russianFont = document.FontManager.CreateCIDFontFromTrueTypeFont(programSearchResult.FontProgramStream)
End Using
Else
' if "Times New Roman" font does NOT exist
Throw New System.Exception("'Times New Roman' font is not found in the system.")
End If
' create font for Chinese text
Dim chineseFont As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = Nothing
' if "SimHei" font exists
If fontNames.IndexOf("SimHei") >= 0 Then
' create PDF font based on "SimHei" font
Using programSearchResult As Vintasoft.Imaging.Fonts.FontProgramSearchResult = Vintasoft.Imaging.Fonts.FontProgramsControllerBase.[Default].GetTrueTypeFontProgram(New Vintasoft.Imaging.Fonts.FontInfo("SimHei"))
chineseFont = document.FontManager.CreateCIDFontFromTrueTypeFont(programSearchResult.FontProgramStream)
End Using
Else
' if "SimHei" font does NOT exist
Throw New System.Exception("'SimHei' font is not found in the system.")
End If
' get PDF graphics of page
Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = page.GetGraphics()
' location of English text
Dim location As New System.Drawing.PointF(50, page.Size.Height - 50)
' draw English text
graphics.DrawString("Hello", englishFont, fontSize, brush, location)
' location of Russian text
location.Y -= 50
' draw Russian text
graphics.DrawString("Привет", russianFont, fontSize, brush, location)
' location of Chinese text
location.Y -= 50
' draw Chinese text
graphics.DrawString("您好", chineseFont, fontSize, brush, location)
End Using
' save changes to a file
document.SaveChanges()
End Using
End Sub
/// <summary>
/// Creates page of PDF document with English, Russian and Chinese text.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
public static void CreatePdfPageWithEnRuCh(string pdfFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
// create empty PDF page
Vintasoft.Imaging.Pdf.Tree.PdfPage page = new Vintasoft.Imaging.Pdf.Tree.PdfPage(
document, Vintasoft.Imaging.PaperSizeKind.A4);
// add page to PDF document
document.Pages.Add(page);
// text size
float fontSize = 24f;
// text brush
Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush =
new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black);
// get names of font
System.Collections.Generic.List<string> fontNames = new System.Collections.Generic.List<string>(Vintasoft.Imaging.Fonts.FontProgramsControllerBase.Default.GetAvailableFontNames());
// create fornt for English text
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont englishFont = null;
// if "Microsoft Himalaya" font exists
if (fontNames.IndexOf("Microsoft Himalaya") >= 0)
{
using (Vintasoft.Imaging.Fonts.FontProgramSearchResult programSearchResult =
Vintasoft.Imaging.Fonts.FontProgramsControllerBase.Default.GetTrueTypeFontProgram(new Vintasoft.Imaging.Fonts.FontInfo("Microsoft Himalaya")))
// create PDF font based on "Microsoft Himalaya" font
englishFont = document.FontManager.CreateSimpleFontFromTrueTypeFont(programSearchResult.FontProgramStream);
}
// if "Microsoft Himalaya" font does NOT exist
else
{
throw new System.Exception("'Microsoft Himalaya' font is not found in the system.");
}
// create font for Russian text
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont russianFont = null;
// if "Times New Roman" font exists
if (fontNames.IndexOf("Times New Roman") >= 0)
{
// create PDF font based on "Times New Roman" font
using (Vintasoft.Imaging.Fonts.FontProgramSearchResult programSearchResult =
Vintasoft.Imaging.Fonts.FontProgramsControllerBase.Default.GetTrueTypeFontProgram(new Vintasoft.Imaging.Fonts.FontInfo("Times New Roman")))
russianFont = document.FontManager.CreateCIDFontFromTrueTypeFont(programSearchResult.FontProgramStream);
}
// if "Times New Roman" font does NOT exist
else
{
throw new System.Exception("'Times New Roman' font is not found in the system.");
}
// create font for Chinese text
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont chineseFont = null;
// if "SimHei" font exists
if (fontNames.IndexOf("SimHei") >= 0)
{
// create PDF font based on "SimHei" font
using (Vintasoft.Imaging.Fonts.FontProgramSearchResult programSearchResult =
Vintasoft.Imaging.Fonts.FontProgramsControllerBase.Default.GetTrueTypeFontProgram(new Vintasoft.Imaging.Fonts.FontInfo("SimHei")))
chineseFont = document.FontManager.CreateCIDFontFromTrueTypeFont(programSearchResult.FontProgramStream);
}
// if "SimHei" font does NOT exist
else
{
throw new System.Exception("'SimHei' font is not found in the system.");
}
// get PDF graphics of page
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics = page.GetGraphics())
{
// location of English text
System.Drawing.PointF location = new System.Drawing.PointF(50, page.Size.Height - 50);
// draw English text
graphics.DrawString("Hello", englishFont, fontSize, brush, location);
// location of Russian text
location.Y -= 50;
// draw Russian text
graphics.DrawString("Привет", russianFont, fontSize, brush, location);
// location of Chinese text
location.Y -= 50;
// draw Chinese text
graphics.DrawString("您好", chineseFont, fontSize, brush, location);
}
// save changes to a file
document.SaveChanges();
}
}
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также