14

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.

Nic
  • 1,088
  • 3
  • 19
  • 43
  • I can't tell if your problem is with the generating of barcodes or with the printing. Do the barcodes look OK in the PDF and just weird when printed? – Chris Haas Sep 25 '15 at 15:22
  • yes they look ok, it's just an invoice generated from amazone. I will try to upload such file in a moment. – Nic Sep 25 '15 at 15:23
  • here is an example of such file where both barcodes are not working after printing.http://www.filedropper.com/test_56 – Nic Sep 25 '15 at 15:24
  • What is `PdfDocument`? Is that [iText](http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text/pdf/PdfDocument.cs) or [Windows](https://msdn.microsoft.com/en-us/library/windows/apps/windows.data.pdf.pdfdocument.aspx)? – Chris Haas Sep 25 '15 at 16:00
  • @ChrisHaas it is from spire pdf library – Nic Sep 25 '15 at 17:08
  • Printing is the same as rendering and viewing, so if something is printing weird the problem is almost always with the renderer. Not all PDF renderers are the same either which is why it might work everywhere. Does Spire have a PDF viewer that you can test this in? – Chris Haas Sep 25 '15 at 18:11
  • @ChrisHaas I think no, they don't,but you can see that I have tested printing with 2 versions and the result is same.so I think the problem is somewhere elese – Nic Sep 25 '15 at 20:29
  • First, to the best of my knowledge, most common printers don't "speak PDF" and instead need something to translate to another language such as PostScript or PCL which leads us back to a renderer issue. Second, a PDF, by its very definition and reason to exist in the first place, _must_ render the same on every device, screen, printer, plotter, microwave, whatever. Your PDF appears to be syntactically correct so if you are getting different results we're still back to the renderer. – Chris Haas Sep 25 '15 at 20:49
  • For your second test using `ProcessStartInfo`, do you know what renderer you are using? You can find out by looking in your registry under `HKCR\.pdf`. Read the Data for the `(Default)` entry (in my case `AcroExch.Document`) and find that entry in `HKCR`. Inside that entry tip open `shell` and then `print` (which is the `verb` you are sending) and then look at the `command`'s value. This will give you the exact command line arguments for what your second example does and tell you which program is responsible for rendering the PDF. – Chris Haas Sep 25 '15 at 20:53
  • @ChrisHaas thanks for your ideas , I will search more in this direction, but I think and about embedded fonts too , I am not sure but I think that it can be a problem. – Nic Sep 25 '15 at 22:57
  • @ChrisHaas and what is also strange , I can't print this document using Adobe reader, I am getting error like document can not be printer, No selected pages.But I can print it from google chrome. – Nic Sep 25 '15 at 22:58
  • Try to convert it to a bitmap image and print it after this step. See some usefull tips here: http://stackoverflow.com/questions/23905169/how-to-convert-pdf-files-to-image – David BS Sep 30 '15 at 00:22
  • @MDDDC the file link seems to have become stale... – mkl Oct 12 '15 at 15:51
  • @DavidBS I have updated it, please check this http://www.filedropper.com/slp4356473272daf3f99-3200-4508-bf47-aa4264d00bec – Nic Oct 12 '15 at 16:46
  • Why don't you contact Spire? In the past I had problems with Spire.Doc and they were able to troubleshoot them quite well. – ovolko Oct 12 '15 at 17:51
  • @ovolko as you can see second method does not use any Spire..but the result is same, so it seems like problem is caused by something else. – Nic Oct 12 '15 at 18:03
  • @MDDDC I tried now to download again the file and I received the message: "the file was not found in server". Shouldn´t be because I´m not a member of that website? Try OneDrive, DropBox or similar. – David BS Oct 14 '15 at 13:21
  • @DavidBS please check here https://www.dropbox.com/sh/7vhnyji10f4ekx3/AACI2XtG3PoiHzGzoJNbH_k7a?dl=0 – Nic Oct 14 '15 at 17:40
  • The Pdf file is in landscape mode. There is nothing that specify that the document has to be printed in landscape mode. Have you tried to force it ? Would you please like to upload a picture of a failed print ? – Larry Oct 14 '15 at 20:10
  • @MDDDC, why don´t you try to make the same as Chrome (sometimes), converting the created PDF to an image and print it after this? Look here: http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-PDF-Page-to-Image-with-C-code.html – David BS Oct 14 '15 at 21:29
  • @Larry yes I have tried to force printing landscape but the result was exactly the same. I will try to upload an image in a moment. – Nic Oct 15 '15 at 07:09
  • @DavidBS I have tried and this but image quality is not good enough,and working with a lot of pdf's I think it can be not best approach, first I would like to understand what is happening now and why I am getting such strange result. – Nic Oct 15 '15 at 07:11
  • @MDDDC Hello, we've encountered a similar problem with one of our clients. Apparently some fonts aren't playing nice with specific printer drivers. Could you by any chance test on another printer? Are you by chance using anything of word Automation? – User999999 Oct 16 '15 at 07:09
  • @User999999 I have tried on 4 different printers, but result was exactly the same. – Nic Oct 16 '15 at 07:51

1 Answers1

3

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

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
  • Thanks, it's very interesting, on my computer both methods have same result, I will try to investigate this more. – Nic Oct 16 '15 at 15:01