Resize PDF and keep Hyperlinks

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Resize PDF and keep Hyperlinks

maso
Hi,

I'm using the following code to resize some PDFs to A4 format:


Dim reader As PdfReader = New PdfReader(filePath)
Dim aPage As PdfImportedPage
Dim content As PdfContentByte
Dim aDocument As Document
Dim pageFormat As Rectangle
Dim pageNr As Integer
Dim counter As Integer

pageNr = reader.NumberOfPages

aDocument = New Document(PageSize.A4)

pageFormat = reader.GetPageSize(1)
If pageFormat.Width > pageFormat.Height Then
    'horizontal, rotate page
    aDocument.SetPageSize(PageSize.A4.Rotate())
End If

Dim writer = PdfWriter.GetInstance(aDocument, New FileStream(tmpFilePath, FileMode.Create))
aDocument.Open()
content = writer.DirectContent

While (counter < pageNr)
    counter += 1

    aDocument.NewPage()
    aPage = writer.GetImportedPage(reader, counter)

    If pageFormat.Height > pageFormat.Width Then
        'vertical
        content.AddTemplate(aPage, PageSize.A4.Height / reader.GetPageSize(counter).Height, 0, 0, PageSize.A4.Width / reader.GetPageSize(counter).Width, 0, 0)
    Else
        'horizontal
        content.AddTemplate(aPage, PageSize.A4.Height / reader.GetPageSize(counter).Width, 0, 0, PageSize.A4.Width / reader.GetPageSize(counter).Height, 0, 0)
    End If

End While

aDocument.Close()
writer.Close()
reader.Close()
aDocument.Dispose()
writer.Dispose()
reader.Dispose()


The problem is, that after the resize, the links from the original PDF are lost.
Is there a way resize a PDF without losing the links?