C
C#15mo ago
Laz

❔ How to fix distortion? also how to fix page cut off? also using Using PDFSharp

I have a 21 tabs in winform and a few with scrollable wheels, they are getting cut off, please help.
3 Replies
Laz
LazOP15mo ago
private void button30_Click(object sender, EventArgs e)
{
SaveAllTabsAsPDF();
}
private void SaveAllTabsAsPDF() // by taurius litvinavicius
{

//https://stackoverflow.com/questions/30867703/load-all-user-controls-of-a-form-on-start
// to force cycle and load every single tabpage in order to make them momentarily active to be saved
for (int i = 1; i < Main_TabPage.TabPages.Count; i++)
{
Main_TabPage.SelectedIndex = i;
}
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

//need to create a new pdf document so = , creates new var by incoking the class
PdfSharp.Pdf.PdfDocument pdfDocument = new PdfSharp.Pdf.PdfDocument();

//need to add a page so the information can be stored inside so =
// newPdfPage = pdfDocument.AddPage();

//need to creation class variable for providing drawing related Methods/Functions for the unique page created so =
// XGraphics gfx = XGraphics.FromPdfPage(newPdfPage);
//basically allows to "draw" on the pdf from the winform


foreach (TabPage tab in Main_TabPage.TabPages)
{
{
PdfPage newPdfPage = pdfDocument.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(newPdfPage);
private void button30_Click(object sender, EventArgs e)
{
SaveAllTabsAsPDF();
}
private void SaveAllTabsAsPDF() // by taurius litvinavicius
{

//https://stackoverflow.com/questions/30867703/load-all-user-controls-of-a-form-on-start
// to force cycle and load every single tabpage in order to make them momentarily active to be saved
for (int i = 1; i < Main_TabPage.TabPages.Count; i++)
{
Main_TabPage.SelectedIndex = i;
}
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

//need to create a new pdf document so = , creates new var by incoking the class
PdfSharp.Pdf.PdfDocument pdfDocument = new PdfSharp.Pdf.PdfDocument();

//need to add a page so the information can be stored inside so =
// newPdfPage = pdfDocument.AddPage();

//need to creation class variable for providing drawing related Methods/Functions for the unique page created so =
// XGraphics gfx = XGraphics.FromPdfPage(newPdfPage);
//basically allows to "draw" on the pdf from the winform


foreach (TabPage tab in Main_TabPage.TabPages)
{
{
PdfPage newPdfPage = pdfDocument.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(newPdfPage);
//Ensure that the pdfPage printed is always PORTRAIT so =
newPdfPage.Orientation = PageOrientation.Portrait;
newPdfPage.Size = PageSize.A4;


/*int baseX = 500;
int baseY = 1000;
double height = XUnit.FromMillimeter(baseY + 10).Point;
newPdfPage.CropBox = new PdfRectangle(new XPoint(0, newPdfPage.Height - height),
new XSize(newPdfPage.Width, height));
*/

// Capture the content of the tab as an image
using (Bitmap tabImage = new Bitmap(tab.Width, tab.Height))
{
tab.DrawToBitmap(tabImage, new System.Drawing.Rectangle(0, 0, tab.Width, tab.Height));

// Draw the captured image directly onto the PDF page
XImage image = XImage.FromGdiPlusImage(tabImage);
// https://stackoverflow.com/questions/61874041/how-to-set-format-of-pdf-by-pdfsharp-set-pagesize-to-a4-and-fit-on-page-shrink
// this 595,842 always prints the captured image as an A4 picture
gfx.DrawImage(image, 0, 0, 595, 802);
// the above command makes uploads the image as sized pdf, however it is slightly distorted
}
}
}
// Get the user's desktop path
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

// Specify the save path for the PDF
string savePath = System.IO.Path.Combine(desktopPath, "ETTV CALC PDF.pdf");

// Save the PDF to the desktop
pdfDocument.Save(savePath);

MessageBox.Show("Tabs saved as a single PDF on the desktop.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//Ensure that the pdfPage printed is always PORTRAIT so =
newPdfPage.Orientation = PageOrientation.Portrait;
newPdfPage.Size = PageSize.A4;


/*int baseX = 500;
int baseY = 1000;
double height = XUnit.FromMillimeter(baseY + 10).Point;
newPdfPage.CropBox = new PdfRectangle(new XPoint(0, newPdfPage.Height - height),
new XSize(newPdfPage.Width, height));
*/

// Capture the content of the tab as an image
using (Bitmap tabImage = new Bitmap(tab.Width, tab.Height))
{
tab.DrawToBitmap(tabImage, new System.Drawing.Rectangle(0, 0, tab.Width, tab.Height));

// Draw the captured image directly onto the PDF page
XImage image = XImage.FromGdiPlusImage(tabImage);
// https://stackoverflow.com/questions/61874041/how-to-set-format-of-pdf-by-pdfsharp-set-pagesize-to-a4-and-fit-on-page-shrink
// this 595,842 always prints the captured image as an A4 picture
gfx.DrawImage(image, 0, 0, 595, 802);
// the above command makes uploads the image as sized pdf, however it is slightly distorted
}
}
}
// Get the user's desktop path
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

// Specify the save path for the PDF
string savePath = System.IO.Path.Combine(desktopPath, "ETTV CALC PDF.pdf");

// Save the PDF to the desktop
pdfDocument.Save(savePath);

MessageBox.Show("Tabs saved as a single PDF on the desktop.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Bailey
Bailey15mo ago
Hi, Without seeing the distortion it's quite difficult to say anything about it.. But maybe you have something with this info: 1) save the picture to a file and check if it is also distorted (just to check where the cause is). 2) keep in mind, pdf stores his pictures in 150 DPI this can also cause the problem
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?