以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  如何调用Spire.PDF.dll给PDF加印章?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=150881)

--  作者:xietan417
--  发布时间:2020/6/9 23:12:00
--  如何调用Spire.PDF.dll给PDF加印章?
  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编辑过]

--  作者:有点蓝
--  发布时间:2020/6/9 23:18:00
--  
把dll复制到foxtable安装目录,然后添加引用:http://www.foxtable.com/webhelp/topics/1936.htm

把上面mian函数里的代码转换为vb代码:http://converter.telerik.com/

--  作者:xietan417
--  发布时间: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")

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

--  作者:有点蓝
--  发布时间:2020/6/10 8:37:00
--  
写全命名空间引用比如:
Dim doc As Spire.Pdf.PdfDocument =  New Spire.Pdf.PdfDocument() 

如果不知道是哪个命名空间,看源码,或者官方开发文档

--  作者:xietan417
--  发布时间: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编辑过]

--  作者:有点蓝
--  发布时间:2020/6/10 16:24:00
--  
如果不知道是哪个命名空间,看源码,或者官方开发文档,下载安装vs,打开源码,把鼠标放到对应的类型里可以看到命名空间的提示。

这种绘制的应该是属于Spire.Pdf.Graphics命名空间的

Dim pen As New sG.PdfPen(Color.Black, 1f)

--  作者:xietan417
--  发布时间: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)



--  作者:fanjq8686
--  发布时间:2020/6/30 15:23:00
--  
没太看明白,想请教一下,是实现在一页pdf文件上任意位置加盖印章吗?
--  作者:李孝春
--  发布时间:2020/12/30 21:49:00
--  回复:(xietan417)如何调用Spire.PDF.dll给PDF加印章...
Spire.PDF.dll这个是否可以分享一下呢?
--  作者:有点蓝
--  发布时间:2020/12/31 8:51:00
--  
百度啊,大把的。https://www.e-iceblue.cn/Downloads/Spire-PDF-NET.html