好久没写博客了,今天给大家分享一个图片转PDF的相关操作,也算是一次总结吧。

  首先需要准备动态库itextsharp.dll,这个dll去网上下载,都可以下载到,C#对PDF的操作都是基于这个类库来实现的。话不多说,直接上代码。

        /// <summary>
/// 导出PDF
/// </summary>
/// <param name="FilePath">图片的路劲</param>
/// <param name="FileName">图片的名称</param>
private bool PrintToPdf(string FilePath,string FileName )
{ iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(0, 0, iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
iTextSharp.text.Image image;
iTextSharp.text.Document document = null;
FileStream fs = null;
PdfWriter writer = null;
try
{
//生成PDF
document = new iTextSharp.text.Document(rec);
fs = new FileStream(FilePath + FileName + ".pdf", FileMode.Create); PdfWriter.GetInstance(document, fs);
document.Open();
image = iTextSharp.text.Image.GetInstance(FilePath + FileName + ".jpg");
//调整图片大小,使之适合A4
if (image.Height > rec.Height)
{
image.ScaleToFit(rec.Width, rec.Height);
}
else if (image.Width > rec.Width)
{
image.ScaleToFit(rec.Width, rec.Height);
} //调整图片位置,使之居中
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.NewPage();
document.Add(image);
}
catch (Exception ex)
{
return false;
}
finally
{
if (document.IsOpen())
{
document.Close();
}
fs.Close(); //最后删除生成的图片
File.Delete(FilePath + FileName + ".jpg");
} try
{
//添加水印图片,水印图片采取的是先生成图片,再添加到PDF文件中
document = new iTextSharp.text.Document(rec);
fs = new FileStream(FilePath + FileName + ".pdf", FileMode.Open);
writer = PdfWriter.GetInstance(document, fs);
iTextSharp.text.Image imgWater = CreateWaterImage();
PdfReader reader = new PdfReader(fs);
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage newPage;
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
document.NewPage();
newPage = writer.GetImportedPage(reader, j); //pdf旋转
if (newPage.Width > newPage.Height)
{
cb.AddTemplate(newPage, 0, -1f, 1f, 0, 0, newPage.Width);
}
else
{
cb.AddTemplate(newPage, 0, 0);
}
cb.AddImage(imgWater);
}
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
if (document.IsOpen())
{
document.Close();
}
writer.Close();
fs.Close();
}
} /// <summary>
/// 生成水印图片
/// </summary>
/// <returns></returns>
private iTextSharp.text.Image CreateWaterImage()
{
string WaterText = "水印文本";
Bitmap bitmap = new Bitmap(595, 842);
using (Graphics g = Graphics.FromImage(bitmap))
{
//重置图像
g.ResetTransform();
//设置旋转中心
g.TranslateTransform(bitmap.Width / 2, bitmap.Height / 2);
//旋转50度 顺时针
g.RotateTransform(-50);
//指定文本呈现的质量 解决文字锯齿问题
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 14);
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(100, 0, 0, 0)); //设置文字、字体、大小、颜色、起始位置
g.DrawString(WaterText, font, solidBrush, new Point(-240, -100)); g.DrawString(WaterText, font, solidBrush, new Point(-340, 100));
} //把水印图片保存在系统指定位置
bitmap.Save("C:\\WaterTemp.jpg"); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("C:\\WaterTemp.jpg");
//透明度,灰色填充
image.GrayFill = 20;
image.SetAbsolutePosition(0, 0); return image;
}

  

