PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

LOOKING FOR EXPORT TO PDF USING PDF-SHARP IN WEB/WIN APP
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1692
Page 1 of 1

Author:  balaji [ Wed Jun 15, 2011 11:36 am ]
Post subject:  LOOKING FOR EXPORT TO PDF USING PDF-SHARP IN WEB/WIN APP

Hi Guys,
Can some body help me in exporting Gridview/Dataset to PDF in both web/win applications.

Author:  balaji [ Fri Jun 24, 2011 7:09 am ]
Post subject:  Re: LOOKING FOR EXPORT TO PDF USING PDF-SHARP IN WEB/WIN APP

Hi Guys,

I am able to EXPORT TO PDF USING PDF-SHARP IN WIN app in 4.0 using WPF DATAGRID Control.

private void CreateImage(DataGrid dtGrid,int j)
{
// Specify a "currently active folder"
string activeDir = @ConfigurationManager.AppSettings["EXPORTFOLDERPATH"];
// Create the folder
System.IO.Directory.CreateDirectory(activeDir);

string sFilePath = ConfigurationManager.AppSettings["IMAGEPATH"] + j + ".bmp";

RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)dtGrid.ActualWidth, (int)dtGrid.ActualHeight, 90d, 90d, PixelFormats.Pbgra32);
renderBitmap.Render(dtGrid);

using (FileStream outStream = new FileStream(sFilePath, FileMode.Create))
{
// Use png encoder for our data
PngBitmapEncoder encoder = new PngBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(outStream);
}
}

private void ExportToMultiPages_Click(object sender, RoutedEventArgs e)
{
int pageSize = 19;
DataTable tmpTable = new DataTable();
int i = 0; int j = 1; bool hasdata = false;
int totalRecords = dt.Rows.Count; tmpTable = dt.Clone();
for (i = 0; i < totalRecords; i++)
{
tmpTable.ImportRow(dt.Rows[i]);
hasdata = true;
if(i == (pageSize * j)) // (19 == (19 * 1))
{
//Binding temptable data to Grid for creating Image.
ExportGrid.DataContext = tmpTable.DefaultView;
ExportGrid.UpdateLayout();
CreateImage(ExportGrid,j);
tmpTable.Dispose();
tmpTable.Clear();
j++;
}
}

if (hasdata)
{
ExportGrid.DataContext = tmpTable.DefaultView;
ExportGrid.UpdateLayout();
tmpTable.Dispose();
CreateImage(ExportGrid,j);
}

string[] filepath = Directory.GetFiles(@ConfigurationManager.AppSettings["EXPORTFOLDERPATH"] + "\\" , "*.bmp");
ExportToPDFforMultiplePages(filepath);
}

public void ExportToPDFforMultiplePages(string[] filepath)
{
PdfDocument _PdfDocument = new PdfDocument();
_PdfDocument.Info.Title = "Report";

//Looping through all the .bmp files.
foreach (string path in filepath)
{
PdfPage _PdfPage = _PdfDocument.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(_PdfPage);
BitmapImage objOPBitmapImage = new BitmapImage(new Uri(path));
BitmapSource image = BitmapImageToBitmapSource(objOPBitmapImage);
XImage xIMage = XImage.FromBitmapSource(image);

_PdfPage.Width = xIMage.PointWidth;
_PdfPage.Height = xIMage.PointHeight;
gfx.DrawImage(xIMage, 5, 0, _PdfPage.Width, _PdfPage.Height);

gfx.Dispose();
xIMage.Dispose();
_PdfPage = null;
image = null;
objOPBitmapImage = null;
}

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "PDF FILE|*.pdf";
saveFileDialog1.Title = "Save a PDF File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
_PdfDocument.Save(saveFileDialog1.FileName);

//string ExportReport = ConfigurationManager.AppSettings["EXPORTTOPDFREPORT"];
//if (!string.IsNullOrEmpty(ExportReport))
// _PdfDocument.Save(ExportReport);

GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);

//Deleteting all created .bmp files.
string imgFolder = @ConfigurationManager.AppSettings["EXPORTFOLDERPATH"] + "\\";
string[] imgList = Directory.GetFiles(imgFolder, "*.bmp");
foreach (string img in imgList)
File.Delete(img);
}

private static BitmapSource BitmapImageToBitmapSource(BitmapImage bitmapImage)
{
return (BitmapSource)bitmapImage;
}

Regards,

Balaji.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/