Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Hello, World 示例展示了使用 C# 和 Aspose.PDF 创建 PDF 文档的简单步骤。在本文中,我们将看看如何使用 C# 和 Aspose.PDF for .NET 创建更复杂的文档。作为示例,我们将使用一家虚构公司的文档,该公司经营客运渡轮服务。 我们的文档将包含一张图像、两个文本片段(标题和段落)以及一个表格。为了构建这样的文档,我们将使用基于DOM的方法。您可以在 DOM API 基础知识 部分中阅读更多内容。
如果我们从头开始创建文档,我们需要遵循以下步骤:
以下代码片段也适用于 Aspose.PDF.Drawing 库。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreatingComplexPdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Add image
page.AddImage(dataDir + "logo.png", new Aspose.Pdf.Rectangle(20, 730, 120, 830));
// Add Header
var header = new Aspose.Pdf.Text.TextFragment("New ferry routes in Fall 2020");
header.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
header.TextState.FontSize = 24;
header.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
header.Position = new Aspose.Pdf.Text.Position(130, 720);
page.Paragraphs.Add(header);
// Add description
var descriptionText = "Visitors must buy tickets online and tickets are limited to 5,000 per day. Ferry service is operating at half capacity and on a reduced schedule. Expect lineups.";
var description = new Aspose.Pdf.Text.TextFragment(descriptionText);
description.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Times New Roman");
description.TextState.FontSize = 14;
description.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
page.Paragraphs.Add(description);
// Add table
var table = new Aspose.Pdf.Table
{
ColumnWidths = "200",
Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Box, 1f, Aspose.Pdf.Color.DarkSlateGray),
DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Box, 0.5f, Aspose.Pdf.Color.Black),
DefaultCellPadding = new Aspose.Pdf.MarginInfo(4.5, 4.5, 4.5, 4.5),
Margin =
{
Bottom = 10
},
DefaultCellTextState =
{
Font = Aspose.Pdf.Text.FontRepository.FindFont("Helvetica")
}
};
var headerRow = table.Rows.Add();
headerRow.Cells.Add("Departs City");
headerRow.Cells.Add("Departs Island");
foreach (Aspose.Pdf.Cell headerRowCell in headerRow.Cells)
{
headerRowCell.BackgroundColor = Aspose.Pdf.Color.Gray;
headerRowCell.DefaultCellTextState.ForegroundColor = Aspose.Pdf.Color.WhiteSmoke;
}
var time = new TimeSpan(6, 0, 0);
var incTime = new TimeSpan(0, 30, 0);
for (int i = 0; i < 10; i++)
{
var dataRow = table.Rows.Add();
dataRow.Cells.Add(time.ToString(@"hh\:mm"));
time = time.Add(incTime);
dataRow.Cells.Add(time.ToString(@"hh\:mm"));
}
page.Paragraphs.Add(table);
// Save PDF document
document.Save(dataDir + "Complex_out.pdf");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.