Dim opt As New PdfPageRenderOptions() With {.BitmapEncoderId = Guid.Parse("0131be10-2001-4c5f-a9b0-cc88fab64ce8")}
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim tif As String = "D:\example.tiff" Dim pdf As String = "D:\example.pdf" Using pdfFile As New FileStream(pdf, FileMode.Open, FileAccess.Read), pdfStream = pdfFile.AsRandomAccessStream() Dim pdfDoc = Await PdfDocument.LoadFromStreamAsync(pdfStream) Dim tiffICI As ImageCodecInfo = ImageCodecInfo.GetImageEncoders().First(Function(enc) enc.MimeType = "image/tiff") Dim opt As New PdfPageRenderOptions() With {.BitmapEncoderId = Guid.Parse(WICTiffEncoder)} Using page = pdfDoc.GetPage(0UI), tifFile = File.OpenWrite(tif), tifStream = tifFile.AsRandomAccessStream() Await page.RenderToStreamAsync(tifStream, opt) Await tifStream.FlushAsync() End Using Using tiffImage As New Bitmap(New MemoryStream(File.ReadAllBytes(tif))) '最初のページは MultiFrame で Save する Dim epMultiFrame As New EncoderParameters() With {.Param = {New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.MultiFrame))}} tiffImage.Save(tif, tiffICI, epMultiFrame) For p = 1UI To pdfDoc.PageCount - 1UI Using page = pdfDoc.GetPage(p), pageMemory = New MemoryStream(), tifStream = pageMemory.AsRandomAccessStream() Await page.RenderToStreamAsync(tifStream, opt) Await tifStream.FlushAsync() Using imgStream = tifStream.AsStreamForRead(), pageImage = Image.FromStream(imgStream) '2ページ目以降は FrameDimensionPage を SaveAdd する Dim epFrameDimensionPage As New EncoderParameters() With {.Param = {New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.FrameDimensionPage))}} tiffImage.SaveAdd(pageImage, epFrameDimensionPage) End Using End Using Next ' 全ページを追加したら、Flush を SaveAdd する Dim epFlush As New EncoderParameters() With {.Param = {New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.Flush))}} tiffImage.SaveAdd(epFlush) End Using End Using Process.Start(tif) End Sub