Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Pdf document Barcode generated by font in C#

I have small app where i perform pdf documents printing. Everything is ok except files in which barcode is generated from font, this parts of page looks deformed (barcode text in middle of barcode bars). Does anybody know why this happens , any approach to fix this?

My code:

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

        }

I have tried another method but that method is doing exactly same thing :

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 file example can be found here: https://www.dropbox.com/sh/7vhnyji10f4ekx3/AACI2XtG3PoiHzGzoJNbH_k7a?dl=0

Also I would like to mention that when I print this file thru normal way like open with google chrome and print , it look ok.

like image 531
Nic Avatar asked Sep 25 '15 15:09

Nic


1 Answers

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

The problem with the bar code reproduces: the whole bar code and text shifts down, and also multiple fields lose Bold text,

enter image description here

But if i get Spire out of the picture, doing this:

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

The problem goes away: enter image description here

I also have Adobe Acrobat installed in my machine. So, I'm guessing that, once Spire is out of the picture, Adobe Acrobat, by default, takes over and everything works fine. Now, if all you have is only Spire, then you're stuck with it. Try installing Acrobat and see what you get.

Btw, they use Adobe in their own samples: enter image description here

like image 75
jsanalytics Avatar answered Oct 22 '22 19:10

jsanalytics