DocumentOperationTests.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4. using NUnit.Framework;
  5. using QuestPDF.Fluent;
  6. using QuestPDF.Helpers;
  7. using QuestPDF.Infrastructure;
  8. namespace QuestPDF.UnitTests;
  9. /// <summary>
  10. /// This test suite focus on executing various QPDF operations.
  11. /// In most cases, it does not check the result but rather if any exception is thrown.
  12. /// </summary>
  13. public class DocumentOperationTests
  14. {
  15. public DocumentOperationTests()
  16. {
  17. if (RuntimeInformation.RuntimeIdentifier == "linux-musl-x64")
  18. Assert.Ignore("The DocumentOperations functionality is not supported on Linux Musl, e.g. Alpine.");
  19. }
  20. [Test]
  21. public void TakePages()
  22. {
  23. GenerateSampleDocument("take-input.pdf", Colors.Red.Medium, 10);
  24. DocumentOperation
  25. .LoadFile("take-input.pdf")
  26. .TakePages("2-5")
  27. .Save("operation-take.pdf");
  28. }
  29. [Test]
  30. public void MergeTest()
  31. {
  32. GenerateSampleDocument("merge-first.pdf", Colors.Red.Medium, 3);
  33. GenerateSampleDocument("merge-second.pdf", Colors.Green.Medium, 5);
  34. GenerateSampleDocument("merge-third.pdf", Colors.Blue.Medium, 7);
  35. DocumentOperation
  36. .LoadFile("merge-first.pdf")
  37. .MergeFile("merge-second.pdf")
  38. .MergeFile("merge-third.pdf")
  39. .Save("operation-merged.pdf");
  40. }
  41. [Test]
  42. public void OverlayTest()
  43. {
  44. GenerateSampleDocument("overlay-main.pdf", Colors.Red.Medium, 10);
  45. GenerateSampleDocument("overlay-watermark.pdf", Colors.Green.Medium, 5);
  46. DocumentOperation
  47. .LoadFile("overlay-main.pdf")
  48. .OverlayFile(new DocumentOperation.LayerConfiguration
  49. {
  50. FilePath = "overlay-watermark.pdf"
  51. })
  52. .Save("operation-overlay.pdf");
  53. }
  54. [Test]
  55. public void UnderlayTest()
  56. {
  57. GenerateSampleDocument("underlay-main.pdf", Colors.Red.Medium, 10);
  58. GenerateSampleDocument("underlay-watermark.pdf", Colors.Green.Medium, 5);
  59. DocumentOperation
  60. .LoadFile("underlay-main.pdf")
  61. .UnderlayFile(new DocumentOperation.LayerConfiguration
  62. {
  63. FilePath = "underlay-watermark.pdf",
  64. })
  65. .Save("operation-underlay.pdf");
  66. }
  67. [Test]
  68. public void AttachmentTest()
  69. {
  70. GenerateSampleDocument("attachment-main.pdf", Colors.Red.Medium, 10);
  71. GenerateSampleDocument("attachment-file.pdf", Colors.Green.Medium, 5);
  72. DocumentOperation
  73. .LoadFile("attachment-main.pdf")
  74. .AddAttachment(new DocumentOperation.DocumentAttachment
  75. {
  76. FilePath = "attachment-file.pdf"
  77. })
  78. .Save("operation-attachment.pdf");
  79. }
  80. [Test]
  81. public void Encrypt40Test()
  82. {
  83. GenerateSampleDocument("encrypt40-input.pdf", Colors.Red.Medium, 10);
  84. DocumentOperation
  85. .LoadFile("encrypt40-input.pdf")
  86. .Encrypt(new DocumentOperation.Encryption40Bit()
  87. {
  88. UserPassword = "user_password",
  89. OwnerPassword = "owner_password"
  90. })
  91. .Save("operation-encrypt40.pdf");
  92. }
  93. [Test]
  94. public void Encrypt128Test()
  95. {
  96. GenerateSampleDocument("encrypt128-input.pdf", Colors.Red.Medium, 10);
  97. DocumentOperation
  98. .LoadFile("encrypt128-input.pdf")
  99. .Encrypt(new DocumentOperation.Encryption128Bit()
  100. {
  101. UserPassword = "user_password",
  102. OwnerPassword = "owner_password"
  103. })
  104. .Save("operation-encrypt128.pdf");
  105. }
  106. [Test]
  107. public void Encrypt256Test()
  108. {
  109. GenerateSampleDocument("encrypt256-input.pdf", Colors.Red.Medium, 10);
  110. DocumentOperation
  111. .LoadFile("encrypt256-input.pdf")
  112. .Encrypt(new DocumentOperation.Encryption256Bit()
  113. {
  114. UserPassword = "user_password",
  115. OwnerPassword = "owner_password"
  116. })
  117. .Save("operation-encrypt256.pdf");
  118. }
  119. [Test]
  120. public void LinearizeTest()
  121. {
  122. GenerateSampleDocument("linearize-input.pdf", Colors.Red.Medium, 10);
  123. DocumentOperation
  124. .LoadFile("linearize-input.pdf")
  125. .Linearize()
  126. .Save("operation-linearize.pdf");
  127. }
  128. [Test]
  129. public void DecryptTest()
  130. {
  131. GenerateSampleDocument("decrypt-input-not-encrypted.pdf", Colors.Red.Medium, 10);
  132. DocumentOperation
  133. .LoadFile("decrypt-input-not-encrypted.pdf")
  134. .Encrypt(new DocumentOperation.Encryption256Bit()
  135. {
  136. UserPassword = "user_password",
  137. OwnerPassword = "owner_password"
  138. })
  139. .Save("decrypt-input-encrypted.pdf");
  140. DocumentOperation
  141. .LoadFile("decrypt-input-encrypted.pdf", "owner_password")
  142. .Decrypt()
  143. .Save("operation-decrypt.pdf");
  144. }
  145. [Test]
  146. public void RemoveRestrictionsTest()
  147. {
  148. GenerateSampleDocument("remove-restrictions-input-not-encrypted.pdf", Colors.Red.Medium, 10);
  149. DocumentOperation
  150. .LoadFile("remove-restrictions-input-not-encrypted.pdf")
  151. .Encrypt(new DocumentOperation.Encryption256Bit()
  152. {
  153. UserPassword = string.Empty,
  154. OwnerPassword = "owner_password",
  155. AllowPrinting = false,
  156. AllowContentExtraction = false
  157. })
  158. .Save("remove-restrictions-input-encrypted.pdf");
  159. DocumentOperation
  160. .LoadFile("remove-restrictions-input-encrypted.pdf", "owner_password")
  161. .RemoveRestrictions()
  162. .Save("operation-remove-restrictions.pdf");
  163. }
  164. [Test]
  165. public void LoadEncryptedWithIncorrectPasswordTest()
  166. {
  167. GenerateSampleDocument("load-encrypted-input-not-encrypted.pdf", Colors.Red.Medium, 10);
  168. DocumentOperation
  169. .LoadFile("load-encrypted-input-not-encrypted.pdf")
  170. .Encrypt(new DocumentOperation.Encryption256Bit()
  171. {
  172. UserPassword = "user_password",
  173. OwnerPassword = "owner_password"
  174. })
  175. .Save("load-encrypted-input-encrypted.pdf");
  176. Assert.Catch(() =>
  177. {
  178. DocumentOperation
  179. .LoadFile("load-encrypted-input-encrypted.pdf", "wrong_password")
  180. .Save("operation-load-encrypted.pdf");
  181. });
  182. }
  183. [Test]
  184. public void ExtendMetadataTest()
  185. {
  186. GenerateSampleDocument("extend-metadata-input.pdf", Colors.Red.Medium, 10);
  187. // requires PDF/A-3b
  188. DocumentOperation
  189. .LoadFile("extend-metadata-input.pdf")
  190. .ExtendMetadata("<rdf:Description xmlns:dc=\"http://purl.org/dc/elements/1.1/\" rdf:about=\"\"></rdf:Description>")
  191. .Save("operation-extend-metadata.pdf");
  192. }
  193. private void GenerateSampleDocument(string filePath, Color color, int length)
  194. {
  195. Document
  196. .Create(document =>
  197. {
  198. document.Page(page =>
  199. {
  200. page.Size(PageSizes.A4);
  201. page.PageColor(Colors.Transparent);
  202. page.Content().Column(column =>
  203. {
  204. foreach (var i in Enumerable.Range(1, length))
  205. {
  206. if (i != 1)
  207. column.Item().PageBreak();
  208. var width = Random.Shared.Next(100, 200);
  209. var height = Random.Shared.Next(100, 200);
  210. var horizontalTranslation = Random.Shared.Next(0, (int)PageSizes.A4.Width - width);
  211. var verticalTranslation = Random.Shared.Next(0, (int)PageSizes.A4.Height - height);
  212. column.Item()
  213. .TranslateX(horizontalTranslation)
  214. .TranslateY(verticalTranslation)
  215. .Width(width)
  216. .Height(height)
  217. .Background(color.WithAlpha(64))
  218. .AlignCenter()
  219. .AlignMiddle()
  220. .Text($"{filePath}\npage {i}")
  221. .FontColor(color)
  222. .Bold()
  223. .FontSize(16);
  224. }
  225. });
  226. });
  227. })
  228. .WithSettings(new DocumentSettings { PdfA = true })
  229. .GeneratePdf(filePath);
  230. }
  231. }