使用 ITextSharp 绘制矢量图形

发布于 2024-12-03 15:38:30 字数 131 浏览 1 评论 0原文

我听说 ITextSharp 不支持 JAVA2D 类,这是否意味着我无法从客户端数据库导入向量点以“打印”到 ITextSharp 应用程序?

在进一步提出这个建议之前,我真的很想找到这个问题的答案。 有人有这方面的真实经历吗?

I have heard that ITextSharp does not have support for the JAVA2D class, does this mean that i cant import vector points from a clients databas to "print" to a ITextSharp application?

I would relly like to find answer to this before going further with this suggestion.
Anyone have real experiences of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-12-10 15:38:30

虽然您确实无法将 JAVA2D 与 iTextSharp 一起使用,但您仍然可以通过直接写入 PdfWriter.DirectContent 对象,以 PDF 本机方式绘制矢量图形。它支持您期望从矢量绘图中获得的所有标准 MoveTo()LineTo()CurveTo() 等方法程序。下面是一个针对 iTextSharp 5.1.1.0 的完整工作 VB.Net WinForms 应用程序,展示了一些简单的用途。

Option Explicit On
Option Strict On

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim OutputFile As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "VectorTest.pdf")

        Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
            Using Doc As New Document(PageSize.LETTER)
                Using writer = PdfWriter.GetInstance(Doc, FS)
                    ''//Open the PDF for writing
                    Doc.Open()

                    Dim cb As PdfContentByte = writer.DirectContent

                    ''//Save the current state so that we can restore it later. This is not required but it makes it easier to undo things later
                    cb.SaveState()

                    ''//Draw a line with a bunch of options set
                    cb.MoveTo(100, 100)
                    cb.LineTo(500, 500)
                    cb.SetRGBColorStroke(255, 0, 0)
                    cb.SetLineWidth(5)
                    cb.SetLineDash(10, 10, 20)
                    cb.SetLineCap(PdfContentByte.LINE_CAP_ROUND)
                    cb.Stroke()

                    ''//This undoes any of the colors, widths, etc that we did since the last SaveState
                    cb.RestoreState()

                    ''//Draw a circle
                    cb.SaveState()
                    cb.Circle(200, 500, 50)
                    cb.SetRGBColorStroke(0, 255, 0)
                    cb.Stroke()

                    ''//Draw a bezier curve
                    cb.RestoreState()
                    cb.MoveTo(100, 300)
                    cb.CurveTo(140, 160, 300, 300)
                    cb.SetRGBColorStroke(0, 0, 255)
                    cb.Stroke()

                    ''//Close the PDF
                    Doc.Close()
                End Using
            End Using
        End Using
    End Sub
End Class

编辑

顺便说一句,虽然你不能使用JAVA2D(这显然是Java并且不能与.Net一起工作),但你可以使用标准的System.Drawing.Image 类并将其传递给 iTextSharp.text.Image.GetInstance() 静态方法。不幸的是,System.Drawing.Image是一个光栅/位图对象,因此在这种情况下它对您没有帮助。

While it is true that you can't use JAVA2D with iTextSharp you can still draw vector graphics in a PDF-native way by writing directly to the PdfWriter.DirectContent object. It supports all of the standard MoveTo(), LineTo(), CurveTo(), etc methods that you'd expect from a vector drawing program. Below is a full-working VB.Net WinForms app targeting iTextSharp 5.1.1.0 the shows off some simple uses.

Option Explicit On
Option Strict On

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim OutputFile As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "VectorTest.pdf")

        Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
            Using Doc As New Document(PageSize.LETTER)
                Using writer = PdfWriter.GetInstance(Doc, FS)
                    ''//Open the PDF for writing
                    Doc.Open()

                    Dim cb As PdfContentByte = writer.DirectContent

                    ''//Save the current state so that we can restore it later. This is not required but it makes it easier to undo things later
                    cb.SaveState()

                    ''//Draw a line with a bunch of options set
                    cb.MoveTo(100, 100)
                    cb.LineTo(500, 500)
                    cb.SetRGBColorStroke(255, 0, 0)
                    cb.SetLineWidth(5)
                    cb.SetLineDash(10, 10, 20)
                    cb.SetLineCap(PdfContentByte.LINE_CAP_ROUND)
                    cb.Stroke()

                    ''//This undoes any of the colors, widths, etc that we did since the last SaveState
                    cb.RestoreState()

                    ''//Draw a circle
                    cb.SaveState()
                    cb.Circle(200, 500, 50)
                    cb.SetRGBColorStroke(0, 255, 0)
                    cb.Stroke()

                    ''//Draw a bezier curve
                    cb.RestoreState()
                    cb.MoveTo(100, 300)
                    cb.CurveTo(140, 160, 300, 300)
                    cb.SetRGBColorStroke(0, 0, 255)
                    cb.Stroke()

                    ''//Close the PDF
                    Doc.Close()
                End Using
            End Using
        End Using
    End Sub
End Class

EDIT

Incidentally, although you can't use JAVA2D (which is obviously Java and wouldn't work with .Net) you can create iTextSharp images using the standard System.Drawing.Image class and passing it to iTextSharp.text.Image.GetInstance() static method. Unfortunately System.Drawing.Image is a raster/bitmap object so it won't help you in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文