0
I’m trying to add the thumbnail, the name and size of a file to a ListView
. I can add everything correctly except the size!
I’m making Split
PDF and I will add the information of each page to the ListView
.
The values that appear in the "Size" are 0 or wrong value, which I think is the file name size, not the file size itself.
I will show my code and the differences between giving the value 0 or wrong value.
VALUE 0:
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(input);
iTextSharp.text.pdf.PdfReader.unethicalreading = true;
string name = Path.GetFileNameWithoutExtension(input);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
//get thumbnail from 1st page-----------------------------------------------------
string pastapdf = AppDomain.CurrentDomain.BaseDirectory + "temporario\\Thumbnails";
Directory.CreateDirectory(pastapdf);
using (var document = PdfiumViewer.PdfDocument.Load(input))
{
var image = document.Render(i-1, 60, 84, 300, 300, true);
image.Save(pastapdf + "\\" + name + "Pagina" + i + "_thumbnail" + ".jpg", ImageFormat.Jpeg);
thumbnails.Images.Add(j.ToString(), image);
image.Dispose();
document.Dispose();
}
//split pdf-----------------------------------------------------------------------
string p = AppDomain.CurrentDomain.BaseDirectory + "temporario\\" + name + " - Pagina " + i + ".PDF";
Stream outputStream = new FileStream(p, FileMode.Create);
Document doc = new Document();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, outputStream);
doc.Open();
PdfContentByte pdfContentByte = pdfWriter.DirectContent;
PdfImportedPage importedpage = pdfWriter.GetImportedPage(reader, i);
iTextSharp.text.Rectangle mediabox = reader.GetPageSize(i);
doc.SetPageSize(mediabox);
doc.NewPage();
pdfContentByte.AddTemplate(importedpage, 0, 0);
FileInfo f = new FileInfo(p);
ListViewItem _item1 = new ListViewItem();
_item1.ImageKey = j.ToString();
_item1.Text = name + " - Pagina " + i + ".PDF";
_item1.SubItems.Add(f.Length.ToString()); //diferença está aui
lista2.Items.Add(_item1);
outputStream.Flush();
doc.Close();
doc.Dispose();
outputStream.Close();
pdfWriter.Dispose();
j++;
WRONG VALUE:
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(input);
iTextSharp.text.pdf.PdfReader.unethicalreading = true;
string name = Path.GetFileNameWithoutExtension(input);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
//get thumbnail from 1st page-----------------------------------------------------
string pastapdf = AppDomain.CurrentDomain.BaseDirectory + "temporario\\Thumbnails";
Directory.CreateDirectory(pastapdf);
using (var document = PdfiumViewer.PdfDocument.Load(input))
{
var image = document.Render(i-1, 60, 84, 300, 300, true);
image.Save(pastapdf + "\\" + name + "Pagina" + i + "_thumbnail" + ".jpg", ImageFormat.Jpeg);
thumbnails.Images.Add(j.ToString(), image);
image.Dispose();
document.Dispose();
}
//split pdf-----------------------------------------------------------------------
string p = AppDomain.CurrentDomain.BaseDirectory + "temporario\\" + name + " - Pagina " + i + ".PDF";
Stream outputStream = new FileStream(p, FileMode.Create);
Document doc = new Document();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, outputStream);
doc.Open();
PdfContentByte pdfContentByte = pdfWriter.DirectContent;
PdfImportedPage importedpage = pdfWriter.GetImportedPage(reader, i);
iTextSharp.text.Rectangle mediabox = reader.GetPageSize(i);
doc.SetPageSize(mediabox);
doc.NewPage();
pdfContentByte.AddTemplate(importedpage, 0, 0);
FileInfo f = new FileInfo(p);
ListViewItem _item1 = new ListViewItem();
_item1.ImageKey = j.ToString();
_item1.Text = name + " - Pagina " + i + ".PDF";
_item1.SubItems.Add(p.Length.ToString()); //diferença está aqui
lista2.Items.Add(_item1);
outputStream.Flush();
doc.Close();
doc.Dispose();
outputStream.Close();
pdfWriter.Dispose();
j++;
From now on, thank you!
Thank you for the reply @Leandro, but put yourself
f.Length.ToString()
always returns 0 and not the actual file size.– Sofia Rodrigues
@Sofiarodrigues See the answer edition
– Leandro Angelo
Now that you’ve edited the code, it’s all become clearer and it’s really about shutting down the stream and the Rites before! Thank you so much!
– Sofia Rodrigues