【发布时间】:2011-09-08 07:13:47
【问题描述】:
我听说 ITextSharp 不支持 JAVA2D 类,这是否意味着我无法从客户端数据库导入矢量点以“打印”到 ITextSharp 应用程序?
在进一步提出这个建议之前,我很想找到这个问题的答案。 有人有这方面的真实经历吗?
【问题讨论】:
标签: vector pdf-generation itextsharp itext
我听说 ITextSharp 不支持 JAVA2D 类,这是否意味着我无法从客户端数据库导入矢量点以“打印”到 ITextSharp 应用程序?
在进一步提出这个建议之前,我很想找到这个问题的答案。 有人有这方面的真实经历吗?
【问题讨论】:
标签: vector pdf-generation itextsharp itext
虽然您确实不能将 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 图像并将其传递给 iTextSharp.text.Image.GetInstance() 静态方法。不幸的是,System.Drawing.Image 是一个光栅/位图对象,所以在这种情况下它对您没有帮助。
【讨论】: