Files

375 lines
13 KiB
C#
Raw Permalink Normal View History

2021-09-14 00:16:16 +02:00
using System;
using System.Linq;
2021-09-14 02:02:45 +02:00
using System.Text;
2021-08-25 03:40:16 +02:00
using NUnit.Framework;
2021-08-09 22:35:39 +02:00
using QuestPDF.Examples.Engine;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
namespace QuestPDF.Examples
{
public class TextExamples
{
2021-10-01 01:44:42 +02:00
[Test]
public void SimpleTextBlock()
{
RenderingTest
.Create()
.PageSize(500, 300)
2021-10-23 02:59:47 +02:00
2021-10-01 01:44:42 +02:00
.ProduceImages()
.ShowResults()
.Render(container =>
{
container
.Padding(5)
.MinimalBox()
2021-10-01 01:44:42 +02:00
.Border(1)
.Padding(10)
.Text(text =>
{
2022-03-12 19:17:38 +01:00
text.DefaultTextStyle(TextStyle.Default.FontSize(20));
2021-10-01 01:44:42 +02:00
text.Span("This is a normal text, followed by an ");
2022-03-12 19:17:38 +01:00
text.Span("underlined red text").FontColor(Colors.Red.Medium).Underline();
2022-03-12 12:17:00 +01:00
text.Span(".");
2021-10-01 01:44:42 +02:00
});
});
}
[Test]
public void ParagraphSpacing()
{
RenderingTest
.Create()
.PageSize(500, 300)
.ProduceImages()
.ShowResults()
.Render(container =>
{
container
.Padding(5)
.MinimalBox()
2021-10-01 01:44:42 +02:00
.Border(1)
.Padding(10)
.Text(text =>
{
text.ParagraphSpacing(10);
foreach (var i in Enumerable.Range(1, 3))
{
2022-03-08 17:27:30 +01:00
text.Span($"Paragraph {i}: ").SemiBold();
2021-10-01 01:44:42 +02:00
text.Line(Placeholders.Paragraph());
}
});
});
}
[Test]
public void CustomElement()
{
RenderingTest
.Create()
.PageSize(500, 200)
.ProduceImages()
.ShowResults()
.Render(container =>
{
container
.Padding(5)
.MinimalBox()
2021-10-01 01:44:42 +02:00
.Border(1)
.Padding(10)
.Text(text =>
{
2022-03-13 22:47:06 +01:00
text.DefaultTextStyle(TextStyle.Default.FontSize(20));
2021-10-01 01:44:42 +02:00
text.Span("This is a random image aligned to the baseline: ");
text.Element()
.PaddingBottom(-6)
.Height(24)
.Width(48)
.Image(Placeholders.Image);
text.Span(".");
});
});
}
2021-08-09 22:35:39 +02:00
[Test]
public void TextElements()
{
RenderingTest
.Create()
2021-08-25 03:40:16 +02:00
.PageSize(PageSizes.A4)
2021-08-09 22:35:39 +02:00
.ProducePdf()
2021-08-27 01:55:20 +02:00
.ShowResults()
2021-08-09 22:35:39 +02:00
.Render(container =>
{
2021-08-25 03:40:16 +02:00
container
.Padding(20)
.Padding(10)
.MinimalBox()
2021-08-25 03:40:16 +02:00
.Border(1)
.Padding(5)
.Padding(10)
2021-08-25 03:40:16 +02:00
.Text(text =>
{
text.DefaultTextStyle(TextStyle.Default);
2021-09-12 21:37:46 +02:00
text.AlignLeft();
text.ParagraphSpacing(10);
2021-09-14 02:02:45 +02:00
text.Line(Placeholders.LoremIpsum());
2022-03-08 17:27:30 +01:00
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();
2021-09-14 02:02:45 +02:00
});
});
}
[Test]
public void Textcolumn()
2021-09-14 02:02:45 +02:00
{
RenderingTest
.Create()
.PageSize(PageSizes.A4)
.ProducePdf()
.ShowResults()
.Render(container =>
{
container
.Padding(20)
.Padding(10)
.MinimalBox()
2021-09-14 02:02:45 +02:00
.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()}");
});
});
}
2021-09-12 21:37:46 +02:00
[Test]
public void SpaceIssue()
{
RenderingTest
.Create()
.PageSize(PageSizes.A4)
.ProducePdf()
.ShowResults()
.Render(container =>
{
container
.Padding(20)
.Padding(10)
.MinimalBox()
2021-09-12 21:37:46 +02:00
.Border(1)
.Padding(5)
.Padding(10)
.Text(text =>
{
2022-03-08 17:27:30 +01:00
text.DefaultTextStyle(x => x.Bold());
2021-09-12 21:37:46 +02:00
text.DefaultTextStyle(TextStyle.Default);
text.AlignLeft();
text.ParagraphSpacing(10);
text.Span(Placeholders.LoremIpsum());
2021-09-13 01:06:06 +02:00
text.EmptyLine();
2021-09-12 21:37:46 +02:00
text.Span("This text is a normal text, ");
2022-03-08 17:27:30 +01:00
text.Span("this is a bold text, ").Bold();
2022-03-11 13:23:36 +01:00
text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
text.Span("and this is slightly bigger text.").FontSize(16);
2021-09-12 21:37:46 +02:00
2021-09-13 01:06:06 +02:00
text.EmptyLine();
2021-09-12 21:37:46 +02:00
text.Span("The new text element also supports injecting custom content between words: ");
2022-03-12 12:17:00 +01:00
text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
2021-09-12 21:37:46 +02:00
text.Span(".");
2021-09-13 01:06:06 +02:00
text.EmptyLine();
2021-09-12 21:37:46 +02:00
text.Span("This is page number ");
text.CurrentPageNumber();
text.Span(" out of ");
text.TotalPages();
2021-09-13 01:06:06 +02:00
text.EmptyLine();
2021-09-12 21:37:46 +02:00
2022-03-12 19:20:01 +01:00
text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
2021-09-13 01:06:06 +02:00
text.EmptyLine();
text.Span(Placeholders.Paragraphs());
2021-09-12 21:37:46 +02:00
2021-09-13 01:06:06 +02:00
text.EmptyLine();
2022-03-08 17:27:30 +01:00
text.Span(Placeholders.Paragraphs()).Italic();
2021-09-13 01:06:06 +02:00
text.Line("This is target text that does not show up. " + Placeholders.Paragraph());
2021-09-12 21:37:46 +02:00
});
});
}
2021-09-13 01:06:06 +02:00
[Test]
2021-09-13 01:06:06 +02:00
public void HugeList()
{
RenderingTest
.Create()
2021-09-13 01:06:06 +02:00
.PageSize(PageSizes.A4)
.ProducePdf()
.ShowResults()
.Render(container =>
{
container
2021-09-13 01:06:06 +02:00
.Padding(20)
.Padding(10)
.MinimalBox()
.Border(1)
2021-09-13 01:06:06 +02:00
.Padding(5)
.Padding(10)
.Text(text =>
{
2022-03-13 22:47:06 +01:00
text.DefaultTextStyle(TextStyle.Default.FontSize(20));
text.AlignLeft();
text.ParagraphSpacing(10);
2021-09-13 01:06:06 +02:00
text.Span("This text is a normal text, ");
2022-03-08 17:27:30 +01:00
text.Span("this is a bold text, ").Bold();
2022-03-11 13:23:36 +01:00
text.Span("this is a red and underlined text, ").FontColor(Colors.Red.Medium).Underline();
text.Span("and this is slightly bigger text.").FontSize(16);
2021-09-13 01:06:06 +02:00
text.Span("The new text element also supports injecting custom content between words: ");
2022-03-12 12:17:00 +01:00
text.Element().PaddingBottom(-4).Height(16).Width(32).Image(Placeholders.Image);
2021-09-13 01:06:06 +02:00
text.Span(".");
text.EmptyLine();
foreach (var i in Enumerable.Range(1, 100))
{
text.Line($"{i}: {Placeholders.Paragraph()}");
2022-03-12 19:20:01 +01:00
text.Hyperlink("Please visit QuestPDF website", "https://www.questpdf.com");
2021-09-13 01:06:06 +02:00
text.Span("This is page number ");
text.CurrentPageNumber();
text.Span(" out of ");
text.TotalPages();
2021-09-30 22:48:39 +02:00
text.EmptyLine();
2021-09-13 01:06:06 +02:00
}
2021-08-25 03:40:16 +02:00
});
2021-08-09 22:35:39 +02:00
});
}
[Test]
public void MeasureIssueWhenSpaceAtLineEnd()
{
// issue 135
RenderingTest
.Create()
.ProduceImages()
.ShowResults()
.RenderDocument(container =>
{
container.Page(page =>
{
page.Margin(50);
2022-03-13 22:47:06 +01:00
page.PageColor(Colors.White);
page.Size(PageSizes.A4);
2022-03-12 19:20:01 +01:00
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);
2022-03-12 12:17:00 +01:00
});
});
}
[Test]
public void EmptyText()
{
// issue 135
RenderingTest
.Create()
.ProduceImages()
.ShowResults()
.RenderDocument(container =>
{
container.Page(page =>
{
page.Margin(50);
2022-03-13 22:47:06 +01:00
page.PageColor(Colors.White);
2022-03-12 12:17:00 +01:00
page.Size(PageSizes.A4);
2022-03-12 19:20:01 +01:00
page.Content().Text(" ").FontSize(11).BackgroundColor(Colors.Red.Lighten3);
2022-03-12 12:17:00 +01:00
});
});
}
[Test]
public void Whitespaces()
{
// issue 135
RenderingTest
.Create()
.ProduceImages()
.ShowResults()
.RenderDocument(container =>
{
container.Page(page =>
{
page.Margin(50);
2022-03-13 22:47:06 +01:00
page.PageColor(Colors.White);
2022-03-12 12:17:00 +01:00
page.Size(PageSizes.A4);
2022-03-12 19:20:01 +01:00
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);
});
});
});
});
}
2021-08-09 22:35:39 +02:00
}
}