BackgroundTest.cs 93 KB

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