打印Pdf文档C#中字体生成的条形码

时间:2015-09-25 15:16:35

标签: c# .net pdf printing

我有小应用程序,我执行pdf文档打印。 一切都很好,除了从字体生成条形码的文件,这部分页面看起来变形(条形码条中间的条形码文本)。 有没有人知道为什么会这样,有什么方法可以解决这个问题吗?

我的代码:

public static void PrindDocument(string filePath, PrinterSetting printerSetting, int copies)
        {

            SpirePdf.PdfDocument doc = new SpirePdf.PdfDocument();
            doc.LoadFromFile(filePath);
            PrintDialog dialogPrint = new PrintDialog();
            dialogPrint.AllowPrintToFile = true;
            dialogPrint.AllowSomePages = true;
            dialogPrint.PrinterSettings.MinimumPage = 1;
            dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
            dialogPrint.PrinterSettings.FromPage = 1;
            dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
            dialogPrint.PrinterSettings.Copies = (short)copies;

            var paperSize = dialogPrint.PrinterSettings.PaperSizes.Cast<PaperSize>().FirstOrDefault(e => e.PaperName == printerSetting.Pageformat);
            dialogPrint.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
            doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
            doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
            doc.PrinterName = printerSetting.Printer;
            PrintDocument printDoc = doc.PrintDocument;
            printDoc.DefaultPageSettings.PaperSize = paperSize;
            printDoc.PrinterSettings.Copies = (short)copies;
            dialogPrint.Document = printDoc;
            printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();
            printDoc.Print();

        }

我尝试了另一种方法,但该方法完全相同:

private void SendToPrinter()
    {
        ProcessStartInfo info = new ProcessStartInfo();
        info.Verb = "print";
        info.FileName = @"c:\output.pdf";
        info.CreateNoWindow = true;
        info.WindowStyle = ProcessWindowStyle.Hidden;

        Process p = new Process();
        p.StartInfo = info;
        p.Start();

        p.WaitForInputIdle();
        System.Threading.Thread.Sleep(3000);
        if (false == p.CloseMainWindow())
            p.Kill();
    }

可以在此处找到Pdf文件示例: https://www.dropbox.com/sh/7vhnyji10f4ekx3/AACI2XtG3PoiHzGzoJNbH_k7a?dl=0

另外我想提一下,当我通过普通方式打印此文件时,例如使用谷歌浏览器打印并打印,看起来不错。

1 个答案:

答案 0 :(得分:3)

使用Spire,

    private void buttonSpire_Click(object sender, EventArgs e)
    {
        PdfDocument doc = new PdfDocument();
        doc.LoadFromFile(filename);

        PrintDocument(doc, printername, 1);
    }
    private void PrintDocument(PdfDocument doc, string printername, short copies)
    {
        PrintDialog dialogPrint = new PrintDialog();
        dialogPrint.AllowPrintToFile = true;
        dialogPrint.AllowSomePages = true;
        dialogPrint.PrinterSettings.MinimumPage = 1;
        dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
        dialogPrint.PrinterSettings.FromPage = 1;
        dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
        dialogPrint.PrinterSettings.Copies = copies;

        var paperSize = dialogPrint.PrinterSettings.PaperSizes.Cast<PaperSize>().FirstOrDefault(e => e.PaperName == "Letter");
        dialogPrint.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;

        doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
        doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
        doc.PrinterName = printername;

        PrintDocument printDoc = doc.PrintDocument;
        printDoc.DefaultPageSettings.PaperSize = paperSize;
        printDoc.PrinterSettings.Copies = copies;
        dialogPrint.Document = printDoc;

        printDoc.PrintController = new StandardPrintController();
        printDoc.Print();
    }

条形码的问题再现:整个条形码和文字向下移动,多个字段也会丢失粗体文字,

enter image description here

但如果我把Spire从图片中删除,那么这样做:

    private void buttonOther_Click(object sender, EventArgs e)
    {
        PrintDocument(filename);
    }
    private void PrintDocument(string filename)
    {
        ProcessStartInfo info = new ProcessStartInfo();
        info.Verb = "print";
        info.FileName = filename;

        Process p = new Process();
        p.StartInfo = info;
        p.Start();
    }

问题消失了: enter image description here

我的机器上也安装了Adobe Acrobat。因此,我猜测,一旦Spire不在图片中,Adobe Acrobat默认接管并且一切正常。现在,如果你只拥有Spire,那么你就会坚持下去。尝试安装Acrobat,看看你得到了什么。

顺便说一句,他们在自己的样本中使用Adobe: enter image description here