其他分享
首页 > 其他分享> > .NetCore实现word转PDF无第三方水印+批量生成自定义水印(不用安装Microsoft和WPS插件)

.NetCore实现word转PDF无第三方水印+批量生成自定义水印(不用安装Microsoft和WPS插件)

作者:互联网

技术实现:

  1. Aspose18.7破解版Word转PDF无官方水印,使用Aspose破解版需要在Nuget中下载安装 SkiaSharp1.60.0
  2. iTextSharp添加自定义水印,平铺展示
  3. Demo下载地址:https://download.csdn.net/download/huqngqing/19498465

代码

1. Aspose引入项目后,word转pdf代码示例

 public static void WordToPdf(string wordPath, string pdfPath)
        {
            try
            {
                //打开word文件
                Aspose.Words.Document doc = new Aspose.Words.Document(wordPath);
                //验证参数
                if (doc == null) { throw new Exception("Word文件无效"); }
                doc.Save(pdfPath, Aspose.Words.SaveFormat.Pdf);//还可以改成其它格式
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

2. 给生成的PDF添加水印,并平铺展示

public static void SetWatermark(string filePath, string text)
        {

            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;
            string tempPath = Path.GetDirectoryName(filePath) + "\\" + Path.GetFileNameWithoutExtension(filePath) + "_temp.pdf";
            try
            {
                pdfReader = new PdfReader(filePath);
                int total = pdfReader.NumberOfPages + 1;

                using (var fs = new FileStream(tempPath, FileMode.Create))
                {
                    pdfStamper = new PdfStamper(pdfReader, fs);
                    var psize = pdfReader.GetPageSize(1);
                    float width = psize.Width;
                    float height = psize.Height;
                    PdfContentByte content;
                    BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    PdfGState gs = new PdfGState();
                    int waterMarkNameLenth = text.Length;
                  
                    for (int i = 1; i < total; i++)
                    {
                        var fontLength = 12;
                        content = pdfStamper.GetOverContent(i);//在内容上方加水印
                        // content = pdfStamper.GetUnderContent(i);//在内容下方加水印
                        // 透明度
                        // gs.FillOpacity = 0.3f;

                        content.SetGState(gs);
                        //content.SetGrayFill(0.3f);
                        //开始写入文本
                        content.BeginText();
                        content.SetColorFill(BaseColor.Gray);
                        content.SetFontAndSize(font, fontLength);
                        //content.SetTextMatrix(120, 120);
                        var Margin =100;//调整水印的间距
                        var TextSize = new Size(text.Length * fontLength + Margin, text.Length * fontLength + Margin);
                        var RenderVeiwSize = new Size();
                        RenderVeiwSize.Width = (int)(width / TextSize.Width) + (width % TextSize.Width != 0 ? 1 : 0);
                        RenderVeiwSize.Height = (int)(height / TextSize.Height) + (height % TextSize.Height != 0 ? 1 : 0);

                        for (int h = 0; h < RenderVeiwSize.Height; h++)
                        {
                            for (int w = 0; w < RenderVeiwSize.Width; w++)
                            {
                                content.ShowTextAligned(Element.ALIGN_CENTER, text, TextSize.Width * w + TextSize.Width / 2, height - TextSize.Height * h - TextSize.Height / 2, 45);
                            }
                        }
                   
                        content.EndText();
                    }
                    if (pdfStamper != null)
                        pdfStamper.Close();

                    if (pdfReader != null)
                        pdfReader.Close();

                    System.IO.File.Copy(tempPath, filePath, true);
                }
                System.IO.File.Delete(tempPath);
            }
            catch (Exception ex)
            {
                
            }
        }

最终效果展示:
在这里插入图片描述
有关问题交流,可以加群,相互学习,相互探讨。
在这里插入图片描述

标签:插件,自定义,int,TextSize,水印,Height,content,Width,new
来源: https://blog.csdn.net/huqngqing/article/details/117696582