# Spliting PDF and get thumbnail while adding it to a ListView

I'm doing a application to work with PDF and TIFF files. To divide them, the user opens the file or folder (if it is a folder, it gets all the files inside) and the app splits the file, getting a tiny image (thumbnail) in JPG format. Then it creates a ListViewItem adding the thumbnail, the name and the size of the file.

But it takes too long (1:40min) with a PDF with 68 pages, so if a larger PDF is opened, it will take too long.

So I wish someone could say where can I improve my code so it doesn't take that long.

## CODE

public void SplitPDF2(string input)
{
try
{
string name = Path.GetFileNameWithoutExtension(input);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
using (MemoryStream ms = new MemoryStream())
{
//get thumbnail from 1st page-----------------------------------------------------
{
var image = document.Render(i - 1, 60, 84, 300, 300, true);
image.Save(ms, ImageFormat.Jpeg);
image.Dispose();
}
//split pdf-----------------------------------------------------------------------
string p = AppDomain.CurrentDomain.BaseDirectory + "temporario\\" + name + " - Pagina " + i + ".PDF";
using (Stream outputStream = new FileStream(p, FileMode.Create))
{
Document doc = new Document();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, outputStream);
doc.Open();
PdfContentByte pdfContentByte = pdfWriter.DirectContent;

doc.SetPageSize(mediabox);
doc.NewPage();

doc.Close();
doc.Dispose();
pdfWriter.Dispose();
}
//in the end it will add the thumbnail, the name and the size of the file to the ListView
FileInfo f = new FileInfo(p);
ListViewItem _item1 = new ListViewItem();
_item1.ImageKey = j.ToString(); //image
_item1.SubItems.Add(name + " - Pagina " + i + ".PDF"); //name

j++;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
}
}

• Why are you saving the images on the disk and not working with them in memory? You seem to mix a couple of principles there. using and Dispose at the same time, no using in other cases... quite a mess. – t3chb0t Apr 29 '19 at 8:40
• How should I do it using the memory? Using a MemoryStream? @t3chb0t – Sofia Rodrigues Apr 29 '19 at 8:43
• @t3chb0t Could you help me organizing and "unmessing" my code? – Sofia Rodrigues Apr 29 '19 at 10:19
• Welcome! You might want to take a look at profiling: docs.microsoft.com/en-us/visualstudio/profiling/?view=vs-2019 – user50167 Nov 12 '19 at 16:43

This code leaves a bunch of open questions, minor ones:
- SplitPDF2(string input) what has been/happened to SplitPDF() or SplitPDF1()?
- what is lista2?

Non-minor:

• Document/comment your source code. In the code.
• Preparing thumbnails and splitting a PDF into separate-file-per-page do neither sound nor look related:
Do not put/implement both in a single procedure
• I can't seem to see a purpose in saving each thumbnail rendered into a MemoryStream ms that doesn't get used otherwise.
• You instantiate one and only one iTextSharp.text.pdf.PdfReader(input) for each input:
Do the same for PdfiumViewer.PdfDocument
• it will add the thumbnail I don't see that.

Overall, I'm left mistrusting writing file-per-page PDF to be a useful step in accomplishing the larger task at hand.
wish someone could say where [to improve the] code so it doesn't take that long my crystal ball is dull, but profiling/timeit() might help.

• Thank you for your answer and I’m no longer working with that... since I am portuguese, some variables can be strange to you, such as “lista2”, my appologies. If I remember it well, the other SplitPDF() functions were with another libraries. I’m sure that who uses your answer to resolve their problems, it will help them. – Sofia Rodrigues Nov 13 '19 at 16:08