Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
O exemplo Hello, World mostrou passos simples para criar um documento PDF usando C# e Aspose.PDF. Neste artigo, vamos dar uma olhada em como criar um documento mais complexo com C# e Aspose.PDF for .NET. Como exemplo, pegaremos um documento de uma empresa fictícia que opera serviços de ferry para passageiros. Nosso documento conterá uma imagem, dois fragmentos de texto (cabeçalho e parágrafo) e uma tabela. Para construir tal documento, usaremos uma abordagem baseada em DOM. Você pode ler mais na seção Fundamentos da API DOM.
Se criarmos um documento do zero, precisamos seguir certas etapas:
O seguinte trecho de código também funciona com a biblioteca 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.