Printing PDF but the left side is cut out. Using PdfiumViewer and System.Drawing

67 Views Asked by At

I developed an application to print documents. During testing it seemed to always cut a litte bit of the side of the document, sometimes half of the first character of a line. I noticed it myself during my own testing but I thoguht it was a mechanical difficulty of my printer and it wouldn't be a problem for the client's printer. But the same happened on the client's testing.

Here's how the printed document looks, sorry for the poor quality: Printed documents, red arrows point to where you can see the cropped lines

Here's the method responsible for preparing and sending the document for printing:

private static bool PrintPDF(string printer, PaperSize paperSize, string filename, int copies)
    {
        try
        {
            var document = PdfDocument.Load(filename);

            string tempConvert = JsonConvert.SerializeObject(document);
            dynamic docProperties = JsonConvert.DeserializeObject(tempConvert);

            int printDocWidth = paperSize.Xsize;
            int printDocHeight = paperSize.Ysize;

            string[] docDimensions = docProperties.PageSizes[0].ToString().Split(' ');
            char[] toTrim = { ' ', ',' };
            var temp1 = docDimensions[0].Trim(toTrim).Replace('.',',');
            var temp2 = docDimensions[1].Trim(toTrim).Replace('.', ',');
            int docWidth = (int)float.Parse(temp1);
            int docHeight = (int)float.Parse(temp2);

            // Create the printer settings for our printer
            var printerSettings = new PrinterSettings
            {
                PrinterName = printer,
                Copies = (short)copies
            };

            PaperSize paperS = new PaperSize("customsize", Convert.ToInt32(MilimeterToHundrethOfInch(printDocWidth)), Convert.ToInt32(MilimeterToHundrethOfInch(printDocHeight)));
            paperS.RawKind = (int)PaperKind.Custom;

            // Create our page settings for the paper size selected
            var pageSettings = new PageSettings(printerSettings)
            {
                Margins = new Margins(0, 0, 0, 0),
                PaperSize = paperS
            };

            // Now print the PDF document
            using (document)
            {
                using (var printDocument = document.CreatePrintDocument())
                {
                    if(docHeight > printDocHeight || docWidth > printDocHeight) printDocument.DefaultPageSettings.Landscape = true;
                    else printDocument.DefaultPageSettings.Landscape = false;
                    printDocument.PrinterSettings = printerSettings;
                    printDocument.DefaultPageSettings = pageSettings;
                    printDocument.PrintController = new StandardPrintController();
                    printDocument.Print();
                }
            }
            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }

And here's MilimeterToHundrethOfInch method that was used:

private static float MilimeterToHundrethOfInch(int milimetro)
    {
        return (milimetro / 25.4f) * 100;
    }
0

There are 0 best solutions below