# Settings $textFilePath = "C:\Users\Hezekiah\OneDrive - Xsense GmbH\Desktop\test-oblon-qr.txt" $tempQRCodePath = "$env:Temp\QRCode.png" $resizedQRCodePath = "$env:Temp\ResizedQRCode.png" $regexPatternID = "(?<=pat_id:)[^\r\n]+" # Regex to extract only pat_id $regexPatternName = "(?<=pat_name:)[^\r\n]+" # Regex to extract only pat_name $printerKeyword = "Brother QL-820NWB" # Unique part of the printer name # Desired QR code scaling factor (to make the final QR code smaller) $scalingFactor = 0.03 # Adjust between 0.1 (10%) and 1.0 (100%) # Import the QRCodeGenerator module Import-Module QRCodeGenerator # Extract pat_id and pat_name if (-Not (Test-Path $textFilePath)) { throw "Text file not found: $textFilePath" } $matchedID = ([regex]::Match((Get-Content $textFilePath -Raw), $regexPatternID)).Value.Trim() $matchedName = ([regex]::Match((Get-Content $textFilePath -Raw), $regexPatternName)).Value.Trim() if (-Not $matchedID) { throw "No 'pat_id' found." } if (-Not $matchedName) { throw "No 'pat_name' found." } Write-Output "Match found: pat_id=$matchedID, pat_name=$matchedName" # Generate QR Code at minimum allowable size $initialQRCodeWidth = 100 # A larger default size New-QRCodeText -Text $matchedID -OutPath $tempQRCodePath -Width $initialQRCodeWidth # Verify QR code generation if (-Not (Test-Path $tempQRCodePath)) { throw "QR Code generation failed. Temporary file not found: $tempQRCodePath" } # Resize the QR code image Add-Type -AssemblyName System.Drawing $originalImage = [System.Drawing.Image]::FromFile($tempQRCodePath) $targetWidth = [int]($originalImage.Width * $scalingFactor) $targetHeight = [int]($originalImage.Height * $scalingFactor) Write-Output "Resized Image Dimensions: Width=$targetWidth, Height=$targetHeight" # Create a new resized image $resizedImage = New-Object System.Drawing.Bitmap $targetWidth, $targetHeight $graphics = [System.Drawing.Graphics]::FromImage($resizedImage) $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic $graphics.DrawImage($originalImage, 0, 0, $targetWidth, $targetHeight) $resizedImage.Save($resizedQRCodePath, [System.Drawing.Imaging.ImageFormat]::Png) # Cleanup $graphics.Dispose() $originalImage.Dispose() $resizedImage.Dispose() # Debug: Verify resized image Start-Process $resizedQRCodePath Read-Host "Press Enter after verifying the QR code image" # Calculate label dimensions $fontHeight = 12 # Height of the font in points $padding = 10 # Padding between text and QR code in pixels $maxLabelHeight = 100 # Maximum label height in pixels (adjust as needed) $calculatedHeight = $fontHeight + $padding + $targetHeight $labelHeight = [math]::Min($calculatedHeight, $maxLabelHeight) # Use the smaller value Write-Output "Final Label Height: $labelHeight pixels" # Dynamically find printer $printerList = [System.Drawing.Printing.PrinterSettings]::InstalledPrinters $printerName = $printerList | Where-Object { $_ -like "*$printerKeyword*" } | Select-Object -First 1 if (-Not $printerName) { throw "No printer found matching keyword: $printerKeyword" } Write-Output "Selected printer: $printerName" # Print pat_name and QR Code $printDoc = New-Object System.Drawing.Printing.PrintDocument $printDoc.PrinterSettings.PrinterName = $printerName $printDoc.Add_PrintPage({ param($sender, $e) $qrImage = [System.Drawing.Image]::FromFile($resizedQRCodePath) # Define font for pat_name $font = New-Object System.Drawing.Font "Arial", 12 $brush = [System.Drawing.Brushes]::Black # Calculate positions $textXPosition = ($e.MarginBounds.Width - $e.Graphics.MeasureString($matchedName, $font).Width) / 2 $textYPosition = $e.MarginBounds.Top $qrXPosition = ($e.MarginBounds.Width - $qrImage.Width) / 2 $qrYPosition = $textYPosition + $font.Height + $padding # Draw pat_name $e.Graphics.DrawString($matchedName, $font, $brush, $textXPosition, $textYPosition) # Draw the QR code below the text $e.Graphics.DrawImage($qrImage, $qrXPosition, $qrYPosition, $qrImage.Width, $qrImage.Height) # Cleanup $qrImage.Dispose() }) # Ensure the page settings are configured for shorter labels $printDoc.DefaultPageSettings.Margins = New-Object System.Drawing.Printing.Margins(0, 0, 0, 0) $printDoc.DefaultPageSettings.Landscape = $false $printDoc.DefaultPageSettings.PaperSize = New-Object System.Drawing.Printing.PaperSize("Custom", $targetWidth, $labelHeight) $printDoc.Print() # Cleanup Remove-Item -Path $tempQRCodePath -Force Remove-Item -Path $resizedQRCodePath -Force Write-Output "QR Code printed and temporary files removed."