BackgroundTest.cs 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using ComPDFKit.PDFDocument;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Xml.Linq;
  12. namespace BackgroundTest
  13. {
  14. internal class BackgroundTest
  15. {
  16. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Background";
  17. static void Main(string[] args)
  18. {
  19. Console.WriteLine("Running Watermark test sample…\r\n");
  20. SDKLicenseHelper.LicenseVerify();
  21. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  22. if (!Directory.Exists(outputPath))
  23. {
  24. Directory.CreateDirectory(outputPath);
  25. }
  26. #region Sample 1: Add color background
  27. if (AddColorBackground(document))
  28. {
  29. Console.WriteLine("Add color background done.");
  30. }
  31. else
  32. {
  33. Console.WriteLine("Add color background failed.");
  34. }
  35. document.Release();
  36. Console.WriteLine("--------------------");
  37. #endregion
  38. #region Sample 2: Add image background
  39. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  40. if (AddImageBackground(document))
  41. {
  42. Console.WriteLine("Add image background done.");
  43. }
  44. else
  45. {
  46. Console.WriteLine("Add image background failed.");
  47. }
  48. document.Release();
  49. Console.WriteLine("--------------------");
  50. #endregion
  51. #region Remove background
  52. CPDFDocument colorBgDocument = CPDFDocument.InitWithFilePath("ColorBackground.pdf");
  53. CPDFDocument imageBgDocument = CPDFDocument.InitWithFilePath("ImageBackground.pdf");
  54. if (RemoveBackground(colorBgDocument, imageBgDocument))
  55. {
  56. Console.WriteLine("Remove background done.");
  57. }
  58. else
  59. {
  60. Console.WriteLine("Remove background failed.");
  61. }
  62. colorBgDocument.Release();
  63. imageBgDocument.Release();
  64. Console.WriteLine("--------------------");
  65. #endregion
  66. Console.WriteLine("Done!");
  67. Console.WriteLine("--------------------");
  68. Console.ReadLine();
  69. }
  70. /// <summary>
  71. /// Add color background
  72. /// </summary>
  73. /// <param name="document">Regular document</param>
  74. static private bool AddColorBackground(CPDFDocument document)
  75. {
  76. CPDFBackground background = document.GetBackground();
  77. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  78. background.SetColor(new byte[] { 255, 0, 0 });
  79. background.SetOpacity(255);//0-255
  80. background.SetScale(1);//1 == 100%
  81. background.SetRotation(0);//Use radians
  82. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  83. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  84. background.SetXOffset(0);
  85. background.SetYOffset(0);
  86. background.SetPages("0-2");//Page numbering from 0
  87. background.Update();//Note: update after setup is complete
  88. string path = outputPath + "\\AddColorBackgroundTest.pdf";
  89. if (!document.WriteToFilePath(path))
  90. {
  91. return false;
  92. }
  93. Console.WriteLine("Browse the changed file in " + path);
  94. return true;
  95. }
  96. /// <summary>
  97. /// Convert the bitmap to an array that can be set as an image watermark
  98. /// </summary>
  99. /// <param name="bitmap">Image source to be used as a image watermark.</param>
  100. /// <returns>An array for setting image</returns>
  101. public static byte[] BitmapToByteArray(Bitmap bitmap)
  102. {
  103. BitmapData bmpdata = null;
  104. try
  105. {
  106. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  107. int numbytes = bmpdata.Stride * bitmap.Height;
  108. byte[] bytedata = new byte[numbytes];
  109. IntPtr ptr = bmpdata.Scan0;
  110. Marshal.Copy(ptr, bytedata, 0, numbytes);
  111. return bytedata;
  112. }
  113. finally
  114. {
  115. if (bmpdata != null)
  116. bitmap.UnlockBits(bmpdata);
  117. }
  118. }
  119. /// <summary>
  120. /// Add image background.
  121. /// </summary>
  122. /// <param name="document">Regular document</param>
  123. static private bool AddImageBackground(CPDFDocument document)
  124. {
  125. CPDFBackground background = document.GetBackground();
  126. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  127. Bitmap bitmap = new Bitmap("logo.png");
  128. background.SetImage(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height, ComPDFKit.Import.C_Scale_Type.fitCenter);
  129. background.SetOpacity(128);//0-255
  130. background.SetScale(1);//1 == 100%
  131. background.SetRotation(1f);//Use radians
  132. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  133. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  134. background.SetXOffset(0);
  135. background.SetYOffset(0);
  136. background.SetPages("0-2");//Page numbering from 0
  137. background.Update();//Note: update after setup is complete
  138. string path = outputPath + "\\AddImageBackgroundTest.pdf";
  139. if (!document.WriteToFilePath(path))
  140. {
  141. return false;
  142. }
  143. Console.WriteLine("Browse the changed file in " + path);
  144. return true;
  145. }
  146. /// <summary>
  147. /// Check background type, and remove background.
  148. /// </summary>
  149. /// <param name="colorBgDocument">Document with clolor background.78</param>
  150. /// <param name="imageBgDocument">Document with image background.</param>
  151. /// <returns></returns>
  152. static private bool RemoveBackground(CPDFDocument colorBgDocument, CPDFDocument imageBgDocument)
  153. {
  154. CPDFBackground colorBackground = colorBgDocument.GetBackground();
  155. if(colorBackground.GetBackgroundType() != C_Background_Type.BG_TYPE_COLOR)
  156. {
  157. return false;
  158. }
  159. colorBackground.Clear();
  160. string path1 = outputPath + "\\ClearColorBgTest.pdf";
  161. if (!colorBgDocument.WriteToFilePath(path1))
  162. {
  163. return false;
  164. }
  165. Console.WriteLine("Browse the changed file in " + path1);
  166. CPDFBackground imageBackground = imageBgDocument.GetBackground();
  167. if(imageBackground.GetBackgroundType()!= C_Background_Type.BG_TYPE_IMAGE)
  168. {
  169. return false;
  170. }
  171. imageBackground.Clear();
  172. string path2 = outputPath + "\\ClearImageBgTest.pdf";
  173. if (!imageBgDocument.WriteToFilePath(path2))
  174. {
  175. return false;
  176. }
  177. Console.WriteLine("Browse the changed file in " + path2);
  178. return true;
  179. }
  180. }
  181. }