创建复杂的PDF

Contents
[ ]

Hello, World 示例展示了使用 C# 和 Aspose.PDF 创建 PDF 文档的简单步骤。在本文中,我们将看看如何使用 C# 和 Aspose.PDF for .NET 创建更复杂的文档。作为示例,我们将使用一家虚构公司的文档,该公司经营客运渡轮服务。 我们的文档将包含一张图像、两个文本片段(标题和段落)以及一个表格。为了构建这样的文档,我们将使用基于DOM的方法。您可以在 DOM API 基础知识 部分中阅读更多内容。

如果我们从头开始创建文档,我们需要遵循以下步骤:

  1. 实例化一个 Document 对象。在这一步中,我们将创建一个带有一些元数据但没有页面的空 PDF 文档。
  2. 向文档对象添加一个 Page。现在我们的文档将有一页。
  3. 向页面添加一个 Image
  4. 为标题创建一个 TextFragment。对于标题,我们将使用 Arial 字体,字体大小为 24pt,居中对齐。
  5. 将标题添加到页面的 Paragraphs
  6. 为描述创建一个 TextFragment。对于描述,我们将使用 Arial 字体,字体大小为 24pt,居中对齐。
  7. 将(描述)添加到页面的段落中。
  8. 创建一个表格,添加表格属性。
  9. 将(表格)添加到页面的 Paragraphs
  10. 保存文档为 “Complex.pdf”。

以下代码片段也适用于 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");
    }
}