c#使用iTextSharp

时间:2016-07-05 13:25:39

标签: c# pdf printing itext

我在打印pdf时遇到问题。 所以我的文档是通过在pdf文档中编写一些值并保存它来创建的

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

    }

所以现在我想用这个函数打印文档,该函数也调用CreateDocument函数。它打印,但纸是白色的。我检查了创建的pdf是否正在创建,并且它正在创建但显然没有打印。

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

1 个答案:

答案 0 :(得分:0)

确保您的文件已完全创建..否则您将遇到此问题。快速测试文件创建和打印之间的延迟