I’m having some grief with page sizing when generating a PDF.
My idea is that applicants may upload their results from prior educational establishments for us to file - I’ve got it pretty much working, the code happily converts png/jpeg format images to PDFs. For ease of filing, I’m bundling up any they upload within 28 days of each other.
My issue is that the sizing of the pages are inconsistent. I’ve got this VB codeblock controlling the logic.
Protected Sub ConvertAndSaveToDB()
If PreviousPDFExists() Then
SaveToDB(fileData:=CombinePDFs(pdf1:=GetPreviousPDF(), pdf2:=GetNewFileAsPDF()))
Else
SaveToDB(fileData:=GetNewFileAsPDF())
End If
End Sub
Protected Function CombinePDFs(pdf1 As Aspose.Pdf.Document, pdf2 As Aspose.Pdf.Document) As Aspose.Pdf.Document
Dim toReturn As New Aspose.Pdf.Document()
For Each page As Aspose.Pdf.Page In pdf1.Pages
toReturn.Pages.Add(page)
Next
For Each page As Aspose.Pdf.Page In pdf2.Pages
toReturn.Pages.Add(page)
Next
Return toReturn
End Function
Protected Function GetNewFileAsPDF() As Aspose.Pdf.Document
Dim fileInfo As New PdfFileInfo(fuPhoto.FileContent)
If fileInfo.IsPdfFile Then
Using memStream As New MemoryStream()
fuPhoto.FileContent.Position = 0
fuPhoto.FileContent.CopyTo(memStream)
Return New Aspose.Pdf.Document(memStream)
End Using
Else
'Using memStream As New MemoryStream()
Dim imagePdf As New Aspose.Pdf.Document()
Dim page As Aspose.Pdf.Page = imagePdf.Pages.Add()
Using bmp As New Bitmap(fuPhoto.FileContent)
Dim imageRectangle As New Aspose.Pdf.Rectangle(0, 0, bmp.Width, bmp.Height)
page.CropBox = imageRectangle
page.MediaBox = imageRectangle
page.TrimBox = imageRectangle
page.BleedBox = imageRectangle
page.ArtBox = imageRectangle
page.Resources.Images.Add(fuPhoto.FileContent)
page.Contents.Add(New Aspose.Pdf.Operator.GSave())
Dim matrix As New Matrix(bmp.Width, 0, 0, bmp.Height, 0, 0)
page.Contents.Add(New Aspose.Pdf.Operator.ConcatenateMatrix(matrix))
End Using
Dim xImg As Aspose.Pdf.XImage = page.Resources.Images(page.Resources.Images.Count)
page.Contents.Add(New Aspose.Pdf.Operator.Do(xImg.Name))
page.Contents.Add(New Aspose.Pdf.Operator.GRestore())
'imagePdf.Save(memStream)
Return imagePdf
'End Using
End If
End Function
Currently this project is using version 6.9.0.0 of the Aspose.PDF package, but when using this png to test against: Screenshot 2021-07-22 112733.png (9.8 KB)
It results in this PDF (if used 4 times independantly): Results_Upload_(from_Student).PDF (242.7 KB)
Surely, however, the subsequent pages should also be the size of the first - considering they all acquire the same page content?