C# 图片转PDF,PDF增加水印文字的更多相关文章

  1. C#实现多个PDF合并及去除文字水印功能

    实现pdf合并就是使用Spire.Pdf.dll类库的方法,但是注意需要同时引用Spire.Pdf.dll和Spire.License.dll两个类库,且两个类库的版本要一致 String[] fil ...

  2. java实现图片和pdf添加铺满文字水印

    依赖jar包 <!-- pdf start --> <dependency> <groupId>com.itextpdf</groupId> <a ...

  3. pdf增加水印

    /// <summary> /// 为PDF添加水印或背景图片 /// </summary> /// <param name="strSourceFilePat ...

  4. java -PDF添加文本水印与图片水印

    java pdf添加水印文本及图片文本 PDF文件添加文本水印: private static int interval = 30; public static void waterMark(Stri ...

  5. Java在已存在的pdf文件中生成文字和图片--基础

    自我总结,有什么不足之处请告知,感激不尽!下一次总结pdf模板映射生成报表(应对多变的pdf报表需求,数据提供和报表生成解耦). 目的:在给定的pdf模板上生成报表,就需要知道最基本的操作:文字添加, ...

  6. 「Python实用秘技04」为pdf文件批量添加文字水印

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第4期 ...

  7. 图片合并成PDF,两个PDF的合并

    需求: 将多张手机照片合并成一个PDF,并于另一个成型PDF合并 过程: 使用全能扫描王处理一遍,拆剪掉多余部分,并提高亮度增加文字对比度 合并: 使用Faststone Capture合并图片即可. ...

  8. ABBYY如何把图片转换成pdf格式

    在制作工作文件的时候,有时候会遇到需要进行文件格式转换的情况,比较常见的文件格式转换就包含了Office与pdf格式之间的转换.但除此之外,图片与pdf格式也是可以进行转换的,那么图片要怎么操作,才能 ...

  9. 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】

    iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  10. 阿里云ali-oss图片增加水印

    先附上文档连接 : https://helpcdn.aliyun.com/document_detail/44957.html 水印文字或图片必须经过一下转化 URL安全的Base64位编码 在图片处 ...

随机推荐

  1. MySQL--索引的数据结构

    1.为什么使用索引 索引是存储引擎用于快速找到数据记录的一种数据结构,就好比一本教科书的目录部分,通过目录中找到对应文章的页面,便可以快速定位到需要的文章,mysql中也是一样的道理,进行数据查找时, ...

  2. Kubernetes(K8S)内核优化常用参数详解

    net.ipv4.tcp_keepalive_time=600 net.ipv4.tcp_keepalive_intvl=30 net.ipv4.tcp_keepalive_probes=10 net ...

  3. 有关idea的使用部分

    出现相关异常,提示类似粗在idea找不到相关的包加载失败. 执行mvn命令 mvn -U idea:idea 含义更新重新加载idea工程的相关jar

  4. [Linux]常用命令之【mount/umount】

    1 mount mount命令的作用是加载文件系统,它的用权限是超级用户或/etc/fstab中允许的使用者. 在Linux和Unix系统上,所有文件都是作为一个大型树(以/为根)的一部分访问的. 要 ...

  5. 迁移学习(COAL)《Generalized Domain Adaptation with Covariate and Label Shift CO-ALignment》

    论文信息 论文标题:Generalized Domain Adaptation with Covariate and Label Shift CO-ALignment论文作者:Shuhan Tan, ...

  6. ROS动态调试PID参数

    ROS动态调试PID参数 连接小车 注意:必须在同一区域网 ssh clbrobort@clbrobort 激活树莓派主板 roslaunch clbrobot bringup.launch 打开PI ...

  7. elastic-job源码(1)- job自动装配

    版本:3.1.0-SNAPSHOT git地址:https://github.com/apache/shardingsphere-elasticjob   Maven 坐标 <dependenc ...

  8. redhat中如何设置开机启动脚本

    redhat中如何设置开机启动脚本 前面转载了一篇关于开机启动脚本的文章,觉得写的很详细了,但是自己没有实践,下面是自己实践了一种方式,来设置开机启动脚本(因为有时候我们必须开机关闭一些防火墙,SEL ...

  9. React 监听页面滚动,界面动态显示

    以下是常见的监听滚动以及相应的操作 窗口滚动事件 当页面滚动时,如何动态切换布局/样式 1. 添加滚动事件的监听/注销 1 //在componentDidMount,进行scroll事件的注册,绑定一 ...

  10. sklearn数据预处理中fit(),transform()与fit_transform()的区别

    1 概述 注意这是数据预处理中的方法: Fit(): Method calculates the parameters μ and σ and saves them as internal objec ...