В мире обработки документов точное размещение текста имеет решающее значение для создания профессиональных и читабельных документов. Независимо от того, работаете ли вы с простым текстом или сложными скриптами, Pdf.Net предоставляет функции, необходимые для обеспечения наилучшего внешнего вида ваших документов, и точное расположение текста может иметь решающее значение. Эта статья поможет вам улучшить размещение текста на C# и .NET с помощью библиотеки SautinSoft.Pdf.
Чтобы разместить текст в определённом месте на странице PDF, вы можете использовать метод "DrawText". Для этого метода требуются объекты "PdfFormattedText" и "PdfPoint", которые задают координаты. Для более удобного размещения текста вы можете использовать различные свойства и методы, предоставляемые библиотекой Pdf.Net, например, вы можете выровнять текст по центру или правому краю страницы или даже по ширине.
Pdf.Net также поддерживает написание текста сложными шрифтами, что гарантирует совместимость ваших документов с различными языками и системами письма.
В следующем примере показано, как выравнивать и размещать текст в разных местах на странице PDF-документа:
Полный код
using System;
using SautinSoft.Pdf;
using System.IO;
using SautinSoft.Pdf.Content;
class Program
{
/// <summary>
/// How to set text alignment and positioning.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/text-alignment-and-positioning.php
/// </remarks>
static void Main()
{
// Before starting this example, please get a free 100-day trial key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// PdfDocument.SetLicense("...");
using (var document = new PdfDocument())
{
// Add a new page.
var page = document.Pages.Add();
double margin = 10;
using (var formattedText = new PdfFormattedText())
{
// Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Left;
formattedText.MaxTextWidth = 100;
formattedText.Append("This text is left aligned, ").
Append("placed in the top-left corner of the page and ").
Append("its width should not exceed 100 points.");
// Draw left-aligned text in the top-left corner of the page.
page.Content.DrawText(formattedText,
new PdfPoint(margin,
page.CropBox.Top - margin - formattedText.Height));
// Clear the PdfFormattedText object.
formattedText.Clear();
// Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Center;
formattedText.MaxTextWidth = 200;
formattedText.Append("This text is center aligned, ").
Append("placed in the top-center part of the page ").
Append("and its width should not exceed 200 points.");
// Draw center-aligned text at the top-center part of the page.
page.Content.DrawText(formattedText,
new PdfPoint((page.CropBox.Width - formattedText.MaxTextWidth) / 2,
page.CropBox.Top - margin - formattedText.Height));
// Clear the PdfFormattedText object.
formattedText.Clear();
// Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Right;
formattedText.MaxTextWidth = 100;
formattedText.Append("This text is right aligned, ").
Append("placed in the top-right corner of the page ").
Append("and its width should not exceed 100 points.");
// Draw right-aligned text in the top-right corner of the page.
page.Content.DrawText(formattedText,
new PdfPoint(page.CropBox.Width - margin - formattedText.MaxTextWidth,
page.CropBox.Top - margin - formattedText.Height));
// Clear the PdfFormattedText object.
formattedText.Clear();
// Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Left;
formattedText.MaxTextWidth = 100;
formattedText.Append("This text is left aligned, ").
Append("placed in the bottom-left corner of the page and ").
Append("its width should not exceed 100 points.");
// Draw left-aligned text in the bottom-left corner of the page.
page.Content.DrawText(formattedText,
new PdfPoint(margin,
margin));
// Clear the PdfFormattedText object.
formattedText.Clear();
// Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Center;
formattedText.MaxTextWidth = 200;
formattedText.Append("This text is center aligned, ").
Append("placed in the bottom-center part of the page and ").
Append("its width should not exceed 200 points.");
// Draw center-aligned text at the bottom-center part of the page.
page.Content.DrawText(formattedText,
new PdfPoint((page.CropBox.Width - formattedText.MaxTextWidth) / 2,
margin));
// Clear the PdfFormattedText object.
formattedText.Clear();
// Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Right;
formattedText.MaxTextWidth = 100;
formattedText.Append("This text is right aligned, ").
Append("placed in the bottom-right corner of the page and ").
Append("its width should not exceed 100 points.");
// Draw right-aligned text in the bottom-right corner of the page.
page.Content.DrawText(formattedText,
new PdfPoint(page.CropBox.Width - margin - formattedText.MaxTextWidth,
margin));
// Clear the PdfFormattedText object.
formattedText.Clear();
// Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Justify;
formattedText.MaxTextWidths = new double[] { 200, 150, 100 };
formattedText.Append("This text has justified alignment, ").
Append("is placed in the center of the page and ").
Append("its first line should not exceed 200 points, ").
Append("its second line should not exceed 150 points and ").
Append("its third and all other lines should not exceed 100 points.");
// Draw justify-aligned text in the center part of the page.
// Center the text based on the width of the most lines, which is formattedText.MaxTextWidths[2].
page.Content.DrawText(formattedText,
new PdfPoint((page.CropBox.Width - formattedText.MaxTextWidths[2]) / 2,
(page.CropBox.Height - formattedText.Height) / 2));
// Save a PDF Document.
document.Save("Alignment and Positioning.pdf");
}
}
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Alignment and Positioning.pdf") { UseShellExecute = true });
}
}
Option Infer On
Imports System
Imports SautinSoft.Pdf
Imports System.IO
Imports SautinSoft.Pdf.Content
Friend Class Program
''' <summary>
''' How to set text alignment and positioning.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/text-alignment-and-positioning.php
''' </remarks>
Shared Sub Main()
' Before starting this example, please get a free license:
' https://sautinsoft.com/start-for-free/
' Apply the key here:
' PdfDocument.SetLicense("...");
Using document = New PdfDocument()
' Add a new page.
Dim page = document.Pages.Add()
Dim margin As Double = 10
Using formattedText = New PdfFormattedText()
' Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Left
formattedText.MaxTextWidth = 100
formattedText.Append("This text is left aligned, ").Append("placed in the top-left corner of the page and ").Append("its width should not exceed 100 points.")
' Draw left-aligned text in the top-left corner of the page.
page.Content.DrawText(formattedText, New PdfPoint(margin, page.CropBox.Top - margin - formattedText.Height))
' Clear the PdfFormattedText object.
formattedText.Clear()
' Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Center
formattedText.MaxTextWidth = 200
formattedText.Append("This text is center aligned, ").Append("placed in the top-center part of the page ").Append("and its width should not exceed 200 points.")
' Draw center-aligned text at the top-center part of the page.
' INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
page.Content.DrawText(formattedText, New PdfPoint((page.CropBox.Width - formattedText.MaxTextWidth) / 2, page.CropBox.Top - margin - formattedText.Height))
' Clear the PdfFormattedText object.
formattedText.Clear()
' Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Right
formattedText.MaxTextWidth = 100
formattedText.Append("This text is right aligned, ").Append("placed in the top-right corner of the page ").Append("and its width should not exceed 100 points.")
' Draw right-aligned text in the top-right corner of the page.
page.Content.DrawText(formattedText, New PdfPoint(page.CropBox.Width - margin - formattedText.MaxTextWidth, page.CropBox.Top - margin - formattedText.Height))
' Clear the PdfFormattedText object.
formattedText.Clear()
' Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Left
formattedText.MaxTextWidth = 100
formattedText.Append("This text is left aligned, ").Append("placed in the bottom-left corner of the page and ").Append("its width should not exceed 100 points.")
' Draw left-aligned text in the bottom-left corner of the page.
page.Content.DrawText(formattedText, New PdfPoint(margin, margin))
' Clear the PdfFormattedText object.
formattedText.Clear()
' Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Center
formattedText.MaxTextWidth = 200
formattedText.Append("This text is center aligned, ").Append("placed in the bottom-center part of the page and ").Append("its width should not exceed 200 points.")
' Draw center-aligned text at the bottom-center part of the page.
' INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
page.Content.DrawText(formattedText, New PdfPoint((page.CropBox.Width - formattedText.MaxTextWidth) / 2, margin))
' Clear the PdfFormattedText object.
formattedText.Clear()
' Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Right
formattedText.MaxTextWidth = 100
formattedText.Append("This text is right aligned, ").Append("placed in the bottom-right corner of the page and ").Append("its width should not exceed 100 points.")
' Draw right-aligned text in the bottom-right corner of the page.
page.Content.DrawText(formattedText, New PdfPoint(page.CropBox.Width - margin - formattedText.MaxTextWidth, margin))
' Clear the PdfFormattedText object.
formattedText.Clear()
' Set up and fill the PdfFormattedText object.
formattedText.TextAlignment = PdfTextAlignment.Justify
formattedText.MaxTextWidths = New Double() { 200, 150, 100 }
formattedText.Append("This text has justified alignment, ").Append("is placed in the center of the page and ").Append("its first line should not exceed 200 points, ").Append("its second line should not exceed 150 points and ").Append("its third and all other lines should not exceed 100 points.")
' Draw justify-aligned text in the center part of the page.
' Center the text based on the width of the most lines, which is formattedText.MaxTextWidths[2].
' INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
page.Content.DrawText(formattedText, New PdfPoint((page.CropBox.Width - formattedText.MaxTextWidths(2)) / 2, (page.CropBox.Height - formattedText.Height) \ 2))
' Save a PDF Document.
document.Save("Alignment and Positioning.pdf")
End Using
End Using
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Alignment and Positioning.pdf") With {.UseShellExecute = True})
End Sub
End Class
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: