PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Jul 10, 2024 4:30 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Jun 15, 2011 11:36 am 
Offline

Joined: Wed Jun 15, 2011 11:30 am
Posts: 3
Hi Guys,
Can some body help me in exporting Gridview/Dataset to PDF in both web/win applications.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 24, 2011 7:09 am 
Offline

Joined: Wed Jun 15, 2011 11:30 am
Posts: 3
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 34 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group