Y
Yash Pisat
Guest
Yash Pisat Asks: How do I export VB form that also has a multi-tab form to PDF
I have a form "Form1" and a mulitab form "Tabs". There is a next button on Form1, after cliking on that button, multitab form opens. On the last tab of multitab form, I have save button. After clicking on that, PDF is generated. Problem is , 13 pages pdf is generated and it captures the Form1 and last tab of the multitab form. I want to capture the first 11 tabs of the Tabs form too.
I have a form "Form1" and a mulitab form "Tabs". There is a next button on Form1, after cliking on that button, multitab form opens. On the last tab of multitab form, I have save button. After clicking on that, PDF is generated. Problem is , 13 pages pdf is generated and it captures the Form1 and last tab of the multitab form. I want to capture the first 11 tabs of the Tabs form too.
Code:
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Threading
Imports iText.IO.Image
Imports iText.Kernel.Pdf
Imports iText.Layout
Public Class Form1 Private form1Screenshot As Image
Private Sub createCustomerButton_Click(sender As Object, e As EventArgs) Handles Button1.Click
CaptureAndSaveScreenshot()
Dim tabsForm As New Tabs
AddHandler tabsForm.SaveButtonClick, AddressOf Tabs_SaveButtonClickHandler
tabsForm.Show()
End Sub
Private Sub CaptureAndSaveScreenshot()
' Capture screenshot of Form1
form1Screenshot = New Bitmap(Me.Width, Me.Height)
Using graphics As Graphics = Graphics.FromImage(form1Screenshot)
graphics.CopyFromScreen(Me.PointToScreen(Point.Empty), Point.Empty, Me.Size)
End Using
End Sub
Private Function CaptureTabsScreenshots() As List(Of Image)
Dim screenshots As New List(Of Image)()
' Create an instance of the Tabs form
Using tabsForm As New Tabs
'tabsForm.AddTabPages() ' Add tab pages to the Tabs form
tabsForm.Activate()
' Iterate over each tab in the Tabs form
For i As Integer = 0 To tabsForm.TabControl.TabCount - 1
Dim tabPage As TabPage = tabsForm.TabControl.TabPages(i)
' Select the tab and refresh the TabControl
tabsForm.TabControl.SelectedTab = tabPage
tabsForm.TabControl.Refresh()
' Print the name of the selected tab
Dim selectedTabName As String = tabPage.Text
MessageBox.Show("Selected Tab: " & selectedTabName)
' Capture the screenshot of the selected tab
Dim tabScreenshot As Image = New Bitmap(tabsForm.TabControl.Width, tabsForm.TabControl.Height)
Using graphics As Graphics = Graphics.FromImage(tabScreenshot)
graphics.CopyFromScreen(tabsForm.TabControl.SelectedTab.PointToScreen(Point.Empty), Point.Empty, tabsForm.TabControl.SelectedTab.Size)
End Using
screenshots.Add(tabScreenshot)
Next
' Print the total tab count
Dim tabCount As Integer = tabsForm.TabControl.TabCount
MessageBox.Show("Total Tab Count: " & tabCount.ToString())
End Using
Return screenshots
End Function
Private Sub CreatePdfFromScreenshots(screenshots As List(Of Image), outputPath As String)
Using pdfWriter As PdfWriter = New PdfWriter(outputPath)
Using pdfDocument As PdfDocument = New PdfDocument(pdfWriter)
Using document As Document = New Document(pdfDocument)
For Each screenshot As Image In screenshots
Using memoryStream As New MemoryStream()
screenshot.Save(memoryStream, ImageFormat.Png)
Dim imageData As ImageData = ImageDataFactory.Create(memoryStream.ToArray())
Dim pdfImage As iText.Layout.Element.Image = New iText.Layout.Element.Image(imageData)
document.Add(pdfImage)
End Using
Next
document.Close()
End Using
End Using
End Using
End Sub
Private Sub Tabs_SaveButtonClickHandler(sender As Object, e As EventArgs)
Dim screenshots As List(Of Image) = CaptureTabsScreenshots()
Dim outputPath As String = "C:\Work\Form\output.pdf" ' Replace with your desired output path
' Add the screenshot of Form1 to the list
screenshots.Insert(0, form1Screenshot)
CreatePdfFromScreenshots(screenshots, outputPath)
For i As Integer = 1 To screenshots.Count - 1
Dim screenshot As Image = screenshots(i)
Dim fileName As String = "C:\Work\Form\screenshot" & (i + 1).ToString() & ".jpg"
screenshot.Save(fileName, Imaging.ImageFormat.Jpeg)
Next
'MessageBox.Show("Screenshots saved as individual JPG files.")
MessageBox.Show("Screenshots saved as PDF.")
End Sub
End Class
Public Class Tabs Public TabCtrl As New TabControl
Public Delegate Sub SaveButtonClickHandler(sender As Object, e As EventArgs)
Public Event SaveButtonClick As SaveButtonClickHandler
Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles Button1.Click
RaiseEvent SaveButtonClick(Me, EventArgs.Empty)
End Sub
Private Sub Tabs_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class