DocumentOperationExamples.cs 4.9 KB

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