1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- Imports ComPDFKit.PDFDocument
- Imports ComPDFKit.PDFPage
- Imports System
- Imports System.Drawing
- Imports System.Drawing.Imaging
- Imports System.IO
- Imports System.Windows
- Imports System.Windows.Media
- Imports System.Windows.Media.Imaging
- Module PDFToImageTest
- Private parentPath As String =
- Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())))
- Private outputPath As String = Path.Combine(parentPath, "Output", "VB")
- Sub Main(args As String())
- Console.WriteLine("Running PDFToImage test sample..." & vbCrLf)
- SDKLicenseHelper.LicenseVerify()
- Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
- If Not Directory.Exists(outputPath) Then
- Directory.CreateDirectory(outputPath)
- End If
- PDFPageToImage(document)
- Console.WriteLine("--------------------")
- Console.WriteLine("Done!")
- Console.WriteLine("--------------------")
- Console.ReadLine()
- End Sub
- Private Sub SaveWriteableBitmapAsPng(writeableBitmap As WriteableBitmap, fileName As String)
- Using stream As New FileStream(fileName, FileMode.Create)
- Dim encoder As New PngBitmapEncoder()
- encoder.Frames.Add(BitmapFrame.Create(writeableBitmap))
- encoder.Save(stream)
- End Using
- End Sub
- ' Convert PDF to image
- Private Function PDFPageToImage(document As CPDFDocument) As Boolean
- For i As Integer = 0 To document.PageCount - 1
- Dim pdfPage As CPDFPage = document.PageAtIndex(i, True)
- Dim pageSize As Windows.Size = document.GetPageSize(0)
- Dim pageRect As New Rect(0, 0, CInt(pageSize.Width / 72.0 * 96), CInt(pageSize.Height / 72.0 * 96))
- Dim bmpData As Byte() = New Byte(CInt(pageRect.Width * pageRect.Height * (96 / 72.0) * (96 / 72.0) * 4) - 1) {}
- pdfPage.RenderPageBitmapWithMatrix(CSng(96 / 72.0), pageRect, Convert.ToUInt32(&HFFFFFFF), bmpData, 0, True)
- Dim writeableBitmap As New WriteableBitmap(CInt(pageRect.Width), CInt(pageRect.Height), 96, 96, PixelFormats.Bgra32, Nothing)
- writeableBitmap.WritePixels(New Int32Rect(0, 0, CInt(pageRect.Width), CInt(pageRect.Height)), bmpData, writeableBitmap.BackBufferStride, 0)
- Dim filePath As String = Path.Combine(outputPath, "PDFToImageTest" & i & ".png")
- SaveWriteableBitmapAsPng(writeableBitmap, filePath)
- Console.WriteLine("Png image saved in " & filePath)
- Next
- Return False
- End Function
- End Module
|