Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3934

PdfDocument setting to landscape based on the image width

$
0
0

Xojo 2024r4.1 on Windows 11
I have some code below that takes a bunch of images that are mostly text in landscape mode and makes a new pdfdocument. It is a loop that grabs each image the user has selected. The code checks if the image width is greater that the height and sets the pdf to landscape if it is. The problem is that every other page comes out as portrait even though the images are landscape. I have tried each image alone and it comes out as landscape like it should. Any idea what I am doing wrong?

// Parse the page numbers from the text box
// Create a variable with an array for the page numbers
var pagenums() as string
var str as string
str = TfPageNums.text.ReplaceAll(" ", "")
pagenums = str.ToArray(",")

// Create a new PDFDocument, multipage if more than one page is selected so do a loop with nextpage
Var pdf As New PDFDocument
Var g As Graphics = pdf.Graphics

// Load the image
for i as integer = 0 to pagenums.Count -1
  newpng = SpecialFolder.SharedApplicationData.child("data").Child("tmpextract").child("turing-" + pagenums(i) + ".png")
  Var myImage As Picture = Picture.Open(newpng)
  
  // Check for landscape
  if myimage.Width > myimage.Height then
    pdf.Landscape = true
  else
    pdf.Landscape = false
  end if
  
  // Draw the image onto the PDF page 
  g.DrawPicture(myImage, 0, 0, g.Width, g.Height, 0, 0, myimage.Width, myimage.height)
  
  //Move to next page if not the last image
  if i <> pagenums.Count -1 then
    g.NextPage //moves to next page
  end if
next i

// Save the PDF to a file in the AP folder
Var pdfFile As FolderItem = SpecialFolder.Desktop.Child("output.pdf")
pdf.Save(pdfFile)

2 posts - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 3934

Trending Articles