**Buenas, supongamos que tengo un certificado como este (obviamente bien colorido, con mil detalles, impreso en una hoja especial):
Creditos correspondientes a su autor link
Lo que busco, es poner ese certificado en la impresora y mediante mi aplicación de C#, setear datos mediante una consulta a SQL Server
que realizo obteniendo el valor numérico de una celda en un DataGridView
Este es mi código para llenar con datos el pdf, actualizado en base a la respuesta que medieron:
private void datos48(bool enviarAImpresora = false, string nombreImpresora = "")
{
Certificados certificados = new Certificados();
const int nroColumna = 1;
certificados.DniNumero = 0;
foreach (DataGridViewRow columna in dtgSuscriptor.Rows)
{
certificados.DniNumero = Convert.ToDouble(columna.Cells[nroColumna].Value);
}
PdfReader oReader = new PdfReader(pathPDF);
iTextSharp.text.Rectangle oSize = new iTextSharp.text.Rectangle(1500, 1800);
Document oDocument = new Document(oSize);
string guid = Guid.NewGuid().ToString();
pathPDFCompleto = enviarAImpresora ? $"archivoTemporal{guid}.pdf" : pathPDFCompleto;
FileStream oFS = new FileStream(pathPDFCompleto, FileMode.Create, FileAccess.Write);
PdfWriter oWriter = PdfWriter.GetInstance(oDocument, oFS);
oDocument.Open();
PdfContentByte oPDF = oWriter.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
oPDF.SetColorFill(BaseColor.BLACK);
oPDF.SetFontAndSize(bf, 22);
oPDF.BeginText();
oPDF.SetTextMatrix(210, oSize.Height - 213);
oPDF.ShowText("Franco Ejemplo");
oPDF.SetTextMatrix(160, oSize.Height - 243);
oPDF.ShowText("Domicilio ejempolo");
oPDF.SetTextMatrix(410, oSize.Height - 243);
oPDF.ShowText("localidad");
oPDF.SetTextMatrix(160, oSize.Height - 265);
oPDF.ShowText("provincia");
oPDF.EndText();
PdfImportedPage page = oWriter.GetImportedPage(oReader, 1);
oDocument.NewPage();
oDocument.Close();
oFS.Close();
oWriter.Close();
oReader.Close();
if (enviarAImpresora)
{
Imprimir(pathPDFCompleto, nombreImpresora);
}
}
private void Imprimir(string archivoTemporal, string nombreImpresora)
{
try
{
ProcessStartInfo gsProcessInfo;
Process gsProcess;
gsProcessInfo = new ProcessStartInfo();
gsProcessInfo.Verb = "PrintTo";
gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
gsProcessInfo.FileName = archivoTemporal;
gsProcessInfo.Arguments = "\"" + nombreImpresora + "\"";
gsProcess = Process.Start(gsProcessInfo);
if (gsProcess.HasExited == false)
{
gsProcess.Kill();
}
gsProcess.EnableRaisingEvents = true;
gsProcess.Close();
}
catch (Exception)
{
}
finally
{
File.Delete(archivoTemporal);
}
}
EL código, no me ha funcionado puesto que no manda a imprimir nada. Se lo asigno al botón en el caso true
:
private void btnImprimir_Click(object sender, EventArgs e)
{
datos48(true, "Brother MFC-8460N USB Printer");
}
No sucede nada:
Y en el caso de asignar false:
private void btnImprimir_Click(object sender, EventArgs e)
{
datos48(false, "Brother MFC-8460N USB Printer");
}
No sucede nada:
¿Alguien sabe como puedo lograr lo que digo? Sino me di a entender, me avisan