转自:https://www.cnblogs.com/Shi-zy/p/15494760.html
另一篇:https://www.cnblogs.com/z5337/p/7344636.html
用aspose.word把word 转pdf 的代码段:
Document pdfDocument = new Document(@"D:\pdfdir\jhrs.com.pdf"); for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++) { using (FileStream imageStream = new FileStream($"jhrs.com_{i}.png", FileMode.Create)) { Resolution resolution = new Resolution(300); PngDevice pngDevice = new PngDevice(resolution); pngDevice.Process(pdfDocument.Pages[pageCount], imageStream); imageStream.Close(); } }Aspose.pdf 添加文字水印
/// <summary> /// pdf添加文字水印(自定义水印行数和列数) /// </summary> /// <param name="pdfStream">pdf文件流</param> /// <param name="watermarkText">水印文字</param> /// <param name="fontSize">文字大小</param> /// <param name="fontColor">文字颜色</param> /// <param name="typeface">字体:(中文水印请自行查找相应字体的英文名称,如:宋体(SimSun),黑体(SimHei),仿宋(FangSong),微软雅黑(Microsoft YaHei)等等...)</param> /// <param name="rotation">倾斜角度</param> /// <param name="opacity">不透明度(值为:0-1)</param> /// <param name="watermarkRowNum">水印行数</param> /// <param name="watermarkColumnNum">水印列数</param> /// <returns></returns> public Stream AddTextWatermarkCustom(Stream pdfStream, string watermarkText, float fontSize, Aspose.Pdf.Color fontColor, string typeface, double rotation = 0, double opacity = 0, int watermarkRowNum = 1, int watermarkColumnNum = 1) { try { /*计算倾斜高度-start*/ var angle = Math.Abs(rotation); var width = watermarkText.Length * fontSize; var radian = 2 * Math.PI / 360 * angle; var top = Math.Sin(radian) * width; /*计算倾斜高度-end*/ Document document = new Document(pdfStream); for (int i = 1; i <= document.Pages.Count; i++) { var pageWidth = document.Pages[i].GetPageRect(false).Width; var pageHeight = document.Pages[i].GetPageRect(false).Height; WatermarkArtifact artifact = new WatermarkArtifact(); artifact.SetTextAndState(watermarkText, new TextState { FontSize = fontSize, ForegroundColor = fontColor, Font = FontRepository.FindFont(typeface), }); artifact.Rotation = rotation; artifact.Opacity = opacity; artifact.IsBackground = false; if (watermarkRowNum == 1 && watermarkColumnNum == 1) { artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Center; artifact.ArtifactVerticalAlignment = VerticalAlignment.Center; document.Pages[i].Artifacts.Add(artifact); } else { artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Left; artifact.ArtifactVerticalAlignment = VerticalAlignment.Top; for (int j = 1; j <= watermarkColumnNum; j++) { artifact.LeftMargin = (j - 1) * (pageWidth / watermarkColumnNum) + fontSize; for (int z = 1; z <= watermarkRowNum; z++) { artifact.TopMargin = (z - 1) * (pageHeight / watermarkRowNum) + top; document.Pages[i].Artifacts.Add(artifact); } } } } Stream outStream = new MemoryStream(); document.Save(outStream, SaveFormat.Pdf); outStream.Seek(0, SeekOrigin.Begin); return outStream; } catch (Exception ex) { throw ex; } } /// <summary> /// pdf添加文字水印(平铺) /// </summary> /// <param name="pdfStream">pdf文件流</param> /// <param name="watermarkText">水印文字</param> /// <param name="fontSize">文字大小</param> /// <param name="fontColor">文字颜色</param> /// <param name="typeface">字体:(中文水印请自行查找相应字体的英文名称,如:宋体(SimSun),黑体(SimHei),仿宋(FangSong),微软雅黑(Microsoft YaHei)等等...)</param> /// <param name="rotation">倾斜角度</param> /// <param name="opacity">不透明度(值为:0-1)</param> /// <param name="density">水印密度(值为0-1)</param> /// <returns></returns> public Stream AddTextWatermarkTile(Stream pdfStream, string watermarkText, float fontSize, Aspose.Pdf.Color fontColor, string typeface, double rotation = 0, double opacity = 0, double density = 1) { try { Document document = new Document(pdfStream); /*计算水印详细-start*/ var angle = Math.Abs(rotation); var width = watermarkText.Length * fontSize; var radian = 2 * Math.PI / 360 * angle; var top = Math.Sin(radian) * width; var row = Math.Cos(radian) * width; var pageWidth = document.Pages[1].GetPageRect(false).Width; var pageHeight = document.Pages[1].GetPageRect(false).Height; int watermarkRowNum = (int)(pageHeight / top * density); int watermarkColumnNum = (int)(pageHeight / row * density); if (rotation == 0) { watermarkRowNum = (int)(pageHeight / fontSize * density); watermarkColumnNum = (int)(pageHeight / width * density); } /*计算水印详细-end*/ for (int i = 1; i <= document.Pages.Count; i++) { WatermarkArtifact artifact = new WatermarkArtifact(); artifact.SetTextAndState(watermarkText, new TextState { FontSize = fontSize, ForegroundColor = fontColor, Font = FontRepository.FindFont(typeface), }); artifact.Rotation = rotation; artifact.Opacity = opacity; artifact.IsBackground = false; artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Left; artifact.ArtifactVerticalAlignment = VerticalAlignment.Top; for (int j = 1; j <= watermarkColumnNum; j++) { artifact.LeftMargin = (j - 1) * (pageWidth / watermarkColumnNum) + fontSize; for (int z = 1; z <= watermarkRowNum; z++) { artifact.TopMargin = (z - 1) * (pageHeight / watermarkRowNum) + top; document.Pages[i].Artifacts.Add(artifact); } } } Stream outStream = new MemoryStream(); document.Save(outStream, SaveFormat.Pdf); outStream.Seek(0, SeekOrigin.Begin); return outStream; } catch (Exception ex) { throw ex; } }
调用代码:
Stream stream = File.OpenRead(@"C:\Users\HEDY\Desktop\Test\2.pdf"); var outStream = AddTextWatermarkCustom(stream, "测试水印文字", 32, Color.Blue, "SimSun", 45, 0.5, 6, 3); var saveStream = new FileStream($@"C:\Users\HEDY\Desktop\Test\1_{Guid.NewGuid()}.pdf", FileMode.Create); outStream.CopyTo(saveStream); outStream.Close(); saveStream.Close();
标签:C#,水印,int,Aspose,var,pdf,Document,Math From: https://www.cnblogs.com/z5337/p/18570686