BackgroundTest.cs 93 KB

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