ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-06-09 11:57:23  阅读:610  来源: 互联网

标签:插件 自定义 int TextSize 水印 Height content Width new


技术实现:

  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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有