BarcodeExamples.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. using ZXing;
  5. using ZXing.OneD;
  6. using ZXing.QrCode;
  7. using ZXing.Rendering;
  8. namespace QuestPDF.DocumentationExamples;
  9. public class BarcodeExamples
  10. {
  11. [Test]
  12. public void BarcodeExample()
  13. {
  14. Settings.UseEnvironmentFonts = false;
  15. Document
  16. .Create(document =>
  17. {
  18. document.Page(page =>
  19. {
  20. page.MinSize(new PageSize(550, 0));
  21. page.MaxSize(new PageSize(550, 1000));
  22. page.DefaultTextStyle(x => x.FontSize(16));
  23. page.Content()
  24. .Background(Colors.Grey.Lighten3)
  25. .Padding(25)
  26. .Row(row =>
  27. {
  28. var productId = Random.Shared.NextInt64() % 10_000_000;
  29. row.Spacing(20);
  30. row.RelativeItem().Text(text =>
  31. {
  32. text.ParagraphSpacing(10);
  33. text.Span("Product ID: ").Bold();
  34. text.Line(productId.ToString("D7"));
  35. text.Span("Name: ").Bold();
  36. text.Line(Placeholders.Label());
  37. text.Span("Description: ").Bold();
  38. text.Span(Placeholders.Sentence());
  39. });
  40. row.AutoItem()
  41. .Background(Colors.White)
  42. .AlignCenter()
  43. .AlignMiddle()
  44. .Width(200)
  45. .Height(75)
  46. .Svg(size =>
  47. {
  48. var content = productId.ToString("D7");
  49. var writer = new EAN8Writer();
  50. var eanCode = writer.encode(content, BarcodeFormat.EAN_8, (int)size.Width, (int)size.Height);
  51. var renderer = new SvgRenderer { FontName = "Lato", FontSize = 16 };
  52. return renderer.Render(eanCode, BarcodeFormat.EAN_8, content).Content;
  53. });
  54. });
  55. });
  56. })
  57. .GenerateImages(x => "barcode.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.Best, RasterDpi = 144 });
  58. }
  59. [Test]
  60. public void QRCodeExample()
  61. {
  62. Document
  63. .Create(document =>
  64. {
  65. document.Page(page =>
  66. {
  67. page.MinSize(new PageSize(550, 0));
  68. page.MaxSize(new PageSize(550, 1000));
  69. page.DefaultTextStyle(x => x.FontSize(20));
  70. page.Content()
  71. .Background(Colors.Grey.Lighten3)
  72. .Padding(25)
  73. .Row(row =>
  74. {
  75. const string url = "https://en.wikipedia.org/wiki/Algorithm";
  76. row.Spacing(20);
  77. row.RelativeItem()
  78. .AlignMiddle()
  79. .Text(text =>
  80. {
  81. text.Justify();
  82. text.Span("In mathematics and computer science, ");
  83. text.Span("an algorithm").Bold().BackgroundColor(Colors.White);
  84. text.Span(" is a finite sequence of mathematically rigorous instructions, typically used to solve a class of specific problems or to perform a computation. ");
  85. text.Hyperlink("Learn more", url).Underline().FontColor(Colors.Blue.Darken2);
  86. });
  87. row.ConstantItem(5, Unit.Centimetre)
  88. .AspectRatio(1)
  89. .Background(Colors.White)
  90. .Svg(size =>
  91. {
  92. var writer = new QRCodeWriter();
  93. var qrCode = writer.encode(url, BarcodeFormat.QR_CODE, (int)size.Width, (int)size.Height);
  94. var renderer = new SvgRenderer { FontName = "Lato" };
  95. return renderer.Render(qrCode, BarcodeFormat.EAN_13, null).Content;
  96. });
  97. });
  98. });
  99. })
  100. .GenerateImages(x => "qrcode.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.VeryHigh, RasterDpi = 144 });
  101. }
  102. }