إنشاء PDF معقد

Contents
[ ]

مثال Hello, World أظهر خطوات بسيطة لإنشاء مستند PDF باستخدام C# وAspose.PDF. في هذه المقالة، سنلقي نظرة على إنشاء مستند أكثر تعقيدًا باستخدام C# وAspose.PDF for .NET. كمثال، سنأخذ مستندًا من شركة وهمية تعمل في خدمات عبّارات الركاب. سيحتوي مستندنا على صورة، وقطعتين نصيتين (عنوان وفقرة)، وجدول. لبناء مثل هذا المستند، سنستخدم نهج قائم على DOM. يمكنك قراءة المزيد في قسم أساسيات واجهة برمجة تطبيقات DOM.

إذا قمنا بإنشاء مستند من الصفر، نحتاج إلى اتباع خطوات معينة:

  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");
    }
}