BackgroundTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if (AddColorBackground(document))
  27. {
  28. Console.WriteLine("Add color background done.");
  29. }
  30. else
  31. {
  32. Console.WriteLine("Add color background failed.");
  33. }
  34. Console.WriteLine("--------------------");
  35. document.Release();
  36. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  37. if (AddImageBackground(document))
  38. {
  39. Console.WriteLine("Add image background done.");
  40. }
  41. else
  42. {
  43. Console.WriteLine("Add image background failed.");
  44. }
  45. Console.WriteLine("--------------------");
  46. CPDFDocument colorBgDocument = CPDFDocument.InitWithFilePath("ColorBackground.pdf");
  47. CPDFDocument imageBgDocument = CPDFDocument.InitWithFilePath("ImageBackground.pdf");
  48. if (RemoveBackground(colorBgDocument, imageBgDocument))
  49. {
  50. Console.WriteLine("Remove background done.");
  51. }
  52. else
  53. {
  54. Console.WriteLine("Remove background failed.");
  55. }
  56. Console.WriteLine("--------------------");
  57. Console.WriteLine("Done!");
  58. Console.WriteLine("--------------------");
  59. Console.ReadLine();
  60. }
  61. static private bool AddColorBackground(CPDFDocument document)
  62. {
  63. CPDFBackground background = document.GetBackground();
  64. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  65. background.SetColor(new byte[] { 255, 0, 0 });
  66. background.SetOpacity(255);
  67. background.SetScale(1);
  68. background.SetRotation(0);
  69. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  70. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  71. background.SetXOffset(0);
  72. background.SetYOffset(0);
  73. background.SetPages("0-2");
  74. background.Update();
  75. string path = outputPath + "\\AddColorBackgroundTest.pdf";
  76. if (!document.WriteToFilePath(path))
  77. {
  78. return false;
  79. }
  80. Console.WriteLine("Browse the changed file in " + path);
  81. return true;
  82. }
  83. public static byte[] BitmapToByteArray(Bitmap bitmap)
  84. {
  85. BitmapData bmpdata = null;
  86. try
  87. {
  88. bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  89. int numbytes = bmpdata.Stride * bitmap.Height;
  90. byte[] bytedata = new byte[numbytes];
  91. IntPtr ptr = bmpdata.Scan0;
  92. Marshal.Copy(ptr, bytedata, 0, numbytes);
  93. return bytedata;
  94. }
  95. finally
  96. {
  97. if (bmpdata != null)
  98. bitmap.UnlockBits(bmpdata);
  99. }
  100. }
  101. static private bool AddImageBackground(CPDFDocument document)
  102. {
  103. CPDFBackground background = document.GetBackground();
  104. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  105. Bitmap bitmap = new Bitmap("logo.png");
  106. background.SetImage(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height, ComPDFKit.Import.C_Scale_Type.fitCenter);
  107. background.SetOpacity(128);
  108. background.SetScale(1);
  109. background.SetRotation(1f);
  110. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  111. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  112. background.SetXOffset(0);
  113. background.SetYOffset(0);
  114. background.SetPages("0-2");
  115. background.Update();
  116. string path = outputPath + "\\AddImageBackgroundTest.pdf";
  117. if (!document.WriteToFilePath(path))
  118. {
  119. return false;
  120. }
  121. Console.WriteLine("Browse the changed file in " + path);
  122. return true;
  123. }
  124. static private bool RemoveBackground(CPDFDocument colorBgDocument, CPDFDocument imageBgDocument)
  125. {
  126. CPDFBackground colorBackground = colorBgDocument.GetBackground();
  127. if(colorBackground.GetBackgroundType() != C_Background_Type.BG_TYPE_COLOR)
  128. {
  129. return false;
  130. }
  131. colorBackground.Clear();
  132. string path1 = outputPath + "\\ClearColorBgTest.pdf";
  133. if (!colorBgDocument.WriteToFilePath(path1))
  134. {
  135. return false;
  136. }
  137. Console.WriteLine("Browse the changed file in " + path1);
  138. CPDFBackground imageBackground = imageBgDocument.GetBackground();
  139. if(imageBackground.GetBackgroundType()!= C_Background_Type.BG_TYPE_IMAGE)
  140. {
  141. return false;
  142. }
  143. imageBackground.Clear();
  144. string path2 = outputPath + "\\ClearImageBgTest.pdf";
  145. if (!imageBgDocument.WriteToFilePath(path2))
  146. {
  147. return false;
  148. }
  149. Console.WriteLine("Browse the changed file in " + path2);
  150. return true;
  151. }
  152. }
  153. }