C# PDF加盖电子章
winform界面
1.选择加签pdf按钮代码实现
-
private void button1_Click(object sender, EventArgs e)
-
{
-
OpenFileDialog op = new OpenFileDialog();
-
op.Filter = "PDF文件(*.pdf)|*.pdf";
-
bool flag = op.ShowDialog() == DialogResult.OK;
-
if (flag)
-
{
-
string pdfPath = Path.GetFullPath(op.FileName);
-
fileName = op.SafeFileName;
-
label2.Text = pdfPath;
-
}
-
}
每页加签按钮代码实现
-
private void button2_Click(object sender, EventArgs e)
-
{
-
string path = Application.StartupPath;
-
SignEachPage(label2.Text.ToString(), path "/PDF/" fileName, path @"\InspectionSeal\电子章.png"); // 参数(原pdf地址,加签后pdf地址,电子章地址)
-
Process.Start("explorer", Application.StartupPath "\\PDF");
-
label4.Text = path "/PDF/" fileName;
-
}
SignEachPage方法代码实现
-
public static void SignEachPage(string pdfPath, string outPdfPath, string imagePath)
-
{
-
//读取pdf
-
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfPath);
-
//读取页数
-
int pdfPageSize = pdfReader.NumberOfPages;
-
//创建新pdf
-
System.IO.FileStream outputStream = new System.IO.FileStream(outPdfPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
-
iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputStream);
-
//文件内容
-
iTextSharp.text.pdf.PdfContentByte waterMarkContent;
-
//读取第一页尺寸
-
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
-
//读取盖章图片
-
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);
-
//图片缩放到一定百分比
-
image.ScalePercent(60);
-
//设置图片位置,位置偏移方向为:左到右,下到上
-
image.SetAbsolutePosition(psize.Width / 10 * 7, psize.Height / 10);
-
//循环给每页盖章
-
for (int i = 1; i <= pdfPageSize; i )
-
{
-
//GetUnderContent 加在内容下层
-
//GetOverContent 加在内容上层
-
waterMarkContent = pdfStamper.GetUnderContent(i);
-
//添加
-
waterMarkContent.AddImage(image);
-
}
-
pdfStamper.Close();
-
pdfReader.Close();
-
//直接打开盖章后文件
-
// System.Diagnostics.Process.Start(outPdfPath);
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhiabbbh
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22