C#版本的Aspose.Pdf 资源太难找了,尤其是没有水印的,Aspose.Pdf无水印版下载(10.1),尤其是你在某DN网站上下载的资源基本上测试后都是有水印的,或者就是报强签名的问题,花费了很多积分(这些积分可是实打实的银子啊)下载下来不能使用,想用板砖拍它心都有了。
提示:当前基于 .net framework进行测试的,笔者老项目中正常使用,至于.net core或者.net 5,6,7能否使用,请自行测试。
Aspose.Pdf无水印版下载
这玩意儿你不花钱的话,很难找到最新的无水印版,网上好多打着最新版本的,要么下载不成功,要么不可用,一是不想注册它们网站的用户,二是不想花钱,因此这儿就免费提供了,想下就下呗,你懂的。
下载地址:(Aspose.pdf无水印版下载)
Aspose.pdf 无水印版 (下载946 )下面的代码收录自网上,并没有完整测试,想来直接用问题不大。
C# PDF转图片
网页上要上传一些证件照,但是手头给我的的证件是pdf,不多几十张,由于需要一页显示,所以需要把他们转成图片。
百度在线pdf转jpg,好家伙,一大堆积网页,随便点一个开搞,一张,有点慢,算了等等,转换完成点下载,WHT? 12kb,好几分钟后,下载完成,然后上传第二个,你妹,需要会员,好吧注册个,终于转换了第二个,准备第三个,你妹,又不够了,要订阅,有着个订阅的钱,我还不如开个wps会员呢,连续试了几个网页,全是一个尿性,下载慢,数量少,不能批量,于是突然想到以前用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无水印版下载
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; } }
Aspose.pdf无水印版下载
调用代码部分代码:
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();
Aspose.pdf添加图片水印
/// <summary> /// pdf添加图片水印(自定义水印行数和列数) /// </summary> /// <param name="pdfStream">pdf文件流</param> /// <param name="imgurl">水印文字</param> /// <param name="rotation">倾斜角度</param> /// <param name="opacity">不透明度(值为:0-1)</param> /// <param name="watermarkRowNum">水印行数</param> /// <param name="watermarkColumnNum">水印列数</param> /// <returns></returns> public Stream AddImgWatermarkCustom(Stream pdfStream, string imgPath, double rotation = 0, double opacity = 0, int watermarkRowNum = 1, int watermarkColumnNum = 1) { try { /*计算倾斜高度-start*/ var angle = Math.Abs(rotation); var pic = new Bitmap(imgPath); var width = pic.Width; 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.SetImage(File.OpenRead(imgPath)); 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) + pic.Height; 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="rotation">倾斜角度</param> /// <param name="opacity">不透明度(值为:0-1)</param> /// <param name="density">水印密度(值为0-1)</param> /// <returns></returns> public Stream AddImgWatermarkTile(Stream pdfStream, string imgPath, double rotation = 0, double opacity = 0, double density = 1) { try { Document document = new Document(pdfStream); /*计算水印详细-start*/ var angle = Math.Abs(rotation); var pic = new Bitmap(imgPath); var width = pic.Width; var imgHeight = pic.Height; 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 / imgHeight * density); watermarkColumnNum = (int)(pageHeight / width * density); } /*计算水印详细-end*/ for (int i = 1; i <= document.Pages.Count; i++) { WatermarkArtifact artifact = new WatermarkArtifact(); artifact.SetImage(File.OpenRead(imgPath)); 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) + imgHeight; 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; } }
Aspose.pdf无水印版下载
调用代码部分代码:
Stream stream = File.OpenRead(@"C:\Users\HEDY\Desktop\Test\2.pdf"); var outStream = AddImgWatermarkTile(stream, @"C:\Users\HEDY\Desktop\Test\123.jpg", 0, 0.5,0.5); var saveStream = new FileStream($@"C:\Users\HEDY\Desktop\Test\1_{Guid.NewGuid().ToString("N")}.pdf", FileMode.Create); outStream.CopyTo(saveStream); outStream.Close(); saveStream.Close();
Aspose.pdf无水印版下载
代码引用来源:https://www.cnblogs.com/Shi-zy/p/15494760.html,未经测试代码,留作备用而已。