矢量图形与ITextSharp [英] Vector graphics with ITextSharp

查看:143
本文介绍了矢量图形与ITextSharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说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?

推荐答案

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

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

编辑

顺便提一下,虽然您不能使用JAVA2D(显然是Java并且不能与.Net一起使用),但您可以使用标准系统创建iTextSharp图像.Drawing.Image class并将其传递给 iTextSharp.text.Image.GetInstance()静态方法。不幸的是 System.Drawing.Image 是一个栅格/位图对象,因此在这种情况下它不会帮助你。

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.

这篇关于矢量图形与ITextSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