2011-09-08 67 views
2

我听说ITextSharp不支持JAVA2D类,这是否意味着我不能从客户端数据库导入矢量点以“打印”到ITextSharp应用程序?带有ITextSharp的矢量图形

在继续讨论这个建议之前,我会很乐意找到答案。 任何人都有这方面的真实经历?

回答

2

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

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是一个光栅/位图对象,所以在这种情况下它不会帮助你。

+0

非常感谢您提出这样一个很好的例子并回答我的问题。这将使我的工作更轻松!我想我现在会使用ITextSharp来进行即将到来的项目。/R –