Class PdfContentRendererExample
''' <summary>
''' Shows how manage rendering of PDF page, i.e. render PDF page without images,
''' render PDF page without text, etc
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
Public Shared Sub TestCustomContentRenderer(pdfFilename As String)
' output TIFF file name format string
Dim outputFileName As String = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(pdfFilename), System.IO.Path.GetFileNameWithoutExtension(pdfFilename))
outputFileName = outputFileName & "_{0}.tif"
' custom renderer
Dim renderer As CustomContentRenderer
' create image collection
Using images As New Vintasoft.Imaging.ImageCollection()
' open source PDF file
images.Add(pdfFilename)
' render PDF pages without images
renderer = New CustomContentRenderer()
renderer.DrawImages = False
RenderPdfUseContentRenderer(images, String.Format(outputFileName, "withoutImages"), renderer)
' render PDF pages and invert rendered images
renderer = New CustomContentRenderer()
renderer.ImageProcessing = New Vintasoft.Imaging.ImageProcessing.Color.InvertCommand()
RenderPdfUseContentRenderer(images, String.Format(outputFileName, "invertImages"), renderer)
' render PDF pages without path filling
renderer = New CustomContentRenderer()
renderer.FillAreaUseShadingPatterns = False
renderer.FillPaths = False
renderer.FillPathsUseShadingPatterns = False
renderer.FillPathsUseTilingPatterns = False
RenderPdfUseContentRenderer(images, String.Format(outputFileName, "withoutFillPath"), renderer)
' render PDF pages without path drawing
renderer = New CustomContentRenderer()
renderer.DrawPaths = False
RenderPdfUseContentRenderer(images, String.Format(outputFileName, "withoutDrawPath"), renderer)
' render PDF pages without text
renderer = New CustomContentRenderer()
renderer.DrawText = False
RenderPdfUseContentRenderer(images, String.Format(outputFileName, "withoutText"), renderer)
' clear image collection and dispose images
images.ClearAndDisposeItems()
End Using
End Sub
''' <summary>
''' Renders the PDF file using specified content renderer.
''' </summary>
''' <param name="images">The images to render.</param>
''' <param name="outputTiffFileName">Name of the output tiff file.</param>
''' <param name="renderer">The renderer.</param>
Public Shared Sub RenderPdfUseContentRenderer(images As Vintasoft.Imaging.ImageCollection, outputTiffFileName As String, renderer As Vintasoft.Imaging.Pdf.PdfContentRenderer)
' create PDF rendering settings
Dim settings As New Vintasoft.Imaging.Codecs.Decoders.PdfRenderingSettings()
' specify the custom PDF content renderer
settings.ContentRenderer = renderer
' specify PDF rendering settings for images (PDF pages)
images.SetRenderingSettings(settings)
' create TIFF encoder
Using encoder As New Vintasoft.Imaging.Codecs.Encoders.TiffEncoder()
' save PDF pages to a TIFF file
images.SaveSync(outputTiffFileName, encoder)
End Using
End Sub
''' <summary>
''' Provides functionality for overriding algorithms of PDF content rendering.
''' </summary>
Public Class CustomContentRenderer
Inherits Vintasoft.Imaging.Pdf.PdfContentRenderer
#Region "Fields"
''' <summary>
''' Determines that string is drawing.
''' </summary>
Private _stringDrawing As Boolean = False
#End Region
#Region "Properties"
Private _drawAnnotations As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must draw
''' annotations on PDF page.
''' </summary>
Public Property DrawAnnotations() As Boolean
Get
Return _drawAnnotations
End Get
Set
_drawAnnotations = value
End Set
End Property
Private _drawForms As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must draw
''' form XObjects on PDF page.
''' </summary>
Public Property DrawForms() As Boolean
Get
Return _drawForms
End Get
Set
_drawForms = value
End Set
End Property
Private _drawImages As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must draw
''' images on PDF page.
''' </summary>
Public Property DrawImages() As Boolean
Get
Return _drawImages
End Get
Set
_drawImages = value
End Set
End Property
Private _drawInlineImages As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must draw
''' inline images on PDF page.
''' </summary>
Public Property DrawInlineImages() As Boolean
Get
Return _drawInlineImages
End Get
Set
_drawInlineImages = value
End Set
End Property
Private _drawPaths As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must draw
''' graphics paths on PDF page.
''' </summary>
Public Property DrawPaths() As Boolean
Get
Return _drawPaths
End Get
Set
_drawPaths = value
End Set
End Property
Private _fillPaths As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must fill
''' graphics paths on PDF page.
''' </summary>
Public Property FillPaths() As Boolean
Get
Return _fillPaths
End Get
Set
_fillPaths = value
End Set
End Property
Private _fillPathsUseTilingPatterns As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must fill
''' graphics paths use tiling patterns on PDF page.
''' </summary>
Public Property FillPathsUseTilingPatterns() As Boolean
Get
Return _fillPathsUseTilingPatterns
End Get
Set
_fillPathsUseTilingPatterns = value
End Set
End Property
Private _fillPathsUseShadingPatterns As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must fill
''' graphics paths use shading patterns on PDF page.
''' </summary>
Public Property FillPathsUseShadingPatterns() As Boolean
Get
Return _fillPathsUseShadingPatterns
End Get
Set
_fillPathsUseShadingPatterns = value
End Set
End Property
Private _fillAreaUseShadingPatterns As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must fill
''' all visible area use tiling patterns on PDF page.
''' </summary>
Public Property FillAreaUseShadingPatterns() As Boolean
Get
Return _fillAreaUseShadingPatterns
End Get
Set
_fillAreaUseShadingPatterns = value
End Set
End Property
Private _drawText As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must draw
''' text on PDF page.
''' </summary>
Public Property DrawText() As Boolean
Get
Return _drawText
End Get
Set
_drawText = value
End Set
End Property
Private _drawInvisibleText As Boolean = False
''' <summary>
''' Gets or sets a value indicating whether the renderer must draw
''' invisible text on PDF page.
''' </summary>
Public Property DrawInvisibleText() As Boolean
Get
Return _drawInvisibleText
End Get
Set
_drawInvisibleText = value
End Set
End Property
Private _setClipPaths As Boolean = True
''' <summary>
''' Gets or sets a value indicating whether the renderer must set
''' clip paths.
''' </summary>
Public Property SetClipPaths() As Boolean
Get
Return _setClipPaths
End Get
Set
_setClipPaths = value
End Set
End Property
Private _linesWeigth As Single = 1
''' <summary>
''' Gets or sets the lines weigth, in percents.
''' </summary>
Public Property LinesWeigth() As Single
Get
Return _linesWeigth
End Get
Set
_linesWeigth = value
End Set
End Property
Private _imageProcessing As Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase = Nothing
''' <summary>
''' Gets or sets the image processing command that apply
''' before an image draws on PDF page.
''' </summary>
Public Property ImageProcessing() As Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase
Get
Return _imageProcessing
End Get
Set
_imageProcessing = value
End Set
End Property
#End Region
#Region "Methods"
''' <summary>
''' Draws a text string.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="charCodes">The codes, in font encoding, of text characters.</param>
Public Overrides Sub DrawString(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, charCodes As ULong())
' if text must be drawn AND text is visible
If _drawText AndAlso context.GraphicsState.TextRenderingMode <> Vintasoft.Imaging.Text.TextRenderingMode.Invisible Then
_stringDrawing = True
MyBase.DrawString(context, charCodes)
_stringDrawing = False
' if invisible text must be drawn AND text is invisible
ElseIf _drawInvisibleText AndAlso context.GraphicsState.TextRenderingMode = Vintasoft.Imaging.Text.TextRenderingMode.Invisible Then
context.GraphicsState.TextRenderingMode = Vintasoft.Imaging.Text.TextRenderingMode.Fill
MyBase.DrawString(context, charCodes)
context.GraphicsState.TextRenderingMode = Vintasoft.Imaging.Text.TextRenderingMode.Invisible
End If
End Sub
''' <summary>
''' Draws path using specified pen.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="path">The path to draw.</param>
''' <param name="pen">The pen to use for drawing path.</param>
Public Overrides Sub DrawPath(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, path As Vintasoft.Imaging.Drawing.IGraphicsPath, pen As Vintasoft.Imaging.Drawing.IDrawingPen)
' if path must be drawn
If (_drawPaths AndAlso Not _stringDrawing) OrElse (_drawText AndAlso _stringDrawing) Then
' if path is not a path of text symbol
If _linesWeigth <> 1 AndAlso Not _stringDrawing Then
Dim oldWidth As Single = pen.Width
pen.Width *= _linesWeigth
MyBase.DrawPath(context, path, pen)
pen.Width = oldWidth
Else
MyBase.DrawPath(context, path, pen)
End If
End If
End Sub
''' <summary>
''' Fills path using specified brush.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="path">The path to fill.</param>
''' <param name="brush">The brush to use for filling path.</param>
Public Overrides Sub FillPath(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, path As Vintasoft.Imaging.Drawing.IGraphicsPath, brush As Vintasoft.Imaging.Drawing.IDrawingBrush)
' if path must be filled
If (_fillPaths AndAlso Not _stringDrawing) OrElse (_drawText AndAlso _stringDrawing) Then
MyBase.FillPath(context, path, brush)
End If
End Sub
''' <summary>
''' Fills path using specified pattern.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="path">The path to fill.</param>
''' <param name="pattern">The pattern to use for filling path.</param>
Public Overrides Sub FillPath(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, path As Vintasoft.Imaging.Drawing.IGraphicsPath, pattern As Vintasoft.Imaging.Pdf.Tree.Patterns.PdfGraphicalPattern)
If Not _fillPathsUseShadingPatterns AndAlso TypeOf pattern Is Vintasoft.Imaging.Pdf.Tree.Patterns.ShadingPattern Then
Return
End If
If Not _fillPathsUseTilingPatterns AndAlso TypeOf pattern Is Vintasoft.Imaging.Pdf.Tree.Patterns.TilingPattern Then
Return
End If
MyBase.FillPath(context, path, pattern)
End Sub
''' <summary>
''' Fills all visible areas using specified shading pattern.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="shadingPattern">The shading pattern.</param>
Public Overrides Sub FillArea(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, shadingPattern As Vintasoft.Imaging.Pdf.Tree.ShadingPatterns.PdfShadingPattern)
If _fillAreaUseShadingPatterns Then
MyBase.FillArea(context, shadingPattern)
End If
End Sub
''' <summary>
''' Draws a form XObject.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="formResource">The form XObject to draw.</param>
Public Overrides Sub DrawFormXObject(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, formResource As Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource)
If _drawForms Then
MyBase.DrawFormXObject(context, formResource)
End If
End Sub
''' <summary>
''' Draws an annotation.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="annotation">The annotation to draw.</param>
''' <param name="appearanceForm">The annotation appearance form.</param>
Public Overrides Sub DrawAnnotation(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, annotation As Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotation, appearanceForm As Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource)
If _drawAnnotations Then
MyBase.DrawAnnotation(context, annotation, appearanceForm)
End If
End Sub
''' <summary>
''' Intersects current clip region with specified clip path.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="clipPath">The clip path.</param>
Public Overrides Sub IntersectClip(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, clipPath As Vintasoft.Imaging.Drawing.IGraphicsPath)
If _setClipPaths Then
MyBase.IntersectClip(context, clipPath)
End If
End Sub
''' <summary>
''' Draws an image resource.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="imageResource">The image XObject resource to draw.</param>
Public Overrides Sub DrawImageResource(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, imageResource As Vintasoft.Imaging.Pdf.Tree.PdfImageResource)
If Not _drawImages AndAlso Not imageResource.IsInline Then
Return
End If
If Not _drawInlineImages AndAlso imageResource.IsInline Then
Return
End If
MyBase.DrawImageResource(context, imageResource)
End Sub
''' <summary>
''' Draws an image.
''' </summary>
''' <param name="context">The rendering context.</param>
''' <param name="image">The image to draw.</param>
''' <param name="points">Array of three System.Drawing.PointF structures that
''' define a parallelogram on rendering content where image must be drawn.</param>
Public Overrides Sub DrawImage(context As Vintasoft.Imaging.Pdf.PdfContentRenderingContext, image As Vintasoft.Imaging.VintasoftImage, points As System.Drawing.PointF())
' if image must be processed
If ImageProcessing IsNot Nothing AndAlso ImageProcessing.IsPixelFormatSupported(image.PixelFormat) Then
SyncLock image
Using tempImage As Vintasoft.Imaging.VintasoftImage = ImageProcessing.Execute(image)
MyBase.DrawImage(context, tempImage, points)
End Using
End SyncLock
Else
MyBase.DrawImage(context, image, points)
End If
End Sub
''' <summary>
''' Creates a new object that is a copy of this instance.
''' </summary>
''' <returns>
''' A new object that is a copy of this instance.
''' </returns>
Public Overrides Function Clone() As Object
Dim result As New CustomContentRenderer()
result._drawAnnotations = _drawAnnotations
result._drawForms = _drawForms
result._drawImages = _drawImages
result._drawInlineImages = _drawInlineImages
result._drawInvisibleText = _drawInvisibleText
result._drawPaths = _drawPaths
result._drawText = _drawText
result._fillAreaUseShadingPatterns = _fillAreaUseShadingPatterns
result._fillPaths = _fillPaths
result._fillPathsUseShadingPatterns = _fillPathsUseShadingPatterns
result._fillPathsUseTilingPatterns = _fillPathsUseTilingPatterns
If _imageProcessing IsNot Nothing Then
result._imageProcessing = DirectCast(_imageProcessing.Clone(), Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase)
End If
result._linesWeigth = _linesWeigth
result._setClipPaths = _setClipPaths
Return result
End Function
#End Region
End Class
End Class