骑缝章常见于合同等比较重要的文件上,具有防止在文件内增减页码的作用。本文将介绍如何使用spire.pdf给pdf格式的合同文件添加骑缝章。
原pdf文件如下:
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("input.pdf");
pdfunitconvertor convert = new pdfunitconvertor();
pdfpagebase pagebase = null;
//获取分割后的印章图片
image[] images = getimage(doc.pages.count);
float x = 0;
float y = 0;
//将图片画到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));
}
//保存pdf文件
doc.savetofile("output.pdf");
doc.close();
}
//定义getimage方法,根据pdf页数分割印章图片
static image[] getimage(int num)
{
list lists = new list();
image image = image.fromfile("sealimage.jpg");
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("input.pdf")
dim convert as new pdfunitconvertor()
dim pagebase as pdfpagebase = nothing
'获取分割后的印章图片
dim images as image() = getimage(doc.pages.count)
dim x as single = 0
dim y as single = 0
'将图片画到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
'保存pdf文件
doc.savetofile("output.pdf")
doc.close()
end sub
'定义getimage方法,根据pdf页数分割印章图片
private shared function getimage(num as integer) as image()
dim lists as new list()
dim image__1 as image = image.fromfile("sealimage.jpg")
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
添加骑缝章效果: