Foxtable(狐表)用户栏目专家坐堂 → 如何调用Spire.PDF.dll给PDF加印章?


  共有4444人关注过本帖树形打印复制链接

主题:如何调用Spire.PDF.dll给PDF加印章?

帅哥哟,离线,有人找我吗?
xietan417
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:731 积分:6493 威望:0 精华:0 注册:2011/6/26 11:45:00
如何调用Spire.PDF.dll给PDF加印章?  发帖心情 Post By:2020/6/9 23:12:00 [显示全部帖子]

  1. using Spire.Pdf;
  2. using Spire.Pdf.Annotations;
  3. using Spire.Pdf.Annotations.Appearance;
  4. using Spire.Pdf.Graphics;
  5. using System;
  6. using System.Drawing;
  7. namespace AddStamp_PDF
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //创建一个PdfDocument类对象,并加载PDF文档
  14. PdfDocument doc = new PdfDocument();
  15. doc.LoadFromFile(@"C:\Users\Administrator\Desktop\test.pdf");
  16. //获取PDF文档第一页
  17. PdfPageBase page = doc.Pages[0];
  18. //新建一个PdfRubberStampAnnotation对象,指定其注释的位置和大小
  19. PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(-5, -5), new SizeF(200, 200)));
  20. //实例化一个PdfAppearance对象,并加载作为印章的图片
  21. PdfAppearance loApprearance = new PdfAppearance(loStamp);
  22. PdfImage image = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\yz.jpg");
  23. //新建一个PDF模板,并在模板里绘制图片
  24. PdfTemplate template = new PdfTemplate(600, 800);
  25. template.Graphics.DrawImage(image, 0, 0);
  26. loApprearance.Normal = template;
  27. loStamp.Appearance = loApprearance;
  28. //添加印章到PDF文档
  29. page.AnnotationsWidget.Add(loStamp);
  30. //保存并打开文档
  31. string output = "ImageStamp.pdf";
  32. doc.SaveToFile(output);
  33. System.Diagnostics.Process.Start("ImageStamp.pdf");
  34. }
  35. }
  36. }


[此贴子已经被作者于2020/6/9 23:12:28编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
xietan417
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:731 积分:6493 威望:0 精华:0 注册:2011/6/26 11:45:00
  发帖心情 Post By:2020/6/9 23:23:00 [显示全部帖子]

Dim doc As PdfDocument =  New PdfDocument() 
doc.LoadFromFile("C:\Users\Administrator\Desktop\test.pdf")
'获取PDF文档第一页
Dim page As PdfPageBase =  doc.Pages(0) 
'新建一个PdfRubberStampAnnotation对象,指定其注释的位置和大小
Dim loStamp As PdfRubberStampAnnotation =  New PdfRubberStampAnnotation(New RectangleF(New PointF(-5,-5),New SizeF(200,200))) 
'实例化一个PdfAppearance对象,并加载作为印章的图片
Dim loApprearance As PdfAppearance =  New PdfAppearance(loStamp) 
Dim image As PdfImage =  PdfImage.FromFile("C:\Users\Administrator\Desktop\yz.jpg") 
'新建一个PDF模板,并在模板里绘制图片
Dim template As PdfTemplate =  New PdfTemplate(600,800) 
template.Graphics.DrawImage(image, 0, 0)
loApprearance.Normal = template
loStamp.Appearance = loApprearance
'添加印章到PDF文档
page.AnnotationsWidget.Add(loStamp)
'保存并打开文档
Dim output As String =  "ImageStamp.pdf" 
doc.SaveToFile(output)
System.Diagnostics.Process.Start("ImageStamp.pdf")

添加引用后,就可以这样直接运行了?

 回到顶部
帅哥哟,离线,有人找我吗?
xietan417
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:731 积分:6493 威望:0 精华:0 注册:2011/6/26 11:45:00
  发帖心情 Post By:2020/6/10 16:04:00 [显示全部帖子]

Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace DrawFilledRectangles
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
            Dim pdf As New PdfDocument()
            pdf.LoadFromFile("..\..\..\..\..\..\Data\DrawingTemplate.pdf")
            'Create one page
            Dim page As PdfPageBase = pdf.Pages(0)

            'save graphics state
            Dim state As PdfGraphicsState = page.Canvas.Save()

            'Set rectangle display location and size
            Dim x As Integer = 200
            Dim y As Integer = 300
            Dim width As Integer = 200
            Dim height As Integer = 120
            'Create one page and brush
            Dim pen As New PdfPen(Color.Black, 1f)
            Dim brush As PdfBrush = New PdfSolidBrush(Color.OrangeRed)
            'Draw a filled rectangle
            page.Canvas.DrawRectangle(pen, brush, New Rectangle(New Point(x, y), New Size(width, height)))

            'restore graphics
            page.Canvas.Restore(state)

            Dim result As String = "DrawFilledRectangles_out.pdf"

            'Save the document
            pdf.SaveToFile(result)
            'Launch the Pdf file
            PDFDocumentViewer(result)
        End Sub
        Private Sub PDFDocumentViewer(ByVal filename As String)
            Try
                Process.Start(filename)
            Catch
            End Try
        End Sub
    End Class
End Namespace
上面是列子的代码

此主题相关图片如下:xt1.png
按此在新窗口浏览图片

此主题相关图片如下:xt2.png
按此在新窗口浏览图片
Dim pdf As New sPdf.PdfDocument
pdf.LoadFromFile("C:\Users\EGIT\Desktop\tesst.pdf")

Dim page As sPdf.PdfPageBase = pdf.Pages(0)


Dim state As sG.PdfGraphicsState = page.Canvas.Save()

Dim x As Integer = 200
Dim y As Integer = 300
Dim width As Integer = 200
Dim height As Integer = 120


Dim pen As New sPdf.PdfPen(Color.Black, 1f) ’----------这个sPdf.PdfPen提示未定义类型     是命名空间的问题吗?

Dim brush As sPdf.PdfBrush = New PdfSolidBrush(Color.OrangeRed) '-------这个也是一样错误

page.Canvas.DrawRectangle(pen, brush, New Rectangle(New Point(x, y), New Size(width, height)))


page.Canvas.Restore(state)

Dim result As String = "C:\Users\EGIT\Desktop\test_out.pdf"

'Save the document
pdf.SaveToFile(result)



[此贴子已经被作者于2020/6/10 16:06:17编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
xietan417
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:731 积分:6493 威望:0 精华:0 注册:2011/6/26 11:45:00
  发帖心情 Post By:2020/6/11 10:01:00 [显示全部帖子]

搞定,就是属于Spire.Pdf.Graphics命名空间的,谢谢蓝版

Dim pdf As New sPdf.PdfDocument
pdf.LoadFromFile("C:\Users\EGIT\Desktop\tesst.pdf")

Dim page As sPdf.PdfPageBase = pdf.Pages(0)
Dim state As sG.PdfGraphicsState = page.Canvas.Save()

Dim x As Integer = 200
Dim y As Integer = 300
Dim width As Integer = 200
Dim height As Integer = 120

Dim pen As New sG.PdfPen(Color.Black, 1f) 
Dim brush As sG.PdfBrush = New sG.PdfSolidBrush(Color.OrangeRed)
page.Canvas.DrawRectangle(pen, brush, New Rectangle(New Point(x, y), New Size(width, height)))
page.Canvas.Restore(state)

Dim result As String = "C:\Users\EGIT\Desktop\test_out.pdf"
pdf.SaveToFile(result)



 回到顶部