LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#给PDF每一页都加上自己的图片logo标识

admin
2023年5月22日 9:37 本文热度 549

自己做了一个PDF说明文件给客户,现在需要在每一页上都加上自己的logo图片,具体操作过程如下:


建项目这些就省略了,直接上代码:

using iTextSharp.text.pdf;
using System;
using System.IO;
using System.Windows.Forms;
namespace Pic2PDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        
        private void button1_Click(object sender, EventArgs e)
        {
            string path = Application.StartupPath;            //源PDF地址
            string pdffilename = path + "\\test.pdf";            //加完图片的PDF地址
            string pdffilename1 = path + "\\test1.pdf";            //图片文件地址
            string picfilename = path + "\\sign.jpg";            //执行操作,加上图片
            string r = AddPics(pdffilename, pdffilename1, picfilename);
            MessageBox.Show(r);
        }        
        public static string AddPics(string PdfPath, string OutPdfPath, string picpath)
        {            
            try
            {                
                if (File.Exists(OutPdfPath))
                {
                    File.delete(OutPdfPath);
                }
                PdfReader reader = new PdfReader(PdfPath);
                PdfStamper stamp = new PdfStamper(reader, new FileStream(OutPdfPath, FileMode.create));                
                int n = reader.NumberOfPages;                
                int i = 0;
                PdfContentByte under;
                iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(picpath);
                PDFATT att = new PDFATT(PdfPath);                
                float ww = att.Width();                
                float hh = att.Height();                
                float w = im.Width;                
                float h = im.Height; 
                float locationx;                
                float locationy;
                locationx = ww - w - 20;
                locationy = 50;
                im.SetAbsolutePosition(locationx, locationy);
                im.ScaleAbsolute(w, h);                
                while (i < n)
                {
                    i++;
                    under = stamp.GetOverContent(i);
                    under.AddImage(im, false);
                }
                stamp.Close();
                reader.Close();
            }            
            catch (Exception ex)
            {                
                return ex.Message;
            }
            return "ok";
        }        
        class PDFATT
        {
            PdfReader reader;            
            public PDFATT(string iPdfFilePath)
            {
                reader = new PdfReader(iPdfFilePath);
            }
            public int PageCount()
            {
                return reader.NumberOfPages;
            }
            public float Width()
            {
                return reader.GetPageSize(1).Width;
            }
            public float Height()
            {
                return reader.GetPageSize(1).Height;
            }
        }
    }
}

我这里设置的是添加到右下角了,位置的代码主要在这个地方

PDFATT att = new PDFATT(PdfPath);

float ww = att.Width(); //PDF的宽度

float hh = att.Height(); //PDF的高度

float w = im.Width; //图片宽

float h = im.Height; //图片高

float locationx;

float locationy;

locationx = ww - w - 20; //横向位置自己计算

locationy = 50; //纵向位置自己试下就行

其它的都很简单了。

运行效果如下:


该文章在 2023/5/22 9:37:43 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved