BackgroundTest.cs 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. using ImageMagick;
  8. namespace BackgroundTest
  9. {
  10. internal class BackgroundTest
  11. {
  12. static private string parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
  13. static private string outputPath = Path.Combine(parentPath, "Output", "CS");
  14. static void Main(string[] args)
  15. {
  16. Console.WriteLine("Running Watermark test sample…" + Environment.NewLine);
  17. SDKLicenseHelper.LicenseVerify();
  18. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  19. if (!Directory.Exists(outputPath))
  20. {
  21. Directory.CreateDirectory(outputPath);
  22. }
  23. #region Sample 1: Add color background
  24. if (AddColorBackground(document))
  25. {
  26. Console.WriteLine("Add color background done.");
  27. }
  28. document.Release();
  29. Console.WriteLine("--------------------");
  30. #endregion
  31. #region Sample 2: Add image background
  32. document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  33. if (AddImageBackground(document))
  34. {
  35. Console.WriteLine("Add image background done.");
  36. }
  37. document.Release();
  38. Console.WriteLine("--------------------");
  39. #endregion
  40. #region Sample 3: Remove background
  41. CPDFDocument colorBgDocument = CPDFDocument.InitWithFilePath("ColorBackground.pdf");
  42. CPDFDocument imageBgDocument = CPDFDocument.InitWithFilePath("ImageBackground.pdf");
  43. if (RemoveBackground(colorBgDocument, imageBgDocument))
  44. {
  45. Console.WriteLine("Remove background done.");
  46. }
  47. colorBgDocument.Release();
  48. imageBgDocument.Release();
  49. Console.WriteLine("--------------------");
  50. #endregion
  51. Console.WriteLine("Done!");
  52. Console.WriteLine("--------------------");
  53. Console.ReadLine();
  54. }
  55. /// <summary>
  56. /// Add color background
  57. /// </summary>
  58. /// <param name="document">Regular document</param>
  59. static private bool AddColorBackground(CPDFDocument document)
  60. {
  61. CPDFBackground background = document.GetBackground();
  62. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  63. background.SetColor(new byte[] { 255, 0, 0 });
  64. background.SetOpacity(255);//0-255
  65. background.SetScale(1);//1 == 100%
  66. background.SetRotation(0);//Use radians
  67. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  68. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  69. background.SetXOffset(0);
  70. background.SetYOffset(0);
  71. background.SetPages("0-2");//Page numbering from 0
  72. background.Update();//Note: update after setup is complete
  73. string path = Path.Combine(outputPath, "AddColorBackgroundTest.pdf");
  74. if (!document.WriteToFilePath(path))
  75. {
  76. return false;
  77. }
  78. Console.WriteLine("Browse the changed file in " + path);
  79. return true;
  80. }
  81. /// <summary>
  82. /// Add image background.
  83. /// </summary>
  84. /// <param name="document">Regular document</param>
  85. static private bool AddImageBackground(CPDFDocument document)
  86. {
  87. CPDFBackground background = document.GetBackground();
  88. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  89. using (var image = new MagickImage("ComPDFKit_Logo.ico"))
  90. {
  91. byte[] byteArray = image.ToByteArray(MagickFormat.Bgra);
  92. background.SetImage(byteArray, image.Width, image.Height, ComPDFKit.Import.C_Scale_Type.fitCenter);
  93. }
  94. background.SetOpacity(128);//0-255
  95. background.SetScale(1);//1 == 100%
  96. background.SetRotation(1f);//Use radians
  97. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  98. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  99. background.SetXOffset(0);
  100. background.SetYOffset(0);
  101. background.SetPages("0-2");//Page numbering from 0
  102. background.Update();//Note: update after setup is complete
  103. string path = Path.Combine(outputPath, "AddImageBackgroundTest.pdf");
  104. if (!document.WriteToFilePath(path))
  105. {
  106. return false;
  107. }
  108. Console.WriteLine("Browse the changed file in " + path);
  109. return true;
  110. }
  111. /// <summary>
  112. /// Check background type, and remove background.
  113. /// </summary>
  114. /// <param name="colorBgDocument">Document with clolor background.</param>
  115. /// <param name="imageBgDocument">Document with image background.</param>
  116. /// <returns></returns>
  117. static private bool RemoveBackground(CPDFDocument colorBgDocument, CPDFDocument imageBgDocument)
  118. {
  119. CPDFBackground colorBackground = colorBgDocument.GetBackground();
  120. if(colorBackground.GetBackgroundType() != C_Background_Type.BG_TYPE_COLOR)
  121. {
  122. return false;
  123. }
  124. colorBackground.Clear();
  125. string path1 = Path.Combine(outputPath, "ClearColorBgTest.pdf");
  126. if (!colorBgDocument.WriteToFilePath(path1))
  127. {
  128. return false;
  129. }
  130. Console.WriteLine("Browse the changed file in " + path1);
  131. CPDFBackground imageBackground = imageBgDocument.GetBackground();
  132. imageBackground.Clear();
  133. string path2 = Path.Combine(outputPath, "ClearImageBgTest.pdf");
  134. if (!imageBgDocument.WriteToFilePath(path2))
  135. {
  136. return false;
  137. }
  138. Console.WriteLine("Browse the changed file in " + path2);
  139. return true;
  140. }
  141. }
  142. }