GetInstance() public static method

public static GetInstance ( Document document, Stream os ) : PdfWriter
document Document
os Stream
return PdfWriter
Beispiel #1
0
        protected void BtnSavePDF_Click(object sender, EventArgs e)
        {
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            using (MemoryStream stream = new MemoryStream())
            {
                Alumnos.SaveImage(stream, ChartImageFormat.Png);
                iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
                chartImage.ScalePercent(75f);
                pdfDoc.Add(chartImage);
                pdfDoc.Close();

                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
        }
Beispiel #2
0
        private Boolean AñadirDatos()
        {
            Document  doc    = new Document(PageSize.LETTER);
            PdfWriter writer = null;
            // Indicamos donde vamos a guardar el documento
            int numero = 0;

            for (int i = 0; i <= numero; i++)
            {
                if (System.IO.File.Exists("C:/Users/vichg/OneDrive/Escritorio/ControlAlumnos" + numero + ".pdf"))
                {
                    numero++;
                }
                else
                {
                    writer = PdfWriter.GetInstance(doc,
                                                   new FileStream(@"C:\Users\vichg\OneDrive\Escritorio\ControlAlumnos" + numero + ".pdf", FileMode.Create));
                }
            }

            // Le colocamos el título y el autor
            // **Nota: Esto no será visible en el documento
            doc.AddTitle("Lista de Alumnos ");
            doc.AddCreator("Luis Enrique García Rodríguez y Victor Hugo Gonzales Arreola");

            // Abrimos el archivo
            doc.Open();

            iTextSharp.text.Font _standardFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

            // Escribimos el encabezamiento en el documento
            doc.Add(new Paragraph("         Lista de ALumnos 2019 Clase Desarrollo Asp.NET"));
            doc.Add(Chunk.NEWLINE);

            // Creamos una tabla que contendrá el nombre, apellido y país
            // de nuestros visitante.
            PdfPTable tblPrueba = new PdfPTable(5);

            tblPrueba.WidthPercentage = 100;

            // Configuramos el título de las columnas de la tabla
            PdfPCell clControl = new PdfPCell(new Phrase("Numero Control", _standardFont));

            clControl.BorderWidth       = 0;
            clControl.BorderWidthBottom = 0.50f;

            PdfPCell clNombre = new PdfPCell(new Phrase("Nombre Alumno", _standardFont));

            clNombre.BorderWidth       = 0;
            clNombre.BorderWidthBottom = 0.50f;

            PdfPCell clCarrera = new PdfPCell(new Phrase("Carrera", _standardFont));

            clCarrera.BorderWidth       = 0;
            clCarrera.BorderWidthBottom = 0.50f;

            PdfPCell clSemestre = new PdfPCell(new Phrase("Semestre", _standardFont));

            clSemestre.BorderWidth       = 0;
            clSemestre.BorderWidthBottom = 0.50f;

            PdfPCell clCorreo = new PdfPCell(new Phrase("Correo", _standardFont));

            clCorreo.BorderWidth       = 0;
            clCorreo.BorderWidthBottom = 0.50f;


            // Añadimos las celdas a la tabla
            tblPrueba.AddCell(clControl);
            tblPrueba.AddCell(clNombre);
            tblPrueba.AddCell(clCarrera);
            tblPrueba.AddCell(clSemestre);
            tblPrueba.AddCell(clCorreo);

            int iPosicion = 0;

            foreach (GridViewRow row in GrdAlumnos.Rows)
            {
                doc.Add(Chunk.NEWLINE);
                iPosicion++;

                // Llenamos la tabla con información
                clControl             = new PdfPCell(new Phrase(row.Cells[0].Text, _standardFont));
                clControl.BorderWidth = 0;

                clNombre             = new PdfPCell(new Phrase(row.Cells[1].Text, _standardFont));
                clNombre.BorderWidth = 0;

                clCarrera             = new PdfPCell(new Phrase(row.Cells[2].Text, _standardFont));
                clCarrera.BorderWidth = 0;

                clSemestre             = new PdfPCell(new Phrase(row.Cells[3].Text, _standardFont));
                clSemestre.BorderWidth = 0;

                clCorreo             = new PdfPCell(new Phrase(row.Cells[4].Text, _standardFont));
                clCorreo.BorderWidth = 0;

                // Añadimos las celdas a la tabla
                tblPrueba.AddCell(clControl);
                tblPrueba.AddCell(clNombre);
                tblPrueba.AddCell(clCarrera);
                tblPrueba.AddCell(clSemestre);
                tblPrueba.AddCell(clCorreo);
            }
            doc.Add(tblPrueba);

            doc.Close();
            writer.Close();
            return(true);
        }