c#pdf文件首页批量添加照片水印并使pdf文件名称和子目录文件名一致
使用的插件就是spire.pdf for net 在资源里边已经上传过了 有需要的自己下载就好了
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Annotations;
using System.Drawing;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new System.EventHandler(button1_Click);
//button2.Click += new System.EventHandler(button2_Click);
}
//水印照片存放路径
//string watermarkImagePath = "C:\\Users\\20755\\Desktop\\test\\444.png";
string watermarkImagePath = "C:\\Users\\Administrator\\Desktop\\test\\444.png";
//string extName = "*.pdf";
//List<string> nameList = new List<string>();
//button1是生成带水印的功能 button2只是用来测试的
private void button1_Click(object sender, EventArgs e)
{
try
{
FolderBrowserDialog folder = new FolderBrowserDialog();
folder.Description = "Open Folder";
if (folder.ShowDialog() == DialogResult.OK)
{
string folderPath = folder.SelectedPath;
string[] pdfFiles = Directory.GetFiles(folderPath, "*.pdf", SearchOption.AllDirectories);
foreach (string pdfFile in pdfFiles)
{
using (PdfDocument document = new PdfDocument())
{
document.LoadFromFile(pdfFile);
//foreach (PdfPageBase page in document.Pages)
//{
// Image image = Image.FromFile(watermarkImagePath);
// int imgWidth = image.Width;
// int imgHeight = image.Height;
// float pageWidth = page.ActualSize.Width;
// float pageHeight = page.ActualSize.Height;
// page.BackgroudOpacity = 1f;
// page.BackgroundImage = image;
// Rectangle rect = new Rectangle(200, 0, 200, 125);
// page.BackgroundRegion = rect;
//}
PdfPageBase firstPage = document.Pages[0]; // 获取第一页
Image image = Image.FromFile(watermarkImagePath);
int imgWidth = image.Width;
int imgHeight = image.Height;
float pageWidth = firstPage.ActualSize.Width;
float pageHeight = firstPage.ActualSize.Height;
firstPage.BackgroudOpacity = 1f;
firstPage.BackgroundImage = image;
Rectangle rect = new Rectangle(200, 0, 200, 125);
firstPage.BackgroundRegion = rect;
string outputFolder = "C:\\Users\\Administrator\\Desktop\\test\\newDir";
//string outputFolder = "C:\\Users\\20755\\Desktop\\test\\newDir";
Directory.CreateDirectory(outputFolder);
// 相对路径 根据pdf所在的位置拿到所在目录名称 并从开始选中的文件夹位置开始截取
//相当于 pdfFile= C:\\Users\\Administrator\\Desktop\\test\\111\\1.pdf
//relativePath =\test\\111\\1.pdf
string relativePath = Path.GetDirectoryName(pdfFile).Substring(folderPath.Length);
if (relativePath.StartsWith("\\"))
//如果路径是以\\开始 那么就从2的位置开始截取
//relativePath =111 这么做的目的其实就是为了拿到pdf上一级的目录文件夹名称
//如果是多层嵌套 这个地方的逻辑就需要再增加
relativePath = relativePath.Substring(1);
// 目录名 确保文件名和目录名保持一致
string dirName = Path.GetFileName(Path.GetDirectoryName(pdfFile));
// 新文件夹
string newFolderPath = Path.Combine(outputFolder, relativePath);
Directory.CreateDirectory(newFolderPath);
// 把文件名按照目录重新命名
string newFileName = dirName + ".pdf";
string newFilePath = Path.Combine(newFolderPath, newFileName);
//string outputFilePath = Path.Combine(newFolderPath, Path.GetFileName(pdfFile));
document.SaveToFile(newFilePath);
document.Close();
}
}
MessageBox.Show("Watermarks added successfully.");
}
else
{
MessageBox.Show("Failed to add watermarks.");
}
}
catch (Exception er)
{
// Handle any exceptions here
}
}