This article describes the C# implementation of pdf generated image text watermark class. Share it for your reference, as follows:
public class PDFSetWaterMark { /// <summary> /// Create a pdf that displays the specified image /// </summary> /// <param name="picPdfPath"></param> /// <param name="picPath"></param> /// <returns></returns> public static bool CreatePDFByPic(string picPdfPath, string picPath) { // Create a new document Document doc = new Document(); try { //Create a writer (Writer) and document object association (doc, new FileStream(picPdfPath, , )); //Open a document (); //Add content to the document Image img = (picPath); //(); (img); return true; } catch (Exception ex) { return false; throw ex; } finally { if (doc != null) { (); } } } /// <summary> /// Add picture watermark /// </summary> /// <param name="inputfilepath"></param> /// <param name="outputfilepath"></param> /// <param name="ModelPicName"></param> /// <param name="top"></param> /// <param name="left"></param> /// <returns></returns> public static bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left) { //throw new NotImplementedException(); PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(inputfilepath); int numberOfPages = ; psize = (1); float width = ; float height = ; pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, )); PdfContentByte waterMarkContent; image = (ModelPicName); = 20;//Transparency, grey fill ////Rotation ////Rotation angle //The location of the watermark if (left < 0) { left = width / 2 - + left; } //(left, (height - ) - top); (left, (height / 2 - ) - top); // Add watermark to each page, or you can also set a watermark to add a certain page. for (int i = 1; i <= numberOfPages; i++) { //waterMarkContent = (i);// Add watermark to the lower layer of the content waterMarkContent = (i);// Add watermark to the upper layer of the content (image); } //strMsg = "success"; return true; } catch (Exception ex) { throw ex; } finally { if (pdfStamper != null) (); if (pdfReader != null) (); } } /// <summary> /// Add normal deflection angle text watermark /// </summary> /// <param name="inputfilepath"></param> /// <param name="outputfilepath"></param> /// <param name="waterMarkName"></param> /// <param name="permission"></param> public static void setWatermark(string inputfilepath, string outputfilepath, string waterMarkName) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(inputfilepath); pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, )); int total = + 1; psize = (1); float width = ; float height = ; PdfContentByte content; BaseFont font = (@"C:\WINDOWS\Fonts\", BaseFont.IDENTITY_H, ); PdfGState gs = new PdfGState(); for (int i = 1; i < total; i++) { content = (i);//Add a watermark above the content //content = (i);//Add a watermark below the content //transparency = 0.3f; (gs); //(0.3f); //Start writing text (); (BaseColor.LIGHT_GRAY); (font, 100); (0, 0); (Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55); //(); //(font, 8); //(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0); (); } } catch (Exception ex) { throw ex; } finally { if (pdfStamper != null) (); if (pdfReader != null) (); } } /// <summary> /// Add tilt watermark /// </summary> /// <param name="inputfilepath"></param> /// <param name="outputfilepath"></param> /// <param name="waterMarkName"></param> /// <param name="userPassWord"></param> /// <param name="ownerPassWord"></param> /// <param name="permission"></param> public static void setWatermark(string inputfilepath, string outputfilepath, string waterMarkName, string userPassWord, string ownerPassWord, int permission) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(inputfilepath); pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, )); // Set password //(false,userPassWord, ownerPassWord, permission); int total = + 1; PdfContentByte content; BaseFont font = (@"C:\WINDOWS\Fonts\", BaseFont.IDENTITY_H, ); PdfGState gs = new PdfGState(); = 0.2f;//transparency int j = ; char c; int rise = 0; for (int i = 1; i < total; i++) { rise = 500; content = (i);//Add a watermark above the content //content = (i);//Add a watermark below the content (); (BaseColor.DARK_GRAY); (font, 50); // Set the font tilt of the watermark text begin if (j >= 15) { (200, 120); for (int k = 0; k < j; k++) { (rise); c = waterMarkName[k]; (c + ""); rise -= 20; } } else { (180, 100); for (int k = 0; k < j; k++) { (rise); c = waterMarkName[k]; (c + ""); rise -= 18; } } // Font settings end (); // Draw a circle //(250, 450, 350, 550); //(1f); //(); } } catch (Exception ex) { throw ex; } finally { if (pdfStamper != null) (); if (pdfReader != null) (); } } }
For more information about C# related content, please check out the topic of this site:Summary of C# picture operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.