2021-08-26 22:25:33 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using QuestPDF.Examples.Engine;
|
|
|
|
|
using QuestPDF.Fluent;
|
|
|
|
|
using QuestPDF.Helpers;
|
|
|
|
|
using QuestPDF.Infrastructure;
|
|
|
|
|
|
|
|
|
|
namespace QuestPDF.Examples
|
|
|
|
|
{
|
|
|
|
|
public class TextBenchmark
|
2021-08-27 01:10:30 +02:00
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void Generate()
|
|
|
|
|
{
|
|
|
|
|
var chapters = GetBookChapters().ToList();
|
|
|
|
|
|
|
|
|
|
RenderingTest
|
|
|
|
|
.Create()
|
|
|
|
|
.PageSize(PageSizes.A4)
|
|
|
|
|
.ProducePdf()
|
|
|
|
|
.ShowResults()
|
2022-03-12 12:17:00 +01:00
|
|
|
.RenderDocument(x => ComposeBook(x, chapters));
|
2021-08-27 01:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-26 22:25:33 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Benchmark()
|
|
|
|
|
{
|
2021-08-27 01:10:30 +02:00
|
|
|
var chapters = GetBookChapters().ToList();
|
2021-08-27 00:29:04 +02:00
|
|
|
|
2022-05-22 20:25:11 +02:00
|
|
|
var results = PerformTest(128).ToList();
|
2021-08-27 00:29:04 +02:00
|
|
|
|
2021-08-26 22:25:33 +02:00
|
|
|
Console.WriteLine($"Min: {results.Min():F}");
|
|
|
|
|
Console.WriteLine($"Max: {results.Max():F}");
|
|
|
|
|
Console.WriteLine($"Avg: {results.Average():F}");
|
|
|
|
|
|
|
|
|
|
void GenerateDocument()
|
|
|
|
|
{
|
|
|
|
|
RenderingTest
|
|
|
|
|
.Create()
|
|
|
|
|
.PageSize(PageSizes.A4)
|
2022-03-12 12:17:00 +01:00
|
|
|
.ProducePdf()
|
|
|
|
|
.RenderDocument(x => ComposeBook(x, chapters));
|
2021-08-26 22:25:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerable<float> PerformTest(int attempts)
|
|
|
|
|
{
|
|
|
|
|
foreach (var i in Enumerable.Range(0, attempts))
|
|
|
|
|
{
|
|
|
|
|
var timer = new Stopwatch();
|
|
|
|
|
|
|
|
|
|
timer.Start();
|
|
|
|
|
GenerateDocument();
|
|
|
|
|
timer.Stop();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"Attempt {i}: {timer.ElapsedMilliseconds:F}");
|
|
|
|
|
yield return timer.ElapsedMilliseconds;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-27 01:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class BookChapter
|
|
|
|
|
{
|
|
|
|
|
public string Title { get; set; }
|
|
|
|
|
public string Content { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<BookChapter> GetBookChapters()
|
|
|
|
|
{
|
|
|
|
|
var book = File.ReadAllLines("quo-vadis.txt");
|
|
|
|
|
|
|
|
|
|
var chapterPointers = book
|
|
|
|
|
.Select((line, index) => new
|
|
|
|
|
{
|
|
|
|
|
LineNumber = index,
|
|
|
|
|
Text = line
|
|
|
|
|
})
|
|
|
|
|
.Where(x => x.Text.Length < 50 && x.Text.Contains("Rozdział") || x.Text.Contains("-----"))
|
|
|
|
|
.Select(x => x.LineNumber)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var index in Enumerable.Range(0, chapterPointers.Count - 1))
|
|
|
|
|
{
|
|
|
|
|
var chapter = chapterPointers[index];
|
|
|
|
|
|
|
|
|
|
var title = book[chapter];
|
|
|
|
|
|
|
|
|
|
var lineFrom = chapterPointers[index];
|
|
|
|
|
var lineTo = chapterPointers[index + 1] - 1;
|
2021-09-12 21:01:10 +02:00
|
|
|
|
|
|
|
|
var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom).Where(x => !string.IsNullOrWhiteSpace(x));
|
2021-08-27 01:10:30 +02:00
|
|
|
var content = string.Join(Environment.NewLine, lines);
|
|
|
|
|
|
|
|
|
|
yield return new BookChapter
|
|
|
|
|
{
|
|
|
|
|
Title = title,
|
|
|
|
|
Content = content
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 12:17:00 +01:00
|
|
|
private void ComposeBook(IDocumentContainer container, ICollection<BookChapter> chapters)
|
2021-08-27 01:10:30 +02:00
|
|
|
{
|
|
|
|
|
var subtitleStyle = TextStyle.Default.Size(24).SemiBold().Color(Colors.Blue.Medium);
|
|
|
|
|
var normalStyle = TextStyle.Default.Size(14);
|
2021-08-26 22:25:33 +02:00
|
|
|
|
2022-03-12 12:17:00 +01:00
|
|
|
container.Page(page =>
|
2021-08-26 22:25:33 +02:00
|
|
|
{
|
2022-03-12 12:17:00 +01:00
|
|
|
page.Margin(50);
|
|
|
|
|
|
2022-09-15 22:19:28 +02:00
|
|
|
page.Content().PaddingVertical(10).Column(column =>
|
2022-03-12 12:17:00 +01:00
|
|
|
{
|
|
|
|
|
column.Item().Element(Title);
|
|
|
|
|
column.Item().PageBreak();
|
|
|
|
|
column.Item().Element(TableOfContents);
|
|
|
|
|
column.Item().PageBreak();
|
2021-08-26 22:25:33 +02:00
|
|
|
|
2022-03-12 12:17:00 +01:00
|
|
|
Chapters(column);
|
2021-08-27 01:38:26 +02:00
|
|
|
|
2022-03-12 12:17:00 +01:00
|
|
|
column.Item().Element(Acknowledgements);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
page.Footer().Element(Footer);
|
|
|
|
|
});
|
2021-08-26 22:25:33 +02:00
|
|
|
|
|
|
|
|
void Title(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container
|
|
|
|
|
.Extend()
|
|
|
|
|
.PaddingBottom(200)
|
|
|
|
|
.AlignBottom()
|
2022-01-09 00:11:34 +01:00
|
|
|
.Column(column =>
|
2021-08-26 22:25:33 +02:00
|
|
|
{
|
2022-03-11 13:23:36 +01:00
|
|
|
column.Item().Text("Quo Vadis").FontSize(72).Bold().FontColor(Colors.Blue.Darken2);
|
|
|
|
|
column.Item().Text("Henryk Sienkiewicz").FontSize(24).FontColor(Colors.Grey.Darken2);
|
2021-08-26 22:25:33 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TableOfContents(IContainer container)
|
|
|
|
|
{
|
2022-01-09 00:11:34 +01:00
|
|
|
container.Column(column =>
|
2021-08-26 22:25:33 +02:00
|
|
|
{
|
2022-01-09 00:11:34 +01:00
|
|
|
SectionTitle(column, "Spis treści");
|
2021-08-26 22:25:33 +02:00
|
|
|
|
|
|
|
|
foreach (var chapter in chapters)
|
|
|
|
|
{
|
2022-01-09 00:11:34 +01:00
|
|
|
column.Item().InternalLink(chapter.Title).Row(row =>
|
2021-08-26 22:25:33 +02:00
|
|
|
{
|
2022-03-08 17:27:30 +01:00
|
|
|
row.RelativeItem().Text(chapter.Title).Style(normalStyle);
|
|
|
|
|
row.ConstantItem(100).AlignRight().Text(text => text.BeginPageNumberOfSection(chapter.Title).Style(normalStyle));
|
2021-08-26 22:25:33 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 00:11:34 +01:00
|
|
|
void Chapters(ColumnDescriptor column)
|
2021-08-26 22:25:33 +02:00
|
|
|
{
|
|
|
|
|
foreach (var chapter in chapters)
|
|
|
|
|
{
|
2022-01-09 00:11:34 +01:00
|
|
|
column.Item().Element(container => Chapter(container, chapter.Title, chapter.Content));
|
2021-08-26 22:25:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Chapter(IContainer container, string title, string content)
|
|
|
|
|
{
|
2022-01-09 00:11:34 +01:00
|
|
|
container.Column(column =>
|
2021-08-26 22:25:33 +02:00
|
|
|
{
|
2022-01-09 00:11:34 +01:00
|
|
|
SectionTitle(column, title);
|
2021-08-26 22:25:33 +02:00
|
|
|
|
2022-01-09 00:11:34 +01:00
|
|
|
column.Item().Text(text =>
|
2021-08-26 22:25:33 +02:00
|
|
|
{
|
2021-08-29 19:18:58 +02:00
|
|
|
text.ParagraphSpacing(5);
|
2022-03-08 17:27:30 +01:00
|
|
|
text.Span(content).Style(normalStyle);
|
2021-08-26 22:25:33 +02:00
|
|
|
});
|
|
|
|
|
|
2022-01-09 00:11:34 +01:00
|
|
|
column.Item().PageBreak();
|
2021-08-26 22:25:33 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 19:51:28 +02:00
|
|
|
void Acknowledgements(IContainer container)
|
2021-08-27 01:38:26 +02:00
|
|
|
{
|
2022-01-09 00:11:34 +01:00
|
|
|
container.Column(column =>
|
2021-08-27 01:38:26 +02:00
|
|
|
{
|
2022-01-09 00:11:34 +01:00
|
|
|
SectionTitle(column, "Podziękowania");
|
2021-08-27 01:38:26 +02:00
|
|
|
|
2022-01-09 00:11:34 +01:00
|
|
|
column.Item().Text(text =>
|
2021-08-27 01:38:26 +02:00
|
|
|
{
|
|
|
|
|
text.DefaultTextStyle(normalStyle);
|
|
|
|
|
|
2021-08-27 19:51:28 +02:00
|
|
|
text.Span("Ten dokument został wygenerowany na podstawie książki w formacie TXT opublikowanej w serwisie ");
|
2022-03-11 13:23:36 +01:00
|
|
|
text.Hyperlink("wolnelektury.pl", "https://wolnelektury.pl/").FontColor(Colors.Blue.Medium).Underline();
|
2021-08-27 19:51:28 +02:00
|
|
|
text.Span(". Dziękuję za wspieranie polskiego czytelnictwa!");
|
2021-08-27 01:38:26 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 00:11:34 +01:00
|
|
|
void SectionTitle(ColumnDescriptor column, string text)
|
2021-08-27 01:38:26 +02:00
|
|
|
{
|
2022-03-08 17:27:30 +01:00
|
|
|
column.Item().Location(text).Text(text).Style(subtitleStyle);
|
2022-01-09 00:11:34 +01:00
|
|
|
column.Item().PaddingTop(10).PaddingBottom(50).BorderBottom(1).BorderColor(Colors.Grey.Lighten2).ExtendHorizontal();
|
2021-08-27 01:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-26 22:25:33 +02:00
|
|
|
void Footer(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container
|
|
|
|
|
.AlignCenter()
|
|
|
|
|
.Text(text =>
|
|
|
|
|
{
|
|
|
|
|
text.CurrentPageNumber();
|
|
|
|
|
text.Span(" / ");
|
|
|
|
|
text.TotalPages();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|