I’m using the following code to go through all images in a pdf and convert them to jpeg. inputStream contains the pdf and outputStream returns a pdf with converted images. The weird thing is that all the images are flipped afterwards. Therefore, I have to (counter)flip the images during the conversion process. Anybody who knows why the images are flipped? By the way - if anybody knows of a better way to convert all images in a pdf, then please let me know.
Public Shared Function ConvertPDFToPDF(ByRef inputStream As Stream) As MemoryStream<span style="color: blue; ">Dim</span> pdfEditor <span style="color: blue; ">As</span> <span style="color: rgb(43, 145, 175); ">PdfFileEditor</span> = <span style="color: blue; ">New</span> <span style="color: rgb(43, 145, 175); ">PdfFileEditor</span>() <span style="color: blue; ">Dim</span> outputStream <span style="color: blue; ">As</span> <span style="color: blue; ">New</span> <span style="color: rgb(43, 145, 175); ">MemoryStream</span>() <span style="color: blue; ">Try</span> <span style="color: blue; ">Dim</span> pdfExtractor <span style="color: blue; ">As</span> <span style="color: blue; ">New</span> <span style="color: rgb(43, 145, 175); ">PdfExtractor</span>() pdfExtractor.BindPdf(inputStream) <span style="color: blue; ">Dim</span> imageDescriptionList() <span style="color: blue; ">As</span> <span style="color: rgb(43, 145, 175); ">ImageDescription</span> = pdfExtractor.GetImageDescriptions() <span style="color: blue; ">Dim</span> imageStreams(imageDescriptionList.Count - 1) <span style="color: blue; ">As</span> <span style="color: rgb(43, 145, 175); ">MemoryStream</span> <span style="color: blue; ">Dim</span> imageOutStreams(imageDescriptionList.Count - 1) <span style="color: blue; ">As</span> <span style="color: rgb(43, 145, 175); ">MemoryStream</span> <span style="color: blue; ">Dim</span> i <span style="color: blue; ">As</span> <span style="color: blue; ">Integer</span> = 0 pdfExtractor.ExtractImage() <span style="color: blue; ">While</span> pdfExtractor.HasNextImage() imageStreams(i) = <span style="color: blue; ">New</span> <span style="color: rgb(43, 145, 175); ">MemoryStream</span>() imageOutStreams(i) = <span style="color: blue; ">New</span> <span style="color: rgb(43, 145, 175); ">MemoryStream</span>() pdfExtractor.GetNextImage(imageStreams(i), <span style="color: rgb(43, 145, 175); ">ImageFormat</span>.Jpeg) imageStreams(i).Seek(0, <span style="color: rgb(43, 145, 175); ">SeekOrigin</span>.Begin) <span style="color: blue; ">Using</span> Image <span style="color: blue; ">As</span> System.Drawing.<span style="color: rgb(43, 145, 175); ">Bitmap</span> = System.Drawing.<span style="color: rgb(43, 145, 175); ">Bitmap</span>.FromStream(imageStreams(i)) Image.RotateFlip(System.Drawing.<span style="color: rgb(43, 145, 175); ">RotateFlipType</span>.RotateNoneFlipY) Image.Save(imageOutStreams(i), <span style="color: rgb(43, 145, 175); ">ImageFormat</span>.Jpeg) <span style="color: blue; ">End</span> <font class="Apple-style-span" color="#0000ff">Using
i = i + 1
End While
i = 0
Dim document As New Aspose.Pdf.Document(inputStream)
For Each imageDescription As ImageDescription In imageDescriptionList
document.Pages(imageDescription.Page).Resources.Images.Replace(imageDescription.Index, imageOutStreams(i))
i = i + 1
Next<span style="color:blue;">Using</span> outputStream2 <span style="color:blue;">As</span> <span style="color:blue;">New</span> <span style="color:#2b91af;">MemoryStream</span>() document.Save(outputStream2) pdfEditor.Extract(outputStream2, 0, 9999, outputStream) <span style="color:blue;">End</span> <span style="color:blue;">Using</span> <span style="color:blue;">Catch</span> ex <span style="color:blue;">As</span> <span style="color:#2b91af;">Exception</span> pdfEditor.Extract(inputStream, 0, 9999, outputStream) <span style="color:blue;">End</span> <span style="color:blue;">Try</span> <span style="color:blue;">Return</span> outputStream <span style="color:blue;">End</span> <span style="color:blue;">Function</span>