| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- using System;
- using System.Linq;
- using System.Text;
- using NUnit.Framework;
- using QuestPDF.Examples.Engine;
- using QuestPDF.Fluent;
- using QuestPDF.Helpers;
- using QuestPDF.Infrastructure;
- namespace QuestPDF.Examples
- {
- public class TextExamples
- {
- [Test]
- public void SimpleText()
- {
- RenderingTest
- .Create()
- .PageSize(500, 100)
-
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(5)
- .MinimalBox()
- .Border(1)
- .Padding(10)
- .Text(Placeholders.Paragraph());
- });
- }
-
- [Test]
- public void SimpleTextBlock()
- {
- RenderingTest
- .Create()
- .PageSize(600, 300)
-
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(5)
- .MinimalBox()
- .Border(1)
- .MaxWidth(300)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default.FontSize(20));
- text.Span("This is a normal text, followed by an ");
- text.Span("underlined red text").FontColor(Colors.Red.Medium).Underline();
- text.Span(".");
- });
- });
- }
- [Test]
- public void TextWeight()
- {
- RenderingTest
- .Create()
- .PageSize(500, 500)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .MinimalBox()
- .Border(1)
- .Padding(20)
- .Text(text =>
- {
- text.DefaultTextStyle(x => x.FontFamily(Fonts.Calibri).FontSize(20));
- text.Line("Thin").Thin();
- text.Line("ExtraLight").ExtraLight();
- text.Line("Light").Light();
- text.Line("NormalWeight").NormalWeight();
- text.Line("Medium").Medium();
- text.Line("SemiBold").SemiBold();
- text.Line("Bold").Bold();
- text.Line("ExtraBold").ExtraBold();
- text.Line("Black").Black();
- text.Line("ExtraBlack").ExtraBlack();
- });
- });
- }
-
- [Test]
- public void LineHeight()
- {
- RenderingTest
- .Create()
- .PageSize(500, 700)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Column(column =>
- {
- var lineHeights = new[] { 0.8f, 1f, 1.5f };
- var paragraph = Placeholders.Paragraph();
- foreach (var lineHeight in lineHeights)
- {
- column
- .Item()
- .Border(1)
- .Padding(10)
- .Text(paragraph)
- .FontSize(16)
- .LineHeight(lineHeight);
- }
- });
- });
- }
-
- [Test]
- public void SuperscriptSubscript_Simple()
- {
- RenderingTest
- .Create()
- .PageSize(500, 500)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .MinimalBox()
- .Border(1)
- .Padding(20)
- .Text(text =>
- {
- text.DefaultTextStyle(x => x.FontSize(20));
- text.ParagraphSpacing(10);
- var highlight = TextStyle.Default.BackgroundColor(Colors.Green.Lighten3);
- text.Span("E=mc").Style(highlight);
- text.Span("2").Superscript().Style(highlight);
- text.Span(" is the equation of mass–energy equivalence.");
- text.EmptyLine();
-
- text.Span("H").Style(highlight);
- text.Span("2").Subscript().Style(highlight);
- text.Span("O").Style(highlight);
- text.Span(" is the chemical formula for water.");
- });
- });
- }
- [Test]
- public void SuperscriptSubscript_Effects()
- {
- RenderingTest
- .Create()
- .PageSize(800, 400)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(25)
- .DefaultTextStyle(x => x.FontSize(30))
- .Column(column =>
- {
- column.Spacing(25);
-
- column.Item().Text(text =>
- {
- text.DefaultTextStyle(x => x.Underline());
-
- text.Span("Underline of the superscript (E = mc");
- text.Span("2").Superscript();
- text.Span(") should be at the same height as for normal text.");
- });
-
- column.Item().Text(text =>
- {
- text.DefaultTextStyle(x => x.Underline());
-
- text.Span("Underline of the subscript(H");
- text.Span("2").Subscript();
- text.Span("O) should be slightly lower than a normal text.");
- });
-
- column.Item().Text(text =>
- {
- text.DefaultTextStyle(x => x.Strikethrough());
-
- text.Span("Strikethrough of both superscript (E=mc");
- text.Span("2").Superscript();
- text.Span(") and subscript(H");
- text.Span("2").Subscript();
- text.Span("O) should be visible in the middle of the text.");
- });
- });
- });
- }
- [Test]
- public void ParagraphSpacing()
- {
- RenderingTest
- .Create()
- .PageSize(500, 500)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(5)
- .MinimalBox()
- .Border(1)
- .Padding(10)
- .Text(text =>
- {
- text.Line(Placeholders.Paragraph());
- });
- });
- }
-
- [Test]
- public void CustomElement()
- {
- RenderingTest
- .Create()
- .PageSize(500, 200)
- .ProduceImages()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(5)
- .MinimalBox()
- .Border(1)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default.FontSize(20));
- text.Span("This is a random image aligned to the baseline: ");
-
- text.Element()
- .PaddingBottom(-6)
- .Height(24)
- .Width(48)
- .Image(Placeholders.Image);
-
- text.Span(".");
- });
- });
- }
-
- [Test]
- public void TextElements()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Padding(10)
- .MinimalBox()
- .Border(1)
- .Padding(5)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default);
- text.AlignLeft();
- text.ParagraphSpacing(10);
- text.Line(Placeholders.LoremIpsum());
- text.Span($"This is target text that should show up. {DateTime.UtcNow:T} > This is a short sentence that will be wrapped into second line hopefully, right? <").Underline();
- });
- });
- }
-
- [Test]
- public void Textcolumn()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Padding(10)
- .MinimalBox()
- .Border(1)
- .Padding(5)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default);
- text.AlignLeft();
- text.ParagraphSpacing(10);
-
- foreach (var i in Enumerable.Range(1, 100))
- text.Line($"{i}: {Placeholders.Paragraph()}");
- });
- });
- }
- [Test]
- public void SpaceIssue()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Padding(10)
- .MinimalBox()
- .Border(1)
- .Padding(5)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(x => x.Bold());
-
- text.DefaultTextStyle(TextStyle.Default);
- text.AlignLeft();
- text.ParagraphSpacing(10);
- text.Span(Placeholders.LoremIpsum());
- text.EmptyLine();
- text.Span("This text is a normal text, ");
- text.Span("this is a bold text, ").Bold();
- text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
- text.Span("and this is slightly bigger text.").FontSize(16);
- text.EmptyLine();
- text.Span("The new text element also supports injecting custom content between words: ");
- text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
- text.Span(".");
- text.EmptyLine();
- text.Span("This is page number ");
- text.CurrentPageNumber();
- text.Span(" out of ");
- text.TotalPages();
- text.EmptyLine();
- text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
- text.EmptyLine();
- text.Span(Placeholders.Paragraphs());
-
-
- text.EmptyLine();
- text.Span(Placeholders.Paragraphs()).Italic();
-
- text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
- });
- });
- }
- [Test]
- public void HugeList()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProducePdf()
- .ShowResults()
- .Render(container =>
- {
- container
- .Padding(20)
- .Padding(10)
- .MinimalBox()
- .Border(1)
- .Padding(5)
- .Padding(10)
- .Text(text =>
- {
- text.DefaultTextStyle(TextStyle.Default.FontSize(20));
- text.AlignLeft();
- text.ParagraphSpacing(10);
- text.Span("This text is a normal text, ");
- text.Span("this is a bold text, ").Bold();
- text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
- text.Span("and this is slightly bigger text.").FontSize(16);
-
- text.Span("The new text element also supports injecting custom content between words: ");
- text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
- text.Span(".");
-
- text.EmptyLine();
-
- foreach (var i in Enumerable.Range(1, 100))
- {
- text.Line($"{i}: {Placeholders.Paragraph()}");
- text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
-
- text.Span("This is page number ");
- text.CurrentPageNumber();
- text.Span(" out of ");
- text.TotalPages();
-
- text.EmptyLine();
- }
- });
- });
- }
-
- [Test]
- public void MeasureIssueWhenSpaceAtLineEnd()
- {
- // issue 135
-
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Text("This is a specially crafted sentence with a specially chosen length for demonstration of the bug that occurs ;;;;;. ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
- });
- });
- }
-
- [Test]
- public void EmptyText()
- {
- // issue 135
-
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
- });
- });
- }
-
- [Test]
- public void Whitespaces()
- {
- // issue 135
-
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Text(" x ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
- });
- });
- }
-
- [Test]
- public void DrawingNullTextShouldNotThrowException()
- {
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Column(column =>
- {
- column.Item().Text(null);
- column.Item().Text(text =>
- {
- text.Span(null);
- text.Line(null);
- text.Hyperlink(null, "http://www.questpdf.com");
- text.TotalPages().Format(x => null);
- });
- });
- });
- });
- }
-
- [Test]
- public void BreakingLongWord()
- {
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .RenderDocument(container =>
- {
- container.Page(page =>
- {
- page.Margin(50);
- page.PageColor(Colors.White);
- page.Size(PageSizes.A4);
- page.Content().Column(column =>
- {
- column.Item().Text(null);
- column.Item().Text(text =>
- {
- text.DefaultTextStyle(x => x.BackgroundColor(Colors.Red.Lighten3).FontSize(24));
-
- text.Span(" " + Placeholders.LoremIpsum());
- text.Span(" 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 ").WrapAnywhere();
- });
- });
- });
- });
- }
- }
- }
|