using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PDFControls
{
public class PDFControlsItemSetting
{
public Color BorderColor { get; set; }
public string RectRadio { get; set; }
}
internal class PDFHelper
{
public static void addRectText(string pdfPath, string savepath, List<PDFControlsItemSetting> settings)
{
// 打开PDF文件
PdfDocument document = PdfReader.Open(pdfPath, PdfDocumentOpenMode.Modify);
// 获取第二页的PdfPage对象
PdfPage page = document.Pages[0];
// 获取XGraphics对象
XGraphics gfx = XGraphics.FromPdfPage(page);
foreach (var item in settings)
{
// 定义矩形边框颜色和宽度
XPen pen = new XPen(XColor.FromArgb(item.BorderColor.A, item.BorderColor.R, item.BorderColor.G, item.BorderColor.B), 1);
var rect2 = item.RectRadio.Split(',').Select(x => double.Parse(x)).ToList();
// 定义矩形区域
XRect rect = new XRect(page.Width * rect2[0], page.Height * rect2[1], page.Width * rect2[2], page.Height * rect2[3]);
// 在矩形区域绘制边框
gfx.DrawRectangle(pen, rect);
}
if (pdfPath == savepath)
{
var tempFile = Path.GetTempFileName();
document.Save(tempFile);
document.Close();
File.Move(tempFile, savepath);
}
else
{
// 保存PDF文档
document.Save(savepath);
// 关闭PDF文档
document.Close();
}
}
}
}
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1
{
public class pdfhelper
{
public static void addRectText()
{
// 打开PDF文件
PdfDocument document = PdfReader.Open("C:\\Users\\Administrator\\Desktop\\查看详情3.pdf", PdfDocumentOpenMode.Modify);
// 获取第二页的PdfPage对象
PdfPage page = document.Pages[0];
// 获取XGraphics对象
XGraphics gfx = XGraphics.FromPdfPage(page);
// 定义矩形边框颜色和宽度
XPen pen = new XPen(XColors.Red, 2);
// 定义矩形区域
XRect rect = new XRect(50, 50, 300, 100);
// 设置自定义字体解析器
GlobalFontSettings.FontResolver = new MyFontResolver("Arial");
// 在矩形区域内绘制文本
XFont font = new XFont("Arial", 12);
XBrush brush = XBrushes.Black;
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Center;
format.LineAlignment = XLineAlignment.Center;
gfx.DrawString("Hello, world!", font, brush, rect, format);
// 在矩形区域绘制边框
gfx.DrawRectangle(pen, rect);
// 保存PDF文档
document.Save("output.pdf");
// 关闭PDF文档
document.Close();
}
public static void addStamp(string imagePath)
{
// 打开PDF文件
PdfDocument document = PdfReader.Open("C:\\Users\\Administrator\\Desktop\\查看详情3.pdf", PdfDocumentOpenMode.Modify);
// 获取第二页的PdfPage对象
PdfPage page = document.Pages[0];
// 获取XGraphics对象
XGraphics gfx = XGraphics.FromPdfPage(page);
var stampImagePath = "stamp.png";
if (string.IsNullOrEmpty(imagePath))
{
imageHelper.CreateStemp(stampImagePath);
}
else
{
stampImagePath = imagePath;
}
// 加载图章图片
XImage image = XImage.FromFile(stampImagePath);
// 绘制图章
gfx.DrawImage(image, new XRect(100, 100, 100, 100));
// 保存PDF文档
document.Save("output.pdf");
// 关闭PDF文档
document.Close();
}
}
// 自定义字体解析器
public class MyFontResolver : IFontResolver
{
private readonly string _fontName;
public MyFontResolver(string fontName)
{
_fontName = fontName;
}
public byte[] GetFont(string faceName)
{
if (faceName == _fontName)
{
var ttfPath = GetFontFileName(faceName);
// 读取字体文件并返回字节数组
return System.IO.File.ReadAllBytes(ttfPath);
}
else
{
// 返回空字节数组表示不支持该字体
return new byte[0];
}
}
public static string GetFontFileName(string fontname)
{
string folderFullName = System.Environment.GetEnvironmentVariable("windir") + "\\fonts";
DirectoryInfo TheFolder = new DirectoryInfo(folderFullName);
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
if (NextFile.Exists)
{
if (fontname == getFontName(NextFile.FullName))
{
return NextFile.FullName;
}
}
}
return "";
}
private static string getFontName(string fontfilename)
{
PrivateFontCollection pfc = new PrivateFontCollection();
try
{
pfc.AddFontFile(fontfilename);
if (pfc.Families.Length > 0)
{
string fontName = pfc.Families[0].Name;
//Console.WriteLine(fontName + pfc.Families.Length);
return fontName;
}
}
catch (Exception e)
{
//Log.Error("Failed to add font. " + e.Message);
}
return "";
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
if (familyName == _fontName)
{
// 返回字体信息
return new FontResolverInfo(_fontName, true, true);
}
else
{
// 返回null表示不支持该字体
return null;
}
}
}
}
public class imageHelper
{
public static void CreateStemp(string savePath)
{
// 创建一个黑色背景的图片
Bitmap bitmap = new Bitmap(100, 100, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
//graphics.Clear(Color.Transparent);
// 获取要添加的文本
string text = "Hello, world!";
// 获取字体
Font font = new Font("Arial", 8, FontStyle.Regular, GraphicsUnit.Pixel);
// 获取文本的宽度和高度
SizeF textSize = graphics.MeasureString(text, font);
// 计算文本的位置
PointF location = new PointF((bitmap.Width - textSize.Width) / 2, (bitmap.Height - textSize.Height) / 2);
// 绘制文本
graphics.DrawString(text, font, Brushes.Red, location);
// 定义矩形边框颜色和宽度
Pen pen = new Pen(Color.Red, 10);
// 绘制矩形边框
graphics.DrawRectangle(pen, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
}
// 将Bitmap转换为字节数组
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
byte[] bytes = stream.ToArray();
// 将字节数组保存为图片文件
File.WriteAllBytes(savePath, bytes);
}
}
}