WatermarkTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFWatermark;
  3. using System;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using ImageMagick;
  9. namespace WatermarkTest
  10. {
  11. internal class WatermarkTest
  12. {
  13. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\CS";
  14. static void Main(string[] args)
  15. {
  16. #region Perparation work
  17. Console.WriteLine("Running Watermark test sample…\r\n");
  18. SDKLicenseHelper.LicenseVerify();
  19. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  20. if (!Directory.Exists(outputPath))
  21. {
  22. Directory.CreateDirectory(outputPath);
  23. }
  24. #endregion
  25. #region Sample 1: Add text watermark.
  26. if (AddTextWatermark(document))
  27. {
  28. Console.WriteLine("Add text watermark done.");
  29. }
  30. else
  31. {
  32. Console.WriteLine("Add text watermark failed.");
  33. }
  34. document.Release();
  35. Console.WriteLine("--------------------");
  36. #endregion
  37. #region Sample 2: Add image watermark
  38. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  39. if (AddImageWatermark(document))
  40. {
  41. Console.WriteLine("Add image watermark done.");
  42. }
  43. else
  44. {
  45. Console.WriteLine("Add image watermark failed.");
  46. }
  47. document.Release();
  48. Console.WriteLine("--------------------");
  49. #endregion
  50. #region Sample 3: Delete watermark
  51. CPDFDocument textWatermarkDocument = CPDFDocument.InitWithFilePath("Watermark.pdf");
  52. if (DeleteWatermark(textWatermarkDocument))
  53. {
  54. Console.WriteLine("Delete watermark done.");
  55. }
  56. else
  57. {
  58. Console.WriteLine("Delete watermark failed.");
  59. }
  60. textWatermarkDocument.Release();
  61. Console.WriteLine("--------------------");
  62. #endregion
  63. Console.WriteLine("Done!");
  64. Console.WriteLine("--------------------");
  65. Console.ReadLine();
  66. }
  67. /// <summary>
  68. /// Add text watermark
  69. /// </summary>
  70. /// <param name="document">Regular document without watermark.</param>
  71. static private bool AddTextWatermark(CPDFDocument document)
  72. {
  73. CPDFWatermark watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  74. watermark.SetText("test");
  75. watermark.SetFontName("Helvetica");
  76. watermark.SetPages("0-3");
  77. byte[] color = { 255, 0, 0 };
  78. watermark.SetTextRGBColor(color);
  79. watermark.SetScale(2);
  80. watermark.SetRotation(0);
  81. watermark.SetOpacity(120);
  82. watermark.SetVertalign(C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER);
  83. watermark.SetHorizalign(C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER);
  84. watermark.SetVertOffset(0);
  85. watermark.SetHorizOffset(0);
  86. watermark.SetFront(true);
  87. watermark.SetFullScreen(true);
  88. watermark.SetVerticalSpacing(10);
  89. watermark.SetHorizontalSpacing(10);
  90. watermark.CreateWatermark();
  91. string path = outputPath + "\\AddTextWatermarkTest.pdf";
  92. if (!document.WriteToFilePath(path))
  93. {
  94. return false;
  95. }
  96. Console.WriteLine("Browse the changed file in " + path);
  97. return true;
  98. }
  99. /// <summary>
  100. /// Add image watermark
  101. /// </summary>
  102. /// <param name="document">Image source to be used as a image watermark.</param>
  103. static private bool AddImageWatermark(CPDFDocument document)
  104. {
  105. CPDFWatermark watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  106. using (var image = new MagickImage("ComPDFKit_Logo.ico"))
  107. {
  108. byte[] byteArray = image.ToByteArray(MagickFormat.Bgra);
  109. watermark.SetImage(byteArray, image.Width, image.Height);
  110. }
  111. watermark.SetPages("0-3");
  112. watermark.SetScale(2);
  113. watermark.SetRotation(1);
  114. watermark.SetOpacity(128);
  115. watermark.SetVertalign(C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER);
  116. watermark.SetHorizalign(C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER);
  117. watermark.SetVertOffset(0);
  118. watermark.SetHorizOffset(0);
  119. watermark.SetFront(false);
  120. watermark.SetFullScreen(true);
  121. watermark.SetVerticalSpacing(10);
  122. watermark.SetHorizontalSpacing(10);
  123. watermark.CreateWatermark();
  124. string path = outputPath + "\\AddImageWatermarkTest.pdf";
  125. if (!document.WriteToFilePath(path))
  126. {
  127. return false;
  128. }
  129. Console.WriteLine("Browse the changed file in " + path);
  130. return true;
  131. }
  132. /// <summary>
  133. /// Delete watermark
  134. /// </summary>
  135. /// <param name="watermarkDocument">Documents with text watermarks</param>
  136. static private bool DeleteWatermark(CPDFDocument watermarkDocument)
  137. {
  138. watermarkDocument.DeleteWatermarks();
  139. string path = outputPath + "\\DeleteWatermarkTest.pdf";
  140. if (!watermarkDocument.WriteToFilePath(path))
  141. {
  142. return false;
  143. }
  144. Console.WriteLine("Browse the changed file in " + path);
  145. return true;
  146. }
  147. }
  148. }