WatermarkTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. namespace WatermarkTest
  9. {
  10. internal class WatermarkTest
  11. {
  12. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Watermark";
  13. static void Main(string[] args)
  14. {
  15. #region Perparation work
  16. Console.WriteLine("Running Watermark test sample…\r\n");
  17. SDKLicenseHelper.LicenseVerify();
  18. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  19. if (!Directory.Exists(outputPath))
  20. {
  21. Directory.CreateDirectory(outputPath);
  22. }
  23. #endregion
  24. #region Sample 1: Add text watermark.
  25. if (AddTextWatermark(document))
  26. {
  27. Console.WriteLine("Add text watermark done.");
  28. }
  29. else
  30. {
  31. Console.WriteLine("Add text watermark failed.");
  32. }
  33. document.Release();
  34. Console.WriteLine("--------------------");
  35. #endregion
  36. #region Sample 2: Add image watermark
  37. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  38. if (AddImageWatermark(document))
  39. {
  40. Console.WriteLine("Add image watermark done.");
  41. }
  42. else
  43. {
  44. Console.WriteLine("Add image watermark failed.");
  45. }
  46. document.Release();
  47. Console.WriteLine("--------------------");
  48. #endregion
  49. #region Sample 3: Delete watermark
  50. CPDFDocument textWatermarkDocument = CPDFDocument.InitWithFilePath("Watermark.pdf");
  51. if (DeleteWatermark(textWatermarkDocument))
  52. {
  53. Console.WriteLine("Delete watermark done.");
  54. }
  55. else
  56. {
  57. Console.WriteLine("Delete watermark failed.");
  58. }
  59. textWatermarkDocument.Release();
  60. Console.WriteLine("--------------------");
  61. #endregion
  62. Console.WriteLine("Done!");
  63. Console.WriteLine("--------------------");
  64. Console.ReadLine();
  65. }
  66. /// <summary>
  67. /// Add text watermark
  68. /// </summary>
  69. /// <param name="document">Regular document without watermark.</param>
  70. static private bool AddTextWatermark(CPDFDocument document)
  71. {
  72. CPDFWatermark watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  73. watermark.SetText("test");
  74. watermark.SetFontName("Helvetica");
  75. watermark.SetPages("0-3");
  76. byte[] color = { 255, 0, 0 };
  77. watermark.SetTextRGBColor(color);
  78. watermark.SetScale(2);
  79. watermark.SetRotation(0);
  80. watermark.SetOpacity(120);
  81. watermark.SetVertalign(C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER);
  82. watermark.SetHorizalign(C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER);
  83. watermark.SetVertOffset(0);
  84. watermark.SetHorizOffset(0);
  85. watermark.SetFront(true);
  86. watermark.SetFullScreen(true);
  87. watermark.SetVerticalSpacing(10);
  88. watermark.SetHorizontalSpacing(10);
  89. watermark.CreateWatermark();
  90. string path = outputPath + "\\AddTextWatermarkTest.pdf";
  91. if (!document.WriteToFilePath(path))
  92. {
  93. return false;
  94. }
  95. Console.WriteLine("Browse the changed file in " + path);
  96. return true;
  97. }
  98. /// <summary>
  99. /// Convert the bitmap to an array that can be set as an image watermark
  100. /// </summary>
  101. /// <param name="bitmap">Image source to be used as a image watermark.</param>
  102. /// <returns>An array for setting image</returns>
  103. public static byte[] BitmapToByteArray(Bitmap bitmap)
  104. {
  105. BitmapData bmpdata = null;
  106. try
  107. {
  108. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  109. int numbytes = bmpdata.Stride * bitmap.Height;
  110. byte[] bytedata = new byte[numbytes];
  111. IntPtr ptr = bmpdata.Scan0;
  112. Marshal.Copy(ptr, bytedata, 0, numbytes);
  113. return bytedata;
  114. }
  115. finally
  116. {
  117. if (bmpdata != null)
  118. bitmap.UnlockBits(bmpdata);
  119. }
  120. }
  121. /// <summary>
  122. /// Add image watermark
  123. /// </summary>
  124. /// <param name="document">Image source to be used as a image watermark.</param>
  125. static private bool AddImageWatermark(CPDFDocument document)
  126. {
  127. CPDFWatermark watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  128. Bitmap bitmap = new Bitmap("logo.png");
  129. watermark.SetImage(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height);
  130. watermark.SetPages("0-3");
  131. watermark.SetScale(2);
  132. watermark.SetRotation(1);
  133. watermark.SetOpacity(128);
  134. watermark.SetVertalign(C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER);
  135. watermark.SetHorizalign(C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER);
  136. watermark.SetVertOffset(0);
  137. watermark.SetHorizOffset(0);
  138. watermark.SetFront(false);
  139. watermark.SetFullScreen(true);
  140. watermark.SetVerticalSpacing(10);
  141. watermark.SetHorizontalSpacing(10);
  142. watermark.CreateWatermark();
  143. watermark.UpdateWatermark();
  144. string path = outputPath + "\\AddImageWatermarkTest.pdf";
  145. if (!document.WriteToFilePath(path))
  146. {
  147. return false;
  148. }
  149. Console.WriteLine("Browse the changed file in " + path);
  150. return true;
  151. }
  152. /// <summary>
  153. /// Delete watermark
  154. /// </summary>
  155. /// <param name="watermarkDocument">Documents with text watermarks</param>
  156. static private bool DeleteWatermark(CPDFDocument watermarkDocument)
  157. {
  158. watermarkDocument.DeleteWatermarks();
  159. string path = outputPath + "\\DeleteWatermarkTest.pdf";
  160. if (!watermarkDocument.WriteToFilePath(path))
  161. {
  162. return false;
  163. }
  164. Console.WriteLine("Browse the changed file in " + path);
  165. return true;
  166. }
  167. }
  168. }