DocumentOperationExamples.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Runtime.InteropServices;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. namespace QuestPDF.DocumentationExamples;
  6. [TestFixture]
  7. public class DocumentOperationExamples
  8. {
  9. public DocumentOperationExamples()
  10. {
  11. if (RuntimeInformation.RuntimeIdentifier == "linux-musl-x64")
  12. Assert.Ignore("The DocumentOperations functionality is not supported on Linux Musl, e.g. Alpine.");
  13. }
  14. [Test]
  15. public void MergeFiles()
  16. {
  17. const string prefix = "document-operation-merge";
  18. GenerateSampleDocument($"{prefix}-source-red.pdf", Colors.Red.Lighten3, 2);
  19. GenerateSampleDocument($"{prefix}-source-green.pdf", Colors.Green.Lighten3, 3);
  20. GenerateSampleDocument($"{prefix}-source-blue.pdf", Colors.Blue.Lighten3, 5);
  21. DocumentOperation
  22. .LoadFile($"{prefix}-source-red.pdf")
  23. .MergeFile($"{prefix}-source-green.pdf")
  24. .MergeFile($"{prefix}-source-blue.pdf")
  25. .Save($"{prefix}-result.pdf");
  26. }
  27. [Test]
  28. public void SelectEvenPages()
  29. {
  30. const string prefix = "document-operation-select-even-pages";
  31. GenerateSampleDocument($"{prefix}-source.pdf", Colors.Indigo.Lighten3, 11);
  32. DocumentOperation
  33. .LoadFile($"{prefix}-source.pdf")
  34. .TakePages("1-z:even")
  35. .Save($"{prefix}-result.pdf");
  36. }
  37. [Test]
  38. public void Encrypt()
  39. {
  40. const string prefix = "document-operation-encrypt";
  41. GenerateSampleDocument($"{prefix}-source.pdf", Colors.Orange.Lighten3, 7);
  42. DocumentOperation
  43. .LoadFile($"{prefix}-source.pdf")
  44. .Encrypt(new DocumentOperation.Encryption256Bit()
  45. {
  46. UserPassword = "user-password",
  47. OwnerPassword = "owner-password",
  48. AllowContentExtraction = false,
  49. AllowPrinting = false
  50. })
  51. .Save($"{prefix}-result.pdf");
  52. }
  53. [Test]
  54. public void AddAttachment()
  55. {
  56. const string prefix = "document-operation-add-attachment";
  57. GenerateSampleDocument($"{prefix}-source.pdf", Colors.Cyan.Lighten3, 7);
  58. File.WriteAllText($"{prefix}-content.txt", "Hello, World!");
  59. DocumentOperation
  60. .LoadFile($"{prefix}-source.pdf")
  61. .AddAttachment(new DocumentOperation.DocumentAttachment
  62. {
  63. FilePath = $"{prefix}-content.txt",
  64. AttachmentName = "Attached message"
  65. })
  66. .Save($"{prefix}-result.pdf");
  67. }
  68. [Test]
  69. public void Overlay()
  70. {
  71. const string prefix = "document-operation-overlay";
  72. GenerateSampleDocument($"{prefix}-source.pdf", Colors.Cyan.Lighten3, 7);
  73. Document
  74. .Create(document =>
  75. {
  76. document.Page(page =>
  77. {
  78. page.Margin(1, Unit.Centimetre);
  79. page.PageColor(Colors.Transparent);
  80. page.Content().Column(column =>
  81. {
  82. foreach (var i in Enumerable.Range(0, 6))
  83. column.Item().PageBreak();
  84. });
  85. page.Footer().AlignCenter().Text(text =>
  86. {
  87. text.DefaultTextStyle(x => x.FontSize(24).Bold().FontColor(Colors.White));
  88. text.Span("Page ");
  89. text.CurrentPageNumber();
  90. text.Span(" of ");
  91. text.TotalPages();
  92. });
  93. });
  94. })
  95. .GeneratePdf($"{prefix}-content.pdf");
  96. DocumentOperation
  97. .LoadFile($"{prefix}-source.pdf")
  98. .OverlayFile(new DocumentOperation.LayerConfiguration
  99. {
  100. FilePath = $"{prefix}-content.pdf"
  101. })
  102. .Save($"{prefix}-result.pdf");
  103. }
  104. private void GenerateSampleDocument(string fileName, Color pageColor, int numberOfPages)
  105. {
  106. Document
  107. .Create(container =>
  108. {
  109. container.Page(page =>
  110. {
  111. page.Margin(1, Unit.Centimetre);
  112. page.PageColor(pageColor);
  113. page.Content().Column(column =>
  114. {
  115. foreach (var pageNumber in Enumerable.Range(1, numberOfPages))
  116. {
  117. column.Item()
  118. .Extend()
  119. .AlignCenter().AlignMiddle()
  120. .Text($"{pageNumber}")
  121. .FontSize(256)
  122. .FontColor(Colors.White)
  123. .Bold();
  124. if (pageNumber != numberOfPages)
  125. column.Item().PageBreak();
  126. }
  127. });
  128. });
  129. })
  130. .GeneratePdf(fileName);
  131. }
  132. }