123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using ComPDFKit.PDFDocument;
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Runtime.InteropServices;
- namespace BackgroundTest
- {
- internal class BackgroundTest
- {
- static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Background";
- static void Main(string[] args)
- {
- Console.WriteLine("Running Watermark test sample…\r\n");
- SDKLicenseHelper.LicenseVerify();
- CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
- if (!Directory.Exists(outputPath))
- {
- Directory.CreateDirectory(outputPath);
- }
- #region Sample 1: Add color background
- if (AddColorBackground(document))
- {
- Console.WriteLine("Add color background done.");
- }
- else
- {
- Console.WriteLine("Add color background failed.");
- }
- document.Release();
- Console.WriteLine("--------------------");
- #endregion
- #region Sample 2: Add image background
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
- if (AddImageBackground(document))
- {
- Console.WriteLine("Add image background done.");
- }
- else
- {
- Console.WriteLine("Add image background failed.");
- }
- document.Release();
- Console.WriteLine("--------------------");
- #endregion
- #region Remove background
- CPDFDocument colorBgDocument = CPDFDocument.InitWithFilePath("ColorBackground.pdf");
- CPDFDocument imageBgDocument = CPDFDocument.InitWithFilePath("ImageBackground.pdf");
- if (RemoveBackground(colorBgDocument, imageBgDocument))
- {
- Console.WriteLine("Remove background done.");
- }
- else
- {
- Console.WriteLine("Remove background failed.");
- }
- colorBgDocument.Release();
- imageBgDocument.Release();
- Console.WriteLine("--------------------");
- #endregion
- Console.WriteLine("Done!");
- Console.WriteLine("--------------------");
- Console.ReadLine();
- }
- /// <summary>
- /// Add color background
- /// </summary>
- /// <param name="document">Regular document</param>
- static private bool AddColorBackground(CPDFDocument document)
- {
- CPDFBackground background = document.GetBackground();
- background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
- background.SetColor(new byte[] { 255, 0, 0 });
- background.SetOpacity(255);//0-255
- background.SetScale(1);//1 == 100%
- background.SetRotation(0);//Use radians
- background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
- background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
- background.SetXOffset(0);
- background.SetYOffset(0);
- background.SetPages("0-2");//Page numbering from 0
- background.Update();//Note: update after setup is complete
- string path = outputPath + "\\AddColorBackgroundTest.pdf";
- if (!document.WriteToFilePath(path))
- {
- return false;
- }
- Console.WriteLine("Browse the changed file in " + path);
- return true;
- }
- /// <summary>
- /// Convert the bitmap to an array that can be set as an image watermark
- /// </summary>
- /// <param name="bitmap">Image source to be used as a image watermark.</param>
- /// <returns>An array for setting image</returns>
- public static byte[] BitmapToByteArray(Bitmap bitmap)
- {
- BitmapData bmpdata = null;
- try
- {
- bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
- int numbytes = bmpdata.Stride * bitmap.Height;
- byte[] bytedata = new byte[numbytes];
- IntPtr ptr = bmpdata.Scan0;
- Marshal.Copy(ptr, bytedata, 0, numbytes);
- return bytedata;
- }
- finally
- {
- if (bmpdata != null)
- bitmap.UnlockBits(bmpdata);
- }
- }
- /// <summary>
- /// Add image background.
- /// </summary>
- /// <param name="document">Regular document</param>
- static private bool AddImageBackground(CPDFDocument document)
- {
- CPDFBackground background = document.GetBackground();
- background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
- Bitmap bitmap = new Bitmap("logo.png");
- background.SetImage(BitmapToByteArray(bitmap), bitmap.Width, bitmap.Height, ComPDFKit.Import.C_Scale_Type.fitCenter);
- background.SetOpacity(128);//0-255
- background.SetScale(1);//1 == 100%
- background.SetRotation(1f);//Use radians
- background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
- background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
- background.SetXOffset(0);
- background.SetYOffset(0);
- background.SetPages("0-2");//Page numbering from 0
- background.Update();//Note: update after setup is complete
- string path = outputPath + "\\AddImageBackgroundTest.pdf";
- if (!document.WriteToFilePath(path))
- {
- return false;
- }
- Console.WriteLine("Browse the changed file in " + path);
- return true;
- }
- /// <summary>
- /// Check background type, and remove background.
- /// </summary>
- /// <param name="colorBgDocument">Document with clolor background.</param>
- /// <param name="imageBgDocument">Document with image background.</param>
- /// <returns></returns>
- static private bool RemoveBackground(CPDFDocument colorBgDocument, CPDFDocument imageBgDocument)
- {
- CPDFBackground colorBackground = colorBgDocument.GetBackground();
- if(colorBackground.GetBackgroundType() != C_Background_Type.BG_TYPE_COLOR)
- {
- return false;
- }
- colorBackground.Clear();
- string path1 = outputPath + "\\ClearColorBgTest.pdf";
- if (!colorBgDocument.WriteToFilePath(path1))
- {
- return false;
- }
- Console.WriteLine("Browse the changed file in " + path1);
- CPDFBackground imageBackground = imageBgDocument.GetBackground();
- imageBackground.Clear();
- string path2 = outputPath + "\\ClearImageBgTest.pdf";
- if (!imageBgDocument.WriteToFilePath(path2))
- {
- return false;
- }
- Console.WriteLine("Browse the changed file in " + path2);
- return true;
- }
- }
- }
|