iTextSharp deleting document if it does not have pages

1

Well, I have an application that at one point generates a PDF with all JPG's, however, instead of choosing a JPG I choose an Excel file it just moves that file.

Okay, I'd like that if the document I created with iTextSharp ( Doc.Open() among other properties) if no page was deleted, it was not created, any idea how to do this?

I already know that to know the number of pages is Doc.PageNumber

iTextSharp.text.Document Doc = new iTextSharp.text.Document(PageSize.A1, 20, 20, 20, 20);
            string PDFOutput = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
        PdfWriter writer = PdfWriter.GetInstance(Doc, new FileStream("C:/AgendaComp/Encaminhamento/" + txtNomePasta.Text + SID.ToString() + ".pdf", FileMode.Create, FileAccess.Write, FileShare.Read));

        Doc.Open();

        string Folder = @"C:/AgendaComp/Encaminhamento/" + txtNomePasta.Text + " arquivos/";
        foreach (string F in System.IO.Directory.GetFiles(Folder, "*.jpg"))
        {
            if (System.IO.File.Exists(F))
            {
                Doc.NewPage();

                //Doc.PageSize.Height = 1275;
                Doc.Add(new iTextSharp.text.Jpeg(new Uri(new FileInfo(F).FullName)));
            }
        }
        int n = Doc.PageNumber;
        if (n == 0)
        {
            Doc.Close();

        }
        else
        {
            Doc.Close();
            File.Move("C:/AgendaComp/Encaminhamento/" + txtNomePasta.Text + SID.ToString() + ".pdf", "C:/AgendaComp/Encaminhamento/" + DdataAviso.Year + "-" + DdataAviso.Month + "-" + DdataAviso.Day + "/" + txtNomePasta.Text + SID.ToString() + ".pdf");
        }
    
asked by anonymous 11.06.2015 / 16:50

1 answer

1

To put in memory change the following lines:

PdfWriter writer = PdfWriter.GetInstance(Doc, new FileStream("C:/AgendaComp/Encaminhamento/" + txtNomePasta.Text + SID.ToString() + ".pdf", FileMode.Create, FileAccess.Write, FileShare.Read));

for

PdfWriter writer = PdfWriter.GetInstance(Doc, Response.OutputStream);

and after closing the document

 HttpResponse resp = this.Response;
 resp.ContentType = "application/pdf";
    
29.01.2016 / 10:36