欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# / VB.NET在PDF中加盖骑缝章

程序员文章站 2022-03-28 14:29:56
...

骑缝章是用于往来业务合同,以确保合同真实、有效的印章加盖方法,是一种防范风险的重要方式。在程序中,可以通过使用工具来辅助加盖这种骑缝章。

工具:Free Spire.PDF for .NET (免费版)

 

下载地址及dll引用:下载并解压安装到本地指定路径,完成安装后,将安装路径下Bin文件夹下的Spire.Pdf.dll文件添加引用至VS程序。

 

C# 示例

using System.Collections.Generic;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace PagingSeal
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载PDF测试文档
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("test.pdf");          

            //获取分割后的印章图片
            Image[] images = GetImage(doc.Pages.Count);
            float x = 0;
            float y = 0;

            PdfUnitConvertor convert = new PdfUnitConvertor();
            PdfPageBase pageBase = null;

            //将图片绘制到PDF页面上的指定位置
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                pageBase = doc.Pages[i];
                x = pageBase.Size.Width - convert.ConvertToPixels(images[i].Width, PdfGraphicsUnit.Point) - 40;
                y = pageBase.Size.Height / 2;
                pageBase.Canvas.DrawImage(PdfImage.FromImage(images[i]), new PointF(x, y));
            }

            //保存文档
            doc.SaveToFile("Result.pdf");
            System.Diagnostics.Process.Start("Result.pdf");
        }
        //定义GetImage方法,根据PDF页数分割印章图片
        static Image[] GetImage(int num)
        {
            List<Bitmap> lists = new List<Bitmap>();
            Image image = Image.FromFile("seal.png");
            int w = image.Width / num;
            Bitmap bitmap = null;
            for (int i = 0; i < num; i++)
            {
                bitmap = new Bitmap(w, image.Height);
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
                {
                    g.Clear(Color.White);
                    Rectangle rect = new Rectangle(i * w, 0, w, image.Height);
                    g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), rect, GraphicsUnit.Pixel);
                }
                lists.Add(bitmap);
            }
            return lists.ToArray();
        }
    }
}

VB.NET

Imports System.Collections.Generic
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace PagingSeal
	Class Program
		Private Shared Sub Main(args As String())
			'加载PDF测试文档
			Dim doc As New PdfDocument()
			doc.LoadFromFile("test.pdf")

			'获取分割后的印章图片
			Dim images As Image() = GetImage(doc.Pages.Count)
			Dim x As Single = 0
			Dim y As Single = 0

			Dim convert As New PdfUnitConvertor()
			Dim pageBase As PdfPageBase = Nothing

			'将图片绘制到PDF页面上的指定位置
			For i As Integer = 0 To doc.Pages.Count - 1
				pageBase = doc.Pages(i)
				x = pageBase.Size.Width - convert.ConvertToPixels(images(i).Width, PdfGraphicsUnit.Point) - 40
				y = pageBase.Size.Height / 2
				pageBase.Canvas.DrawImage(PdfImage.FromImage(images(i)), New PointF(x, y))
			Next

			'保存文档
			doc.SaveToFile("Result.pdf")
			System.Diagnostics.Process.Start("Result.pdf")
		End Sub
		'定义GetImage方法,根据PDF页数分割印章图片
		Private Shared Function GetImage(num As Integer) As Image()
			Dim lists As New List(Of Bitmap)()
			Dim image__1 As Image = Image.FromFile("seal.png")
			Dim w As Integer = image__1.Width / num
			Dim bitmap As Bitmap = Nothing
			For i As Integer = 0 To num - 1
				bitmap = New Bitmap(w, image__1.Height)
				Using g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)
					g.Clear(Color.White)
					Dim rect As New Rectangle(i * w, 0, w, image__1.Height)
					g.DrawImage(image__1, New Rectangle(0, 0, bitmap.Width, bitmap.Height), rect, GraphicsUnit.Pixel)
				End Using
				lists.Add(bitmap)
			Next
			Return lists.ToArray()
		End Function
	End Class
End Namespace