Archive

Archive for the ‘Imaging’ Category

Checking PDF page color depth using ABCpdf SDK

October 28, 2010 Leave a comment

If you are converting PDF documents to TIFF image files, you may want to convert each page based on the color content of each PDF page.  Using the ABCpdf SDK, you can check each page and output each PDF page as color or bitonal to the TIFF format.

You can check out the ABCpdf SDK at http://www.websupergoo.com/abcpdf-1.htm


Public Shared Function ConvertPDFToTIFbyColor(ByVal _sourceFilename As String, ByVal _targetFilename As String, ByVal _TestForm As frmTest, Optional ByVal _ToColor As Boolean = False) As Integer
 Try

 Using theDoc As Doc = New Doc()
Dim _objectID As Integer = 0
 Dim _isColor As Boolean = False

 theDoc.Read(_sourceFilename)

 Dim pdfPageCount As Integer = theDoc.PageCount
 Dim i As Integer

 ' loop through the pages
 For i = 1 To pdfPageCount
 _isColor = False

 theDoc.PageNumber = i

 Dim _svg As String = ""
 'This function allows you to extract the content from a page.
 'There are four formats supported - "Text", "SVG", "SVG+" and "SVG+2".
 _svg = theDoc.GetText("SVG+")

 Dim xmlPDF As New XDocument
 xmlPDF = XDocument.Parse(_svg, LoadOptions.PreserveWhitespace)

 ' run Linq query to get <image> from the SVG+ nodes
 Dim q = From p In xmlPDF.<SVG:svg>.<SVG:image> _
 Select xValue = p

 For Each p In q
 ' check for objectID attribute
 If p.@pdf_ObjectID.Count > 0 Then
 _objectID = CInt(p.@pdf_ObjectID)
 ' if found, cast to PixMap
 Dim _pixmap As PixMap = CType(theDoc.ObjectSoup(_objectID), PixMap)

 ' check color by checking ColorSpace
 Select Case _pixmap.ColorSpace.ColorSpaceType
 Case ColorSpaceType.CalGray
 Case ColorSpaceType.CalRGB
 Case ColorSpaceType.DeviceCMYK
 Case ColorSpaceType.DeviceGray
 'bitonal
 Case ColorSpaceType.DeviceN
 Case ColorSpaceType.DeviceRGB
 'color
 _isColor = True
 Case ColorSpaceType.ICCBased
 Case ColorSpaceType.Lab
 Case ColorSpaceType.None
 Case ColorSpaceType.Pattern
 Case ColorSpaceType.Separation
 Case ColorSpaceType.Separation

 End Select
 End If
 Next

 If _isColor Then
 ' set up the rendering parameters
 theDoc.Rendering.ColorSpace = "RGB"
 theDoc.Rendering.BitsPerChannel = 8
 theDoc.Rendering.DotsPerInchX = 100
 theDoc.Rendering.DotsPerInchY = 100
 theDoc.Rendering.AntiAliasImages = True
 theDoc.Rendering.AutoRotate = True
 theDoc.Rendering.SaveQuality = 100
 theDoc.Rendering.SaveCompression = XRendering.Compression.LZW
 Else
' set up the rendering parameters
 theDoc.Rendering.ColorSpace = "Grey"
 theDoc.Rendering.BitsPerChannel = 1
 theDoc.Rendering.DotsPerInchX = 200
 theDoc.Rendering.DotsPerInchY = 200
 theDoc.Rendering.AntiAliasImages = True
 theDoc.Rendering.AutoRotate = True
 theDoc.Rendering.SaveQuality = 100
 theDoc.Rendering.SaveCompression = XRendering.Compression.G4
 End If

 theDoc.Rect.String = theDoc.CropBox.String
 theDoc.Rendering.SaveAppend = True '(i <> 1)
 theDoc.Rendering.Save(_targetFilename)
 Application.DoEvents()

 Next

 theDoc.Clear()

 ConvertPDFToTIFbyColor = pdfPageCount
 End Using

 Catch ex As Exception
 Throw ex

 End Try
 End Function

Advertisements
Categories: Imaging, VB.NET Tags: , , , ,