简体   繁体   中英

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM