Fileinfo.Length gives wrong size

Asked

Viewed 31 times

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++;

Valor 0

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++;

Valor errado

From now on, thank you!

1 answer

2


You are measuring the string size and not the file size, the problem is not in the FileInfo.Length but yes in your code.

Now if the FileInfo.Length is returning 0 bytes to you, it is because you still have the stream open and have not persisted their contents on disk.

As you presented in the second block:

string p = AppDomain.CurrentDomain.BaseDirectory + "temporario\\" + name + " - Pagina " + i + ".PDF";
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

FileInfo.Length will return the file size in bytes since the content has already been written in the files. Close streams and Writes before checking them again.

outputStream.Flush();
doc.Close();
doc.Dispose();
outputStream.Close();
pdfWriter.Dispose();

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()); //a diferença estava aqui
  • Thank you for the reply @Leandro, but put yourself f.Length.ToString() always returns 0 and not the actual file size.

  • 1

    @Sofiarodrigues See the answer edition

  • 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.