1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
using QuestPDF.Fluent; using QuestPDF.Helpers; using QuestPDF.Infrastructure; using System.Diagnostics; // File path where the PDF will be saved string filePath = Path.Combine(Environment.CurrentDirectory, "sample.pdf"); QuestPDF.Settings.License = LicenseType.Community; // Create the PDF document using QuestPDF var document = Document.Create(container => { container.Page(page => { // Page Setup page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.PageColor(Colors.White); page.DefaultTextStyle(x => x.FontSize(20)); // Page Content page.Header() .Text("Sample PDF with QuestPDF") .SemiBold().FontSize(36).FontColor(Colors.Blue.Medium); page.Content().Column(col => { col.Spacing(10); // Add some text col.Item().Text("This is a sample PDF created with QuestPDF.") .FontSize(24).FontColor(Colors.Black); byte[] imageBytes = File.ReadAllBytes("images/1.png"); col.Item().Image(imageBytes); // Add more text col.Item().Text("Here is an image displayed above.") .FontSize(20).FontColor(Colors.Grey.Medium); // Add more structured content col.Item().Text(text => { text.Span("QuestPDF "); text.Span("is a powerful PDF generation library for .NET developers.") .Bold().FontSize(22); }); }); page.Footer() .AlignCenter() .Text(x => { x.CurrentPageNumber(); x.Span(" / "); x.TotalPages(); }); }); container.Page(page => { page.Size(PageSizes.A4); page.Margin(2, Unit.Centimetre); page.PageColor(Colors.Red.Medium); page.DefaultTextStyle(x => x.Color(Colors.White)); page.Header() .Text("Second Page of PDF") .SemiBold().FontSize(36).FontColor(Colors.Blue.Medium); page.Content().Column(col => { byte[] imageBytes = File.ReadAllBytes("images/2.jpg"); col.Item().Image(imageBytes); }); page.Footer() .AlignCenter() .Text(x => { x.CurrentPageNumber(); x.Span(" / "); x.TotalPages(); }); }); }); // Save the document as a PDF file document.GeneratePdf(filePath); Process.Start("explorer.exe", $"/select,\"{filePath}\""); Console.WriteLine($"PDF saved to {filePath}"); |