PDF添加文本或者图片

使用的工具itextsharp,免费的

下载地址:链接: https://pan.baidu.com/s/1nIpcRoJ1EOylYr9oCXF6ZQ 提取码: cb3a 复制这段内容后打开百度网盘手机App,操作更方便哦

具体代码:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;
            try
            {
                var uri = "C:\\Users\\Administrator\\Desktop\\test01.pdf";

                //读取你的pdf
                pdfReader = new PdfReader(uri);

                //获取你的pdf总页数
                int numberOfPages = pdfReader.NumberOfPages;

                //获取你的pdf名字带后缀名
                string path = Path.GetFileName(uri);

                //获取你的pdf页面大小
                Rectangle pagesize = pdfReader.GetPageSize(1);
                //pdf的宽
                float width = pagesize.Width;
                //pdf的高
                float height = pagesize.Height;
                //创建新的pdf
                pdfStamper = new PdfStamper(pdfReader, new FileStream("C:\\Users\\Administrator\\Desktop\\testAddText.pdf", FileMode.Create, FileAccess.Write, FileShare.None));

                //创建你要显示的文字的字体,我这里用的是windows里自带的,但是必须是ttf格式
                BaseFont baseFont = BaseFont.CreateFont("C:\\Windows\\Fonts\\simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//获取系统的字体
                //设置你的字体大小
				//当然font里还有很多api,有style和color等,可以自己看
                iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 12);

                //你要添加的文本,和使用的字体
                Phrase p = new Phrase("明日之星公司制作", font);

                //要往哪一页添加
                for (int i = 1; i <= numberOfPages; i++)
                {

                    //你要显示在哪一页上,i从1开始,不是0
                    PdfContentByte over = pdfStamper.GetOverContent(i);//PdfContentBye类,用来设置图像和文本的绝对位置
                    //设置文本显示的位置,我这里设置的是右上角,文本右对齐显示
                    ColumnText.ShowTextAligned(over, Element.ALIGN_RIGHT, p, width - 5, height - 14, 0);
                }
            }
            catch(Exception e)
            {

            }
            finally
            {
			//最后一定要记住关闭这些打开的
                pdfReader.Close();
                pdfStamper.Close();
            }

            

        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值