Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Printing a PDF with iTextSharp

I have a problem with printing a pdf. So my Document is being created by writing some values in a pdf document and saving it

public void CreateDocument(string name)
    {

        string oldreport = @"..\Resources\FehlerReport.pdf";
        string newreportpath = @"..\Resources\" + name;
        using (var newFileStream = new FileStream(newreportpath, FileMode.Create))
        {
            var pdfReader = new PdfReader(oldreport);

            var stamper = new PdfStamper(pdfReader, newFileStream);

            var form = stamper.AcroFields;

            var fieldKeys = form.Fields.Keys;

            form.SetField("Auftragsnummer", Kundeninformation.Auftragsnummer.ToString());
            form.SetField("Kundensachnummer", Kundeninformation.Kundensachnummer.ToString());
            form.SetField("Kundenname", Kundeninformation.Kundenname.ToString());
            form.SetField("Kundenbestellnummer", Kundeninformation.Kundenbestellnummer.ToString());
            form.SetField("Kundenrezepturnummer", Kundeninformation.Kundenrezepturnummer.ToString());
            form.SetField("Spulennummer", Kundeninformation.Spulennummer.ToString());

            form.SetField("Fertigungsdatum1", System.DateTime.Today.ToString("dd.MM.yy"));

            int i = 1;
            foreach (var item in _MeasurementReport.MeasurementReportItems)
            {
                form.SetField(("UhrzeitRow" + i).ToString(), item.Uhrzeit.ToString("HH:mm:ss"));
                form.SetField(("FehlerindexRow" + i).ToString(), i.ToString());
                form.SetField(("Position mmRow" + i).ToString(), (item.Laufmeter * pixelSize).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture));
                form.SetField(("HoeheRow" + i).ToString(), (item.DefectCountours.H * pixelSize).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture));
                form.SetField(("Breite mmRow" + i).ToString(), (item.DefectCountours.W * pixelSize).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture));
                form.SetField(("Flaeche Row" + i).ToString(), (item.DefectCountours.W * pixelSize * pixelSize).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture));
                i++;
            }

            form.SetField("Datum", System.DateTime.Today.ToString("dd.MM.yy"));
            form.SetField("Uhrzeit", System.DateTime.Now.ToString("HH:mm"));

            stamper.FormFlattening = true;


            stamper.Close();
            pdfReader.Close();
        }

    }

So now i want to print the document with this function, which also calls the CreateDocument function. It prints, but the paper is white. I checked if the created pdf is being created, and it is being created but apparently not printed.

    public void Print()
        {

            string name = Kundeninformation.Auftragsnummer + "_" + Kundeninformation.Spulennummer+".pdf";
            CreateDocument(name);
            List<string> PrinterFound = new List<string>();
            PrinterSettings printer = new PrinterSettings();
            foreach (var item in PrinterSettings.InstalledPrinters)
            {
                PrinterFound.Add(item.ToString());
            }
            printer.PrinterName = PrinterFound[7];
            printer.PrintFileName = name;

            PrintDocument PrintDoc = new PrintDocument();

            PrintDoc.DocumentName = @"..\Resources\"+name;
            PrintDoc.PrinterSettings.PrinterName = PrinterFound[7];

            PrintDoc.Print();
}
like image 651
atix Avatar asked Nov 21 '22 01:11

atix


1 Answers

make sure your file is created completely.. otherwise you will this issue. to test quickly put some delay between file creation and printing

like image 153
Prabhu Avatar answered Dec 18 '22 15:12

Prabhu