複雑なPDFの作成

Contents
[ ]

こんにちは、世界の例では、C#とAspose.PDFを使用してPDFドキュメントを作成するための簡単な手順を示しました。この記事では、C#とAspose.PDF for .NETを使用して、より複雑なドキュメントを作成する方法を見ていきます。例として、旅客フェリーサービスを運営する架空の会社のドキュメントを取り上げます。 私たちのドキュメントには、画像、2つのテキストフラグメント(ヘッダーと段落)、およびテーブルが含まれます。このようなドキュメントを構築するために、DOMベースのアプローチを使用します。詳細については、DOM APIの基本のセクションをお読みください。

ゼロからドキュメントを作成する場合、特定の手順に従う必要があります:

  1. Documentオブジェクトをインスタンス化します。このステップでは、メタデータはあるがページがない空のPDFドキュメントを作成します。
  2. ドキュメントオブジェクトにPageを追加します。これで、ドキュメントには1ページが追加されます。
  3. ページにImageを追加します。
  4. ヘッダー用のTextFragmentを作成します。ヘッダーには、フォントサイズ24ptのArialフォントと中央揃えを使用します。
  5. ヘッダーをページのParagraphsに追加します。
  6. 説明用のTextFragmentを作成します。説明には、フォントサイズ24ptのArialフォントと中央揃えを使用します。
  7. (説明)をページのParagraphsに追加します。
  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");
    }
}