.NetCore实现word转PDF无第三方水印+批量生成自定义水印

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)
            {
                
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值