PDF file doesn't display in the listview c# 2008 - c#

I try to display the PDF files in the Listview as LargeIcon. But it doesn't display.
I can display the all image formats.
How to display the PDF file in the ListView?
I tried like this only, like image display.
private void import(string path)
{
ListView1.Items.Clear();
ImageList imageList = new ImageList();
imageList.Images.Clear();
imageList.ImageSize = new Size(170, 140);
imageList.ColorDepth = ColorDepth.Depth32Bit;
int i = 0;
////ArrayList to hold the files with the certain extensions
ArrayList files1 = new ArrayList();
if (Directory.Exists(path).Equals(true))
{
string validExtensions = "*.pdf";
//create a string array of our filters by plitting the
//string of valid filters on the delimiter
string[] extFilter = validExtensions.Split(new char[] { ',' });
//loop through each extension in the filter
foreach (string extension in extFilter)
{
files1.AddRange(Directory.GetFiles(path, extension));
files = (string[])files1.ToArray(typeof(String));
}
string[] pathes = new string[files.Length];
foreach (string file in files)
{
pathes[i] = file;
i++;
}
foreach (string path1 in pathes)
{
imageList.Images.Add(Bitmap.FromFile(path1));
FileInfo fileInfo = new FileInfo(path1);
String strDir = fileInfo.Name;
ListView1.Items.Add(strDir);
ListView1.TileSize = new System.Drawing.Size(100, 80);
}
for (int j = 0; j < pathes.Length; j++)
{
this.ListView1.Items[j].ImageIndex = j;
}
this.ListView1.View = View.LargeIcon;
this.ListView1.LargeImageList = imageList;
ListView1.Focus();
ListView1.Items[0].Selected = true;
}
}
}

You are going to need to rasterize the PDF. I think ImageMagick (maybe in conjunction with Ghostscript) can do this.
Disclaimer: I work for Atalasoft
We have a .NET library for Rasterizing PDF to an image available here:
http://atalasoft.com/products/dotimage/pdfrasterizer/

You need to find a .Net PDF renderer.
See this question.

Related

Efficiently Convert .xslx to .csv in C#?

As input, I have a set of excel files with several worksheets inside. I need to export a single csv file for each worksheet. Below is my code which works but it is very slow. It builds upon the solutions proposed in this previous post. Please consider that I have to run this on rather big .xlsx files (approx. 300Mb).
QUESTION: Is there any way to improve this?
void Main()
{
string folder = #"\\PATH_TO_FOLDER\";
var files = Directory.GetFiles(folder, "*.xlsx", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
ConvertToCsv(file, Directory.GetParent(file) + #"\\output\");
}
}
public static void ConvertToCsv(string file, string targetFolder)
{
FileInfo finfo = new FileInfo(file);
ExcelPackage package = new ExcelPackage(finfo);
// if targetFolder doesn't exist, create it
if (!Directory.Exists(targetFolder)) {
Directory.CreateDirectory(targetFolder);
}
var worksheets = package.Workbook.Worksheets;
int sheetcount = 0;
foreach (ExcelWorksheet worksheet in worksheets)
{
sheetcount++;
var maxColumnNumber = worksheet.Dimension.End.Column;
var currentRow = new List<string>(maxColumnNumber);
var totalRowCount = worksheet.Dimension.End.Row+1;
var currentRowNum = 1;
//No need for a memory buffer, writing directly to a file
//var memory = new MemoryStream();
string file_name = targetFolder + Path.GetFileNameWithoutExtension(file) + "_" + sheetcount + ".csv";
using (var writer = new StreamWriter(file_name, false, Encoding.UTF8))
{
//the rest of the code remains the same
for (int i = 1; i < totalRowCount; i++)
{
i.Dump();
// populate line with semi columns separators
string line = "";
for (int j = 1; j < worksheet.Dimension.End.Column+1; j++)
{
if (worksheet.Cells[i, j].Value != null)
{
string cell = worksheet.Cells[i, j].Value.ToString() + ";";
line += cell;
}
}
// write line
writer.WriteLine(line);
}
}
}
}

Read more than one file

I am writing a pdf to word converter which works perfectly fine for me. But I want to be able to convert more than one file.
What happens now is that it read the first file and does the convert process.
public static void PdfToImage()
{
try
{
Application application = null;
application = new Application();
var doc = application.Documents.Add();
string path = #"C:\Users\Test\Desktop\pdfToWord\";
foreach (string file in Directory.EnumerateFiles(path, "*.pdf"))
{
using (var document = PdfiumViewer.PdfDocument.Load(file))
{
int pagecount = document.PageCount;
for (int index = 0; index < pagecount; index++)
{
var image = document.Render(index, 200, 200, true);
image.Save(#"C:\Users\chnikos\Desktop\pdfToWord\output" + index.ToString("000") + ".png", ImageFormat.Png);
application.Selection.InlineShapes.AddPicture(#"C:\Users\chnikos\Desktop\pdfToWord\output" + index.ToString("000") + ".png");
}
string getFileName = file.Substring(file.LastIndexOf("\\"));
string getFileWithoutExtras = Regex.Replace(getFileName, #"\\", "");
string getFileWihtoutExtension = Regex.Replace(getFileWithoutExtras, #".pdf", "");
string fileName = #"C:\Users\Test\Desktop\pdfToWord\" + getFileWihtoutExtension;
doc.PageSetup.PaperSize = WdPaperSize.wdPaperA4;
foreach (Microsoft.Office.Interop.Word.InlineShape inline in doc.InlineShapes)
{
if (inline.Height > inline.Width)
{
inline.ScaleWidth = 250;
inline.ScaleHeight = 250;
}
}
doc.PageSetup.TopMargin = 28.29f;
doc.PageSetup.LeftMargin = 28.29f;
doc.PageSetup.RightMargin = 30.29f;
doc.PageSetup.BottomMargin = 28.29f;
application.ActiveDocument.SaveAs(fileName, WdSaveFormat.wdFormatDocument);
doc.Close();
}
}
I thought that with my foreach that problem should not occur. And yes there are more than one pdf in this folder
The line
var doc = application.Documents.Add();
is outside the foreach loop. So you only create a single word document for all your *.pdf files.
Move the above line inside the foreach loop to add a new word document for each *.pdf file.

Out of memory exception when adding images to image list and displaying them into a list view

I am in the process to making an image gallery where i need to load more than 2 GB images from 1 directory to list view.
when i browse a folder above 200 mb its showing me the error OUT OF MEMORY.
my code is
_filenames = new List<string>();
DirectoryInfo dir = new DirectoryInfo(#root + "gallery");
foreach (FileInfo file in dir.GetFiles())
{
if (file.Name != "desktop.ini")
{
var image = Image.FromFile(file.FullName);
_filenames.Add(file.Name.ToLower());
imageList1.Images.Add(image);
}
else
{
break;
}
}
listView1.View = View.LargeIcon;
imageList1.ImageSize = new Size(75,75);
listView1.LargeImageList = imageList1;
for (int i = 0; i < imageList1.Images.Count; i++)
{
var item = new ListViewItem();
item.ImageIndex = i;
item.Text = _filenames[i];
listView1.Items.Add(item);
}
}
You need to (as a first), perform your image operations before adding them to the list:
Performing other operations, such as setting the ColorDepth or
ImageSize will cause the Handle to be recreated. Therefore, you should
perform these operations before you add images to the ImageList.
from MSDN
Update following clarifications:
In order to create your own file list then (assuming you have the directory name and file names within the directory) you could create a FileInfo[] array as follows:
// Prepare the directory and file names
var directoryName = "C:\\Temp\\MyFolder";
var filenames = new List<string>();
filenames.Add("0001.jpg");
filenames.Add("2345.jpg");
// Construct FileInfo array - using System.IO
var files = new FileInfo[filenames.Count];
for (var i = 0; i < filenames.Count; i++)
{
var fileName = filenames[i];
files[i] = new FileInfo(Path.Combine(directoryName, fileName));
}
There are many ways you could construct the FileInfo[] array (e.g. LINQ for example), but the above should work fine.

Save images from listview to a folder

I had this listview and a button. There are also several images inside the listview. I want to save the images from the listview inside the folder when the button is pressed. And I don't know how to do it. Could you please help me out? Thanks. This is the code I use to insert images to the listview.
OpenFileDialog opend1 = new OpenFileDialog();
opend1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
opend1.Multiselect = true;
if (opend1.ShowDialog() == DialogResult.OK)
{
listView1.View = View.LargeIcon;
imageList.ImageSize = new Size(100, 100);
for (int c = 0; c < opend1.FileNames.Length; c++)
{
Image i = Image.FromFile(opend1.FileNames[c].ToString());
Image img = i.GetThumbnailImage(100, 100, null, new IntPtr());
imageList.Images.Add(img);
}
listView1.LargeImageList = imageList;
ListViewItem lstItem = new ListViewItem();
lstItem.ImageIndex = imageList.Images.Count-1;
listView1.Items.Add(lstItem);
listView1.Refresh();
}
For each image in your image list (imageList.Images) call this (with your own supplied directory and file name):
img.Save(#"C:\MyImage.jpg", ImageFormat.Jpeg);
foreach (Image image in listView1.LargeImageList.Images)
{
string filename = ""; // make this whatever you need...
image.Save(filename);
}

Telerik RenderReport

I have some problems with Telerik reports.
Feels like i have missed something...
I wanna create a list of reports, and then write them to ONE file.
But when i write it out i only get one page.
The writer writer over page 1 all foreach, so it just write one page.
But i want several pages... in this case 10.
Have tried write with FileStream, File and more...
Does anyone have a good idea?
public void WriteToFile()
{
string path = #"C:\";
string test = "test";
var report = new Report2();
var procceser = new ReportProcessor();
var list = new List<RenderingResult>();
for (int i = 0; i < 10; i++)
{
var res = procceser.RenderReport("PDF", report, null);
list.Add(res);
}
string filePath = Path.Combine(path, test);
var Writer = new BinaryWriter(File.Create(filePath));
foreach (var renderingResult in list)
{
Writer.Write(renderingResult.DocumentBytes);
}
Writer.Flush();
Writer.Close();
}

Categories