How to open a PDF, without saving it ?, using iTextSharp, and C #

4

I am working with iTextSharp. He used a function to generate a PDF file, and save it, with the following piece of code:

//GENERAR ARCHIVO PDF.
    private void To_pdf()
    {
        Document doc = new Document(PageSize.A4.Rotate(), 10, 10, 10, 10);
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.InitialDirectory = @"C:";
        saveFileDialog1.Title = "Guardar Reporte";
        saveFileDialog1.DefaultExt = "pdf";
        saveFileDialog1.Filter = "pdf Files (*.pdf)|*.pdf| All Files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;
        string filename = "";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            filename = saveFileDialog1.FileName;
        }

        if (filename.Trim() != "")
        {
            FileStream file = new FileStream(filename,
            FileMode.OpenOrCreate,
            FileAccess.ReadWrite,
            FileShare.ReadWrite);
            PdfWriter.GetInstance(doc, file);
            doc.Open();
            string remito = "ARCHIVO PDF DE PRUEBA";
            string envio = "Fecha:" + DateTime.Now.ToString();

            //AGERGAR UNA IMÁGEN.
            iTextSharp.text.Image imagen = iTextSharp.text.Image.GetInstance("C:/Users/ABG/Desktop/SistemaNóminaABG/Imágenes/grupoabg.png");
            Chunk chunk = new Chunk("Reporte de General bitacora", FontFactory.GetFont("ARIAL", 20, iTextSharp.text.Font.BOLD));
            doc.Add(new Paragraph(chunk));
            doc.Add(new Paragraph("------------------------------------------------------------------------------------------"));
            doc.Add(imagen);
            doc.Add(new Paragraph(remito));
            doc.Add(new Paragraph(envio));
            //GenerarDocumento(doc);
            doc.AddCreationDate();
            doc.Add(new Paragraph("______________________________________________", FontFactory.GetFont("ARIAL", 20, iTextSharp.text.Font.BOLD)));
            doc.Add(new Paragraph("Firma", FontFactory.GetFont("ARIAL", 20, iTextSharp.text.Font.BOLD)));
            doc.Close();
            Process.Start(filename);//Esta parte se puede omitir, si solo se desea guardar el archivo, y que este no se ejecute al instante
        }

    }

And then what I do is show it in a FORM of my program, calling the path of the file.

What I want to know is ..., How do I show the PDF file in the form, without having to save it ?, that is, when it is created directly, it opens.

I thank you.

    
asked by Patricia Velazquez 04.09.2018 в 20:50
source

1 answer

3

FileStream herada de System.IO.Stream , so in instead of sending a FileStream to the method PdfWriter.GetInstance(); , try sending a MemoryStream since FileStream requires creating the file on disk:

// ...
var ms = new System.IO.MemoryStream();
PdfWriter.GetInstance(doc, ms);
doc.Open();
//...

Then when you finish generating and closing the document, you get the bytes of the pdf using the method MemoryStream#ToArray() :

byte[] pdfBytes = ms.ToArray();

Code:

private void To_pdf()
{
    //...

    if (filename.Trim() != "")
    {
        MemoryStream file = new MemoryStream();
        PdfWriter.GetInstance(doc, file);
        doc.Open();
        string remito = "ARCHIVO PDF DE PRUEBA";
        string envio = "Fecha:" + DateTime.Now.ToString();

        //..
        doc.Close();

        // obtienes los datos del pdf generado
        var pdfBytes = file.ToArray();


    }

}
    
answered by 05.09.2018 в 14:28