GetImages.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Text
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Drawing
  11.  
  12. '' This sample loads a PDF (we use the document generated by the Wetlands sample)
  13. '' and extracts all images from it. It then prints those images into a new PDF,
  14. '' centered one per page.
  15. Public Class GetImages
  16. Function CreatePDF(ByVal stream As Stream) As Integer
  17. Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 12}
  18. Using fs = New FileStream(Path.Combine("Resources", "PDFs", "Wetlands.pdf"), FileMode.Open, FileAccess.Read)
  19. Dim docSrc = New GcPdfDocument()
  20. '' Load an existing PDF with some images:
  21. docSrc.Load(fs)
  22. '' This call extracts information about images from the loaded PDF,
  23. '' note that for large files it may take a while to complete:
  24. Dim imageInfos = docSrc.GetImages()
  25.  
  26. Dim doc = New GcPdfDocument()
  27. Dim textPt = New PointF(72, 72)
  28. Dim imageRc = New RectangleF(72, 72 * 2, doc.PageSize.Width - 72 * 2, doc.PageSize.Height - 72 * 3)
  29.  
  30. For Each imageInfo In imageInfos
  31. '' The same image may appear on multiple locations,
  32. '' imageInfo includes page indices and locations on pages
  33. '' for simplicity sake we only print page numbers here:
  34. Dim sb = New StringBuilder()
  35. imageInfo.Locations.ForEach(Function(il_) sb.Append((il_.Page.Index + 1).ToString() + ", "))
  36. Dim g = doc.NewPage().Graphics
  37. g.DrawString($"This image appears on page(s) {sb.ToString().TrimEnd(" "c, ","c)} of the original PDF:", tf, New PointF(72, 72))
  38. g.DrawImage(imageInfo.Image, imageRc, Nothing, ImageAlign.ScaleImage)
  39. Next
  40. '' Done:
  41. doc.Save(stream)
  42. Return doc.Pages.Count
  43. End Using
  44. End Function
  45. End Class
  46.