Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print image center using PCL

I am using ImageMagick.net to convert jpg image into PCL and trying to print center on 7.5x6 inch page. Image is currently printing but its not cenetred to page. What I have to do to position image center on 7.5x6 inch page?
I tried using Ghosscript by converting jpg to pdf using imagemagick.net and passing pdf to ghostscript which is generating pcl command but again not printing center of the page.

Code:
using (var image = new MagickImage(inputJpgPath))
            {

                int constantDpi = 300;  // set it to 300dpi
                image.Density = new PointD(constantDpi, constantDpi);

                int fixedWidth = 432;  // Define the fixed width for the image
                int fixedHeight = 432; // Define the fixed height for the image
                image.Resize(fixedWidth, fixedHeight);

                int canvasDpi = 72;  // This can be dynamic, e.g., 72 DPI or 300 DPI

                // Get the image's DPI
                double imageDpiX = image.Density.X;
                double imageDpiY = image.Density.Y;

                // Calculate canvas size in pixels based on the desired canvas size in inches (7.5x6 inches)
                int canvasWidth = (int)(7.5 * canvasDpi);  // 7.5 inches * DPI = width in pixels
                int canvasHeight = (int)(6 * canvasDpi);  // 6 inches * DPI = height in pixels


                // Create a canvas with a white background (using the calculated dimensions)
                var canvas = new MagickImage(new MagickColor(Color.White), canvasWidth, canvasHeight);

                // If the image's DPI is different from the canvas DPI, we need to adjust the image size accordingly
                if (imageDpiX != canvasDpi || imageDpiY != canvasDpi)
                {
                    // Calculate the scaling factor to match the canvas DPI
                    double scaleX = (double)canvasDpi / imageDpiX;
                    double scaleY = (double)canvasDpi / imageDpiY;

                    // Resize the image according to the scaling factor
                    image.Resize((int)(image.Width * scaleX), (int)(image.Height * scaleY));

                }

                int xPos = (canvasWidth - image.Width) / 2;
                int yPos = (canvasHeight - image.Height) / 2;

                xPos = xPos < 0 ? 0 : xPos;
                yPos = yPos < 0 ? 0 : yPos;

                // Optional: Set the image's density to match the canvas DPI if needed
                image.Density = new PointD(canvasDpi, canvasDpi);

                // Composite the image onto the canvas at the calculated position
                canvas.Composite(image, xPos, yPos, CompositeOperator.Over);

            }

Using Ghoscript:

Code:
try
            {
                int dpi = 300;
                int widthInPoints = (int)(7.5 * 72);  // 7.5 inches in points (1 inch = 72 points)
                int heightInPoints = (int)(6 * 72); // 6 inches in points


                // Define Ghostscript arguments for conversion
                string gsArguments = $"-dNOPAUSE -dBATCH -dCenterImage -dCenterPages -dDownScaleFactor=3 -sDEVICE=pxlcolor -r{dpi} " +  // Use pxlcolor for color PCL
                                     $"-dDEVICEWIDTHPOINTS={widthInPoints} " +
                                     $"-dDEVICEHEIGHTPOINTS={heightInPoints} " +
                                     $"-dPDFFitPage " +  // Scale PDF to fit page size
                                     $"-sOutputFile=\"{outputPclPath}\" \"{inputPdfPath}\"";


                // Create a process to run Ghostscript
                ProcessStartInfo processInfo = new ProcessStartInfo
                {
                    FileName = @"C:\Program Files (x86)\gs\gs10.03.1\bin\gswin32C.exe",
                    Arguments = gsArguments,
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                };


                // Start the Ghostscript process
                using (Process process = Process.Start(processInfo))
                {
                    process.WaitForExit();
                }
            }
            catch (Exception ex)
            {


            }
 

Part and Inventory Search

Sponsor

Back
Top