Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
以下代码片段也适用于 Aspose.PDF.Drawing 库。
要向现有 PDF 文件添加文本:
以下代码片段向您展示如何在现有 PDF 文件中添加文本。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddText()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Get particular page
var page = document.Pages.Add();
// Create text fragment
var textFragment = new Aspose.Pdf.Text.TextFragment("main text");
textFragment.Position = new Aspose.Pdf.Text.Position(100, 600);
// Set text properties
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman");
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
// Create TextBuilder object
var textBuilder = new Aspose.Pdf.Text.TextBuilder(page);
// Append the text fragment to the PDF page
textBuilder.AppendText(textFragment);
// Save PDF document
document.Save(dataDir + "AddText_out.pdf");
}
}
以下代码片段展示了在向 PDF 文档添加文本时如何从 Stream 对象加载字体。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void LoadingFontFromStream()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
var fontFile = dataDir + "HPSimplified.ttf";
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "LoadFonts.pdf"))
{
// Create text builder object for first page of document
var textBuilder = new Aspose.Pdf.Text.TextBuilder(document.Pages[1]);
// Create text fragment with sample string
var textFragment = new Aspose.Pdf.Text.TextFragment("Hello world");
if (File.Exists(fontFile))
{
// Load the TrueType font into stream object
using (FileStream fontStream = File.OpenRead(fontFile))
{
// Set the font name for text string
textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.OpenFont(fontStream, Aspose.Pdf.Text.FontTypes.TTF);
// Specify the position for Text Fragment
textFragment.Position = new Aspose.Pdf.Text.Position(10, 10);
// Add the text to TextBuilder so that it can be placed over the PDF file
textBuilder.AppendText(textFragment);
}
// Save PDF document
document.Save(dataDir + "LoadingFontFromStream_out.pdf");
}
}
}
以下代码片段展示了如何使用 TextParagraph 类在 PDF 文档中添加文本。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTextWithTextParagraph()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of Document object
var page = document.Pages.Add();
var builder = new Aspose.Pdf.Text.TextBuilder(page);
// Create text paragraph
var paragraph = new Aspose.Pdf.Text.TextParagraph();
// Set subsequent lines indent
paragraph.SubsequentLinesIndent = 20;
// Specify the location to add TextParagraph
paragraph.Rectangle = new Aspose.Pdf.Rectangle(100, 300, 200, 700);
// Specify word wraping mode
paragraph.FormattingOptions.WrapMode = Aspose.Pdf.Text.TextFormattingOptions.WordWrapMode.ByWords;
// Create text fragment
var fragment = new Aspose.Pdf.Text.TextFragment("the quick brown fox jumps over the lazy dog");
fragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Times New Roman");
fragment.TextState.FontSize = 12;
// Add fragment to paragraph
paragraph.AppendLine(fragment);
// Add paragraph
builder.AppendParagraph(paragraph);
// Save PDF document
document.Save(dataDir + "AddTextUsingTextParagraph_out.pdf");
}
}
一个 PDF 页面可以包含一个或多个 TextFragment 对象,其中每个 TextFragment 对象可以有一个或多个 TextSegment 实例。为了为 TextSegment 设置超链接,可以使用 TextSegment 类的 Hyperlink 属性,同时提供 Aspose.Pdf.WebHyperlink 实例的对象。请尝试使用以下代码片段来实现此要求。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddHyperlinkToTextSegment()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Create TextFragment instance
var fragment = new Aspose.Pdf.Text.TextFragment("Sample Text Fragment");
// Set horizontal alignment for TextFragment
fragment.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
// Create a textsegment with sample text
var segment = new Aspose.Pdf.Text.TextSegment(" ... Text Segment 1...");
// Add segment to segments collection of TextFragment
fragment.Segments.Add(segment);
// Create a new TextSegment
segment = new Aspose.Pdf.Text.TextSegment("Link to Google");
// Add segment to segments collection of TextFragment
fragment.Segments.Add(segment);
// Set hyperlink for TextSegment
segment.Hyperlink = new Aspose.Pdf.WebHyperlink("www.google.com");
// Set forground color for text segment
segment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
// Set text formatting as italic
segment.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic;
// Create another TextSegment object
segment = new Aspose.Pdf.Text.TextSegment("TextSegment without hyperlink");
// Add segment to segments collection of TextFragment
fragment.Segments.Add(segment);
// Add TextFragment to paragraphs collection of page object
page.Paragraphs.Add(fragment);
// Save PDF document
document.Save(dataDir + "AddHyperlinkToTextSegment_out.pdf");
}
}
Aspose.PDF for .NET 提供了在创建/操作 PDF 文件内容时使用自定义/TrueType 字体的功能,以便文件内容使用除默认系统字体以外的内容显示。从 Aspose.PDF for .NET 10.3.0 版本开始,我们提供了对 Open Type Fonts 的支持。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UseOTFFont()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Create TextFragment instnace with sample text
var fragment = new Aspose.Pdf.Text.TextFragment("Sample Text in OTF font");
// Find font inside system font directory
// Fragment.TextState.Font = FontRepository.FindFont("HelveticaNeueLT Pro 45 Lt");
// Or you can even specify the path of OTF font in system directory
fragment.TextState.Font = Aspose.Pdf.Text.FontRepository.OpenFont(dataDir + "space age.otf");
// Specify to emend font inside PDF file, so that its displayed properly,
// Even if specific font is not installed/present over target machine
fragment.TextState.Font.IsEmbedded = true;
// Add TextFragment to paragraphs collection of Page instance
page.Paragraphs.Add(fragment);
// Save PDF document
document.Save(dataDir + "OTFFont_out.pdf");
}
}
Aspose.Pdf.Generator.Text 类包含一个名为 IsHtmlTagSupported 的属性,使得可以将 HTML 标签/内容添加到 PDF 文件中。添加的内容以原生 HTML 标签呈现,而不是作为简单的文本字符串出现。为了在 Aspose.Pdf 命名空间的新文档对象模型 (DOM) 中支持类似功能,引入了 HtmlFragment 类。
HtmlFragment 实例可用于指定应放置在 PDF 文件中的 HTML 内容。与 TextFragment 类似,HtmlFragment 是一个段落级对象,可以添加到 Page 对象的段落集合中。以下代码片段展示了使用 DOM 方法将 HTML 内容放置在 PDF 文件中的步骤。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddHTMLStringUsingDOM()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add a page to pages collection of PDF file
var page = document.Pages.Add();
// Instantiate HtmlFragment with HTML contnets
var title = new Aspose.Pdf.HtmlFragment("<fontsize=10><b><i>Table</i></b></fontsize>");
// Set bottom margin information
title.Margin.Bottom = 10;
// Set top margin information
title.Margin.Top = 200;
// Add HTML Fragment to paragraphs collection of page
page.Paragraphs.Add(title);
// Save PDF document
document.Save(dataDir + "AddHTMLUsingDOM_out.pdf");
}
}
以下代码片段演示了如何将 HTML 有序列表添加到文档中:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddHTMLOrderedListIntoDocument()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Instantiate HtmlFragment object with corresponding HTML fragment
var fragment = new Aspose.Pdf.HtmlFragment("`<body style='line-height: 100px;'><ul><li>First</li><li>Second</li><li>Third</li><li>Fourth</li><li>Fifth</li></ul>Text after the list.<br/>Next line<br/>Last line</body>`");
// Add Page in Pages Collection
var page = document.Pages.Add();
// Add HtmlFragment inside page
page.Paragraphs.Add(fragment);
// Save PDF document
document.Save(dataDir + "AddHTMLOrderedListIntoDocuments_out.pdf");
}
}
您还可以使用 TextState 对象设置 HTML 字符串格式,如下所示:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetHTMLStringFormatting()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
var fragment = new Aspose.Pdf.HtmlFragment("some text");
fragment.TextState = new Aspose.Pdf.Text.TextState();
fragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Calibri");
// Add Page in Pages Collection
var page = document.Pages.Add();
// Add HtmlFragment inside page
page.Paragraphs.Add(fragment);
// Save PDF document
document.Save(dataDir + "SetHTMLStringFormatting_out.pdf");
}
}
如果您通过 HTML 标记设置了一些文本属性值,然后在 TextState 属性中提供相同的值,它们将覆盖 TextState 实例的属性中的 HTML 参数。以下代码片段展示了描述的行为。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddHTMLUsingDOMAndOverwrite()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add a page to pages collection of PDF file
var page = document.Pages.Add();
// Instantiate HtmlFragment with HTML contnets
var title = new Aspose.Pdf.HtmlFragment("<p style='font-family: Verdana'><b><i>Table contains text</i></b></p>");
//Font-family from 'Verdana' will be reset to 'Arial'
title.TextState = new Aspose.Pdf.Text.TextState("Arial");
title.TextState.FontSize = 20;
// Set bottom margin information
title.Margin.Bottom = 10;
// Set top margin information
title.Margin.Top = 400;
// Add HTML Fragment to paragraphs collection of page
page.Paragraphs.Add(title);
// Save PDF document
document.Save(dataDir + "AddHTMLUsingDOMAndOverwrite_out.pdf");
}
}
脚注通过使用连续的上标数字在您的论文文本中指示注释。实际注释是缩进的,并且可以作为页面底部的脚注出现。
在脚注引用系统中,通过以下方式指示引用:
脚注的优点在于,读者可以简单地向下查看页面,以发现他们感兴趣的引用的来源。
请按照以下步骤创建脚注:
以下示例演示了如何将脚注添加到 PDF 页面底部并定义自定义线条样式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CustomLineStyleForFootNote()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Create GraphInfo object
var graph = new Aspose.Pdf.GraphInfo();
// Set line width as 2
graph.LineWidth = 2;
// Set the color for graph object
graph.Color = Aspose.Pdf.Color.Red;
// Set dash array value as 3
graph.DashArray = new int[] { 3 };
// Set dash phase value as 1
graph.DashPhase = 1;
// Set footnote line style for page as graph
page.NoteLineStyle = graph;
// Create TextFragment instance
var text = new Aspose.Pdf.Text.TextFragment("Hello World");
// Set FootNote value for TextFragment
text.FootNote = new Aspose.Pdf.Note("foot note for test text 1");
// Add TextFragment to paragraphs collection of first page of document
page.Paragraphs.Add(text);
// Create second TextFragment
text = new Aspose.Pdf.Text.TextFragment("Aspose.PDF for .NET");
// Set FootNote for second text fragment
text.FootNote = new Aspose.Pdf.Note("foot note for test text 2");
// Add second text fragment to paragraphs collection of PDF file
page.Paragraphs.Add(text);
// Save PDF document
document.Save(dataDir + "CustomLineStyleForFootNote_out.pdf");
}
}
我们可以使用 TextState 对象设置脚注标签(注释标识符)格式,如下所示:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void FormattingUsingTextStateObject()
{
var text = new Aspose.Pdf.Text.TextFragment("test text 1");
text.FootNote = new Aspose.Pdf.Note("foot note for test text 1");
text.FootNote.Text = "21";
text.FootNote.TextState = new Aspose.Pdf.Text.TextState();
text.FootNote.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
text.FootNote.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic;
}
默认情况下,脚注编号从 1 开始递增。然而,我们可能需要设置自定义脚注标签。为了实现此要求,请尝试使用以下代码片段。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CustomizeFootNoteLabel()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Create GraphInfo object
var graph = new Aspose.Pdf.GraphInfo();
// Set line width as 2
graph.LineWidth = 2;
// Set the color for graph object
graph.Color = Aspose.Pdf.Color.Red;
// Set dash array value as 3
graph.DashArray = new int[] { 3 };
// Set dash phase value as 1
graph.DashPhase = 1;
// Set footnote line style for page as graph
page.NoteLineStyle = graph;
// Create TextFragment instance
var text = new Aspose.Pdf.Text.TextFragment("Hello World");
// Set FootNote value for TextFragment
text.FootNote = new Aspose.Pdf.Note("foot note for test text 1");
// Specify custom label for FootNote
text.FootNote.Text = " Aspose(2015)";
// Add TextFragment to paragraphs collection of first page of document
page.Paragraphs.Add(text);
// Save PDF document
document.Save(dataDir + "CustomizeFootNoteLabel_out.pdf");
}
}
在早期版本中,脚注支持仅适用于 TextFragment 对象。然而,从 Aspose.PDF for .NET 10.7.0 版本开始,您还可以向 PDF 文档中的其他对象(如表格、单元格等)添加脚注。以下代码片段展示了向 TextFragment 对象添加脚注的步骤,然后将图像和表格对象添加到脚注部分的段落集合中。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImageAndTable()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
var page = document.Pages.Add();
var text = new Aspose.Pdf.Text.TextFragment("some text");
page.Paragraphs.Add(text);
text.FootNote = new Aspose.Pdf.Note();
// Create image
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = dataDir + "aspose-logo.jpg";
image.FixHeight = 20;
text.FootNote.Paragraphs.Add(image);
var footNote = new Aspose.Pdf.Text.TextFragment("footnote text");
footNote.TextState.FontSize = 20;
footNote.IsInLineParagraph = true;
text.FootNote.Paragraphs.Add(footNote);
var table = new Aspose.Pdf.Table();
table.Rows.Add().Cells.Add().Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Row 1 Cell 1"));
text.FootNote.Paragraphs.Add(table);
// Save PDF document
document.Save(dataDir + "AddImageAndTable_out.pdf");
}
}
尾注是一个源引用,指向论文末尾的特定位置,读者可以在该位置找到信息或引用的来源。当使用尾注时,您引用或改写的句子或总结材料后面跟着一个上标数字。
以下示例演示了如何在 PDF 页面中添加尾注。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateEndNotes()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Create TextFragment instance
var text = new Aspose.Pdf.Text.TextFragment("Hello World");
// Set FootNote value for TextFragment
text.EndNote = new Aspose.Pdf.Note("sample End note");
// Specify custom label for FootNote
text.EndNote.Text = " Aspose(2015)";
// Add TextFragment to paragraphs collection of first page of document
page.Paragraphs.Add(text);
// Save PDF document
document.Save(dataDir + "CreateEndNotes_out.pdf");
}
}
PDF 文件的默认布局是流式布局(从左上到右下)。因此,每个新添加到 PDF 文件的元素都是在底部右侧流中添加的。然而,我们可能需要在同一水平上显示各种页面元素,即图像和文本(一个接一个)。一种方法是创建一个表格实例并将两个元素添加到各自的单元格对象中。然而,另一种方法可以是行内段落。通过将图像和文本的 IsInLineParagraph 属性设置为 true,这些段落将作为其他页面元素的行内出现。
以下代码片段展示了如何在 PDF 文件中将文本和图像添加为行内段落。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void TextAndImageAsParagraph()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of Document instance
var page = document.Pages.Add();
// Create TextFragmnet
var text = new Aspose.Pdf.Text.TextFragment("Hello World.. ");
// Add text fragment to paragraphs collection of Page object
page.Paragraphs.Add(text);
// Create an image instance
var image = new Aspose.Pdf.Image();
// Set image as inline paragraph so that it appears right after
// The previous paragraph object (TextFragment)
image.IsInLineParagraph = true;
// Specify image file path
image.File = dataDir + "aspose-logo.jpg";
// Set image Height (optional)
image.FixHeight = 30;
// Set Image Width (optional)
image.FixWidth = 100;
// Add image to paragraphs collection of page object
page.Paragraphs.Add(image);
// Re-initialize TextFragment object with different contents
text = new Aspose.Pdf.Text.TextFragment(" Hello Again..");
// Set TextFragment as inline paragraph
text.IsInLineParagraph = true;
// Add newly created TextFragment to paragraphs collection of page
page.Paragraphs.Add(text);
// Save PDF document
document.Save(dataDir + "TextAndImageAsParagraph_out.pdf");
}
}
可以使用 TextFragment 实例或 TextParagraph 对象在 PDF 文件的段落集合中添加文本,甚至可以使用 TextStamp 类在 PDF 中盖章文本。在添加文本时,我们可能需要为文本对象指定字符间距。为了实现此要求,引入了一个名为 CharacterSpacing 的新属性。请查看以下方法以满足此要求。
以下方法展示了在 PDF 文档中添加文本时指定字符间距的步骤。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CharacterSpacingUsingTextBuilderAndFragment()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of Document
document.Pages.Add();
// Create TextBuilder instance
var builder = new Aspose.Pdf.Text.TextBuilder(document.Pages[1]);
// Create text fragment instance with sample contents
var wideFragment = new Aspose.Pdf.Text.TextFragment("Text with increased character spacing");
wideFragment.TextState.ApplyChangesFrom(new Aspose.Pdf.Text.TextState("Arial", 12));
// Specify character spacing for TextFragment
wideFragment.TextState.CharacterSpacing = 2.0f;
// Specify the position of TextFragment
wideFragment.Position = new Aspose.Pdf.Text.Position(100, 650);
// Append TextFragment to TextBuilder instance
builder.AppendText(wideFragment);
// Save PDF document
document.Save(dataDir + "CharacterSpacingUsingTextBuilderAndFragment_out.pdf");
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CharacterSpacingUsingTextBuilderAndParagraph()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of Document
var page = document.Pages.Add();
// Create TextBuilder instance
var builder = new Aspose.Pdf.Text.TextBuilder(page);
// Instantiate TextParagraph instance
var paragraph = new Aspose.Pdf.Text.TextParagraph();
// Create TextState instance to specify font name and size
var state = new Aspose.Pdf.Text.TextState("Arial", 12);
// Specify the character spacing
state.CharacterSpacing = 1.5f;
// Append text to TextParagraph object
paragraph.AppendLine("This is paragraph with character spacing", state);
// Specify the position for TextParagraph
paragraph.Position = new Aspose.Pdf.Text.Position(100, 550);
// Append TextParagraph to TextBuilder instance
builder.AppendParagraph(paragraph);
// Save PDF document
document.Save(dataDir + "CharacterSpacingUsingTextBuilderAndParagraph_out.pdf");
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CharacterSpacingUsingTextStamp()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page to pages collection of Document
var page = document.Pages.Add();
// Instantiate TextStamp instance with sample text
var stamp = new Aspose.Pdf.TextStamp("This is text stamp with character spacing");
// Specify font name for Stamp object
stamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
// Specify Font size for TextStamp
stamp.TextState.FontSize = 12;
// Specify character specing as 1f
stamp.TextState.CharacterSpacing = 1f;
// Set the XIndent for Stamp
stamp.XIndent = 100;
// Set the YIndent for Stamp
stamp.YIndent = 500;
// Add textual stamp to page instance
stamp.Put(page);
// Save PDF document
document.Save(dataDir + "CharacterSpacingUsingTextStamp_out.pdf");
}
}
在杂志和报纸中,我们通常看到新闻在单页上以多列显示,而不是在书籍中,文本段落通常从左到右打印在整页上。许多文档处理应用程序,如 Microsoft Word 和 Adobe Acrobat Writer,允许用户在单页上创建多列,然后向其中添加数据。
Aspose.PDF for .NET 还提供了在 PDF 文档的页面内创建多列的功能。为了创建多列 PDF 文件,我们可以利用 Aspose.Pdf.FloatingBox 类,因为它提供了 ColumnInfo.ColumnCount 属性来指定 FloatingBox 内的列数,我们还可以使用 ColumnInfo.ColumnSpacing 和 ColumnInfo.ColumnWidths 属性相应地指定列之间的间距和列宽度。请注意,FloatingBox 是文档对象模型中的一个元素,与相对定位(即文本、图形、图像等)相比,它可能具有过时的定位。
列间距是列之间的空间,默认的列间距为 1.25 厘米。如果未指定列宽,则 Aspose.PDF for .NET 会根据页面大小和列间距自动计算每列的宽度。
下面的示例演示了如何创建两个带有图形对象(线)的列,并将它们添加到 FloatingBox 的段落集合中,然后将其添加到页面实例的段落集合中。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateMultiColumnPdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Specify the left margin info for the PDF file
document.PageInfo.Margin.Left = 40;
// Specify the Right margin info for the PDF file
document.PageInfo.Margin.Right = 40;
var page = document.Pages.Add();
var graph1 = new Aspose.Pdf.Drawing.Graph(500, 2);
// Add the line to paraphraphs collection of section object
page.Paragraphs.Add(graph1);
// Specify the coordinates for the line
float[] posArr = new float[] { 1, 2, 500, 2 };
var line1 = new Aspose.Pdf.Drawing.Line(posArr);
graph1.Shapes.Add(line1);
// Create string variables with text containing html tags
string s = "<font face=\"Times New Roman\" size=4>" +
"<strong> How to Steer Clear of money scams</<strong> "
+ "</font>";
// Create text paragraphs containing HTML text
var heading_text = new Aspose.Pdf.HtmlFragment(s);
page.Paragraphs.Add(heading_text);
var box = new Aspose.Pdf.FloatingBox();
// Add four columns in the section
box.ColumnInfo.ColumnCount = 2;
// Set the spacing between the columns
box.ColumnInfo.ColumnSpacing = "5";
box.ColumnInfo.ColumnWidths = "105 105";
var text1 = new Aspose.Pdf.Text.TextFragment("By A Googler (The Official Google Blog)");
text1.TextState.FontSize = 8;
text1.TextState.LineSpacing = 2;
box.Paragraphs.Add(text1);
text1.TextState.FontSize = 10;
text1.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic;
// Create a graphs object to draw a line
var graph2 = new Aspose.Pdf.Drawing.Graph(50, 10);
// Specify the coordinates for the line
float[] posArr2 = new float[] { 1, 10, 100, 10 };
var line2 = new Aspose.Pdf.Drawing.Line(posArr2);
graph2.Shapes.Add(line2);
// Add the line to paragraphs collection of section object
box.Paragraphs.Add(graph2);
var text2 = new Aspose.Pdf.Text.TextFragment(@"Sed augue tortor, sodales id, luctus et, pulvinar ut, eros. Suspendisse vel dolor. Sed quam. Curabitur ut massa vitae eros euismod aliquam. Pellentesque sit amet elit. Vestibulum interdum pellentesque augue. Cras mollis arcu sit amet purus. Donec augue. Nam mollis tortor a elit. Nulla viverra nisl vel mauris. Vivamus sapien. nascetur ridiculus mus. Nam justo lorem, aliquam luctus, sodales et, semper sed, enim Nam justo lorem, aliquam luctus, sodales et,nAenean posuere ante ut neque. Morbi sollicitudin congue felis. Praesent turpis diam, iaculis sed, pharetra non, mollis ac, mauris. Phasellus nisi ipsum, pretium vitae, tempor sed, molestie eu, dui. Duis lacus purus, tristique ut, iaculis cursus, tincidunt vitae, risus. Sed commodo. *** sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam justo lorem, aliquam luctus, sodales et, semper sed, enim Nam justo lorem, aliquam luctus, sodales et, semper sed, enim Nam justo lorem, aliquam luctus, sodales et, semper sed, enim nAenean posuere ante ut neque. Morbi sollicitudin congue felis. Praesent turpis diam, iaculis sed, pharetra non, mollis ac, mauris. Phasellus nisi ipsum, pretium vitae, tempor sed, molestie eu, dui. Duis lacus purus, tristique ut, iaculis cursus, tincidunt vitae, risus. Sed commodo. *** sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed urna. . Duis convallis ultrices nisi. Maecenas non ligula. Nunc nibh est, tincidunt in, placerat sit amet, vestibulum a, nulla. Praesent porttitor turpis eleifend ante. Morbi sodales.nAenean posuere ante ut neque. Morbi sollicitudin congue felis. Praesent turpis diam, iaculis sed, pharetra non, mollis ac, mauris. Phasellus nisi ipsum, pretium vitae, tempor sed, molestie eu, dui. Duis lacus purus, tristique ut, iaculis cursus, tincidunt vitae, risus. Sed commodo. *** sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed urna. . Duis convallis ultrices nisi. Maecenas non ligula. Nunc nibh est, tincidunt in, placerat sit amet, vestibulum a, nulla. Praesent porttitor turpis eleifend ante. Morbi sodales.");
box.Paragraphs.Add(text2);
page.Paragraphs.Add(box);
// Save PDF document
document.Save(dataDir + "CreateMultiColumnPdf_out.pdf");
}
}
制表符停止是制表的停止点。在文字处理过程中,每行包含若干个以规则间隔放置的制表符停止(例如,每半英寸)。然而,它们可以被更改,因为大多数文字处理器允许您在任何位置设置制表符停止。当您按下 Tab 键时,光标或插入点跳到下一个制表符停止,而制表符停止本身是不可见的。尽管制表符停止在文本文件中不存在,但文字处理器会跟踪它们,以便能够正确响应 Tab 键。
Aspose.PDF for .NET 允许开发人员在 PDF 文档中使用自定义制表符停止。Aspose.Pdf.Text.TabStop 类用于在 TextFragment 类中设置自定义制表符停止。
Aspose.PDF for .NET 还提供了一些预定义的制表符引导类型,作为名为 TabLeaderType 的枚举,其预定义值及其描述如下:
制表符引导类型 | 描述 |
---|---|
无 | 没有制表符引导 |
实心 | 实心制表符引导 |
破折号 | 破折号制表符引导 |
点 | 点制表符引导 |
以下是如何设置自定义制表符停止的示例。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CustomTabStops()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
var page = document.Pages.Add();
var tabStops = new Aspose.Pdf.Text.TabStops();
var tabStop1 = tabStops.Add(100);
tabStop1.AlignmentType = Aspose.Pdf.Text.TabAlignmentType.Right;
tabStop1.LeaderType = Aspose.Pdf.Text.TabLeaderType.Solid;
var tabStop2 = tabStops.Add(200);
tabStop2.AlignmentType = Aspose.Pdf.Text.TabAlignmentType.Center;
tabStop2.LeaderType = Aspose.Pdf.Text.TabLeaderType.Dash;
var tabStop3 = tabStops.Add(300);
tabStop3.AlignmentType = Aspose.Pdf.Text.TabAlignmentType.Left;
tabStop3.LeaderType = Aspose.Pdf.Text.TabLeaderType.Dot;
var header = new Aspose.Pdf.Text.TextFragment("This is a example of forming table with TAB stops", tabStops);
var text0 = new Aspose.Pdf.Text.TextFragment("#$TABHead1 #$TABHead2 #$TABHead3", tabStops);
var text1 = new Aspose.Pdf.Text.TextFragment("#$TABdata11 #$TABdata12 #$TABdata13", tabStops);
var text2 = new Aspose.Pdf.Text.TextFragment("#$TABdata21 ", tabStops);
text2.Segments.Add(new Aspose.Pdf.Text.TextSegment("#$TAB"));
text2.Segments.Add(new Aspose.Pdf.Text.TextSegment("data22 "));
text2.Segments.Add(new Aspose.Pdf.Text.TextSegment("#$TAB"));
text2.Segments.Add(new Aspose.Pdf.Text.TextSegment("data23"));
// Add text fragments to page
page.Paragraphs.Add(header);
page.Paragraphs.Add(text0);
page.Paragraphs.Add(text1);
page.Paragraphs.Add(text2);
// Save PDF document
document.Save(dataDir + "CustomTabStops_out.pdf");
}
}
PDF 文件包含图像、文本、图形、附件、注释对象,在创建 TextFragment 时,您可以设置前景、背景颜色信息以及文本格式。Aspose.PDF for .NET 支持添加带有 Alpha 颜色通道的文本的功能。以下代码片段展示了如何添加带有透明颜色的文本。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTransparentText()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Create page to pages collection of PDF file
var page = document.Pages.Add();
// Create Graph object
var canvas = new Aspose.Pdf.Drawing.Graph(100, 400);
// Create rectangle instance with certain dimensions
var rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 400, 400);
// Create color object from Alpha color channel
rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183)));
// Add rectanlge to shapes collection of Graph object
canvas.Shapes.Add(rect);
// Add graph object to paragraphs collection of page object
page.Paragraphs.Add(canvas);
// Set value to not change position for graph object
canvas.IsChangePosition = false;
// Create TextFragment instance with sample value
var text = new Aspose.Pdf.Text.TextFragment("transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text ");
// Create color object from Alpha channel
Aspose.Pdf.Color color = Aspose.Pdf.Color.FromArgb(30, 0, 255, 0);
// Set color information for text instance
text.TextState.ForegroundColor = color;
// Add text to paragraphs collection of page instance
page.Paragraphs.Add(text);
// Save PDF document
document.Save(dataDir + "AddTransparentText_out.pdf");
}
}
每种字体都有一个抽象的正方形,其高度是同一字号的行间距。这个正方形称为 em 正方形,它是定义字形轮廓的设计网格。许多输入字体的字母有些点位于字体的 em 正方形边界之外,因此为了正确显示字体,需要使用特殊设置。TextFragment 对象具有一组可通过属性 TextState.FormattingOptions 访问的文本格式选项。此路径的最后一个属性是类型为 Aspose.Pdf.Text.TextFormattingOptions 的属性。该类具有一个枚举 LineSpacingMode,专为特定字体设计,例如输入字体 “HPSimplified.ttf”。此外,类 Aspose.Pdf.Text.TextFormattingOptions 具有一个类型为 LineSpacingMode 的属性 LineSpacing。您只需将 LineSpacing 设置为 LineSpacingMode.FullSize。获取正确显示字体的代码片段如下:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SpecifyLineSpacing()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
string fontFile = dataDir + "HPSimplified.TTF";
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
//Create TextFormattingOptions with LineSpacingMode.FullSize
var formattingOptions = new Aspose.Pdf.Text.TextFormattingOptions();
formattingOptions.LineSpacing = Aspose.Pdf.Text.TextFormattingOptions.LineSpacingMode.FullSize;
// Create text builder object for first page of document
//TextBuilder textBuilder = new TextBuilder(document.Pages[1]);
// Create text fragment with sample string
var textFragment = new Aspose.Pdf.Text.TextFragment("Hello world");
if (fontFile != "")
{
// Load the TrueType font into stream object
using (FileStream fontStream = File.OpenRead(fontFile))
{
// Set the font name for text string
textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.OpenFont(fontStream, Aspose.Pdf.Text.FontTypes.TTF);
// Specify the position for Text Fragment
textFragment.Position = new Aspose.Pdf.Text.Position(100, 600);
//Set TextFormattingOptions of current fragment to predefined(which points to LineSpacingMode.FullSize)
textFragment.TextState.FormattingOptions = formattingOptions;
// Add the text to TextBuilder so that it can be placed over the PDF file
//textBuilder.AppendText(textFragment);
var page = document.Pages.Add();
page.Paragraphs.Add(textFragment);
}
// Save PDF document
document.Save(dataDir + "SpecifyLineSpacing_out.pdf");
}
}
}
有时,需要动态获取文本宽度。Aspose.PDF for .NET 包含两种字符串宽度测量方法。您可以调用 Aspose.Pdf.Text.Font 或 Aspose.Pdf.Text.TextState 类的 MeasureString 方法(或两者)。以下代码片段展示了如何使用此功能。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetTextWidthDynamically()
{
var font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
var textState = new Aspose.Pdf.Text.TextState();
textState.Font = font;
textState.FontSize = 14;
if (Math.Abs(font.MeasureString("A", 14) - 9.337) > 0.001)
{
Console.WriteLine("Unexpected font string measure!");
}
if (Math.Abs(textState.MeasureString("z") - 7.0) > 0.001)
{
Console.WriteLine("Unexpected font string measure!");
}
for (char c = 'A'; c <= 'z'; c++)
{
double fontMeasure = font.MeasureString(c.ToString(), 14);
double textStateMeasure = textState.MeasureString(c.ToString());
if (Math.Abs(fontMeasure - textStateMeasure) > 0.001)
{
Console.WriteLine("Font and state string measuring doesn't match!");
}
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.