Convert PDF to PCL in .NET PDF Library
30 Nov 202616 minutes to read
Essential® PDF can convert an existing PDF document to PCL XL, the page-description language used by the majority of monochrome and color laser printers. The generated PCL XL job can be sent straight to a PCL XL–aware printer, written to a .pxl file for later replay, or streamed into any caller-owned System.IO.Stream.
The conversion is implemented with the SaveAsPcl method of the PdfDocumentBase class. Every produced job is wrapped in a PJL envelope, so the output can be handed to a printer or a print spooler without any post-processing.
Prerequisites
Assemblies required
The following assemblies need to be referenced in your application based on the platform.
| Platform(s) | Assembly |
|---|---|
| Blazor, .NET Core, and .NET Platforms |
Syncfusion.Compression.Portable.dll Syncfusion.Pdf.Portable.dll |
| Windows UI Library (WinUI), .NET Multi-platform App UI (.NET MAUI) |
Syncfusion.Pdf.NET.dll Syncfusion.Compression.NET.dll |
Namespaces required
The following namespaces must be included in your application to convert a PDF document to PCL XL. The Syncfusion.Pdf namespace provides the PclFormat enumeration, the Syncfusion.Pdf.Exporting namespace provides the PclSettings class, and the Syncfusion.Pdf.Parsing namespace provides the PdfLoadedDocument class.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Exporting;
using Syncfusion.Pdf.Parsing;using Syncfusion.Pdf;
using Syncfusion.Pdf.Exporting;
using Syncfusion.Pdf.Parsing;Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Exporting
Imports Syncfusion.Pdf.ParsingConverting a PDF document to PCL XL
Load the existing PDF document using the PdfLoadedDocument class and call the SaveAsPcl method with the PclFormat value PclFormat.PclXL.
//Load an existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Convert the PDF document to a PCL XL job.
document.SaveAsPcl("Output.pxl", PclFormat.PclXL);
//Close the document.
document.Close(true);//Load an existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Convert the PDF document to a PCL XL job.
document.SaveAsPcl("Output.pxl", PclFormat.PclXL);
//Close the document.
document.Close(true);'Load an existing PDF document.
Dim document As New PdfLoadedDocument("Input.pdf")
'Convert the PDF document to a PCL XL job.
document.SaveAsPcl("Output.pxl", PclFormat.PclXL)
'Close the document.
document.Close(True)The
SaveAsPclmethod is available on bothPdfLoadedDocumentandPdfDocument. When a newly createdPdfDocumentis passed, the library works on an internal copy of the document, and the original document is never modified.
Saving to a file path
The SaveAsPcl(string, PclFormat) overload writes the PCL XL job to the file system. If the supplied file name does not contain an extension, the extension for the selected format is appended automatically, so Output becomes Output.pxl. An existing file with the same name is overwritten.
If the conversion fails, the partially written file is deleted from the disk before the exception is propagated, so a truncated print job is never left behind.
Saving to a stream
Use the SaveAsPcl(Stream, PclFormat) overload to write the PCL XL job into a stream that you own. The library writes the bytes from the current position of the stream and never closes it, so you can append further data or dispose the stream yourself.
//Create a memory stream.
MemoryStream stream = new MemoryStream();
//Load an existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Convert the PDF document to a PCL XL job.
document.SaveAsPcl(stream, PclFormat.PclXL);
//Close the document.
document.Close(true);
//The stream is still open and holds the PCL XL job.
byte[] pxl = stream.ToArray();//Create a memory stream.
MemoryStream stream = new MemoryStream();
//Load an existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Convert the PDF document to a PCL XL job.
document.SaveAsPcl(stream, PclFormat.PclXL);
//Close the document.
document.Close(true);
//The stream is still open and holds the PCL XL job.
byte[] pxl = stream.ToArray();'Create a memory stream.
Dim stream As New MemoryStream()
'Load an existing PDF document.
Dim document As New PdfLoadedDocument("Input.pdf")
'Convert the PDF document to a PCL XL job.
document.SaveAsPcl(stream, PclFormat.PclXL)
'Close the document.
document.Close(True)
'The stream is still open and holds the PCL XL job.
Dim pxl As Byte() = stream.ToArray()Configuring the PCL XL output
The optional PclSettings class controls the resolution, paper size, orientation, input tray, scaling mode, job name, and the number of copies of the produced job. Pass the settings instance to the SaveAsPcl overload that accepts a PclSettings argument. Passing null uses the default settings.
//Create the PCL XL settings.
PclSettings settings = new PclSettings();
settings.Resolution = PclResolutionDpiValue.Dpi600;
settings.PaperSize = PclPaperSize.A4;
settings.Orientation = PclOrientation.Auto;
settings.InputTray = PclInputTray.UpperCassette;
settings.ScalingMode = PclScalingMode.Fit;
settings.JobName = "QuarterlyReport";
settings.Copies = 2;
//Load an existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Convert the PDF document to a PCL XL job.
document.SaveAsPcl("Output.pxl", PclFormat.PclXL, settings);
//Close the document.
document.Close(true);//Create the PCL XL settings.
PclSettings settings = new PclSettings();
settings.Resolution = PclResolutionDpiValue.Dpi600;
settings.PaperSize = PclPaperSize.A4;
settings.Orientation = PclOrientation.Auto;
settings.InputTray = PclInputTray.UpperCassette;
settings.ScalingMode = PclScalingMode.Fit;
settings.JobName = "QuarterlyReport";
settings.Copies = 2;
//Load an existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Convert the PDF document to a PCL XL job.
document.SaveAsPcl("Output.pxl", PclFormat.PclXL, settings);
//Close the document.
document.Close(true);'Create the PCL XL settings.
Dim settings As New PclSettings()
settings.Resolution = PclResolutionDpiValue.Dpi600
settings.PaperSize = PclPaperSize.A4
settings.Orientation = PclOrientation.Auto
settings.InputTray = PclInputTray.UpperCassette
settings.ScalingMode = PclScalingMode.Fit
settings.JobName = "QuarterlyReport"
settings.Copies = 2
'Load an existing PDF document.
Dim document As New PdfLoadedDocument("Input.pdf")
'Convert the PDF document to a PCL XL job.
document.SaveAsPcl("Output.pxl", PclFormat.PclXL, settings)
'Close the document.
document.Close(True)PclSettings properties
The following table lists the properties of the PclSettings class and their default values.
| Property | Type | Default | Description |
|---|---|---|---|
Resolution |
PclResolutionDpiValue |
Dpi300 |
The device resolution used for the generated job. The supported values are Dpi300, Dpi600, and Dpi1200. |
PaperSize |
PclPaperSize |
Auto |
The paper medium requested on the BeginPage operator. Auto derives the nearest standard medium from the PDF page box. |
Orientation |
PclOrientation |
Auto |
The orientation of the output pages. Auto honors the /Rotate entry of each page and prints a page that is wider than it is tall in landscape. Portrait and Landscape force the orientation. |
InputTray |
PclInputTray |
Auto |
The source paper tray. Auto omits the MediaSource attribute, and the remaining values request the corresponding tray. |
ScalingMode |
PclScalingMode |
None |
Determines how the page content is fitted to the selected medium. The supported values are None, Fit, Fill, and Stretch. |
JobName |
string |
"PDF" |
The name of the print job, written into the PJL header as NAME="…". |
Copies |
int |
1 |
The number of copies to print. The valid range is 1 to 999. |
Resolution
The Resolution property selects the device resolution of the produced job. It affects every PCL XL operator that accepts a measurement.
PclSettings settings = new PclSettings();
settings.Resolution = PclResolutionDpiValue.Dpi1200;Dim settings As New PclSettings()
settings.Resolution = PclResolutionDpiValue.Dpi1200Paper size
The PaperSize property selects the physical medium of the output pages. When it is set to Auto, the library compares the PDF page box against the standard PCL XL media and picks the nearest one.
PclSettings settings = new PclSettings();
settings.PaperSize = PclPaperSize.A3;Dim settings As New PclSettings()
settings.PaperSize = PclPaperSize.A3The following table lists the supported paper sizes.
| Value | Emitted PCL XL medium |
|---|---|
Auto |
The nearest standard medium derived from the PDF page box. |
Letter |
LetterPaper |
Legal |
LegalPaper |
Ledger |
LedgerPaper |
A3 |
A3Paper |
A4 |
A4Paper |
A5 |
A5Paper |
Executive |
ExecPaper |
Orientation
The Orientation property controls how each page is placed on the medium.
-
Auto– The/Rotateentry of the page is honored, and a page that is wider than it is tall is printed in landscape. -
Portrait– Every page is printed in portrait, and the/Rotateentry is ignored. -
Landscape– Every page is printed in landscape, and the/Rotateentry is ignored.
PclSettings settings = new PclSettings();
settings.Orientation = PclOrientation.Landscape;Dim settings As New PclSettings()
settings.Orientation = PclOrientation.LandscapeInput tray
The InputTray property requests a specific source tray on the printer. When it is set to Auto, the MediaSource attribute is omitted from the BeginPage operator, and the printer selects the default tray.
PclSettings settings = new PclSettings();
settings.InputTray = PclInputTray.LowerCassette;Dim settings As New PclSettings()
settings.InputTray = PclInputTray.LowerCassetteThe following table lists the supported input trays.
| Value | Emitted PCL XL media source |
|---|---|
Auto |
The MediaSource attribute is omitted. |
Manual |
ManualFeed |
MultiPurpose |
MultiPurposeTray |
UpperCassette |
UpperCassette |
LowerCassette |
LowerCassette |
Envelope |
EnvelopeFeeder |
Scaling mode
The ScalingMode property determines how the page content is fitted to the selected medium.
-
None– The content is emitted at its original size. -
Fit– The content is scaled down until it fits completely on the medium. -
Fill– The content is scaled up until it fills the medium. -
Stretch– The content is scaled independently along each axis so that it fills the medium.
PclSettings settings = new PclSettings();
settings.ScalingMode = PclScalingMode.Fit;Dim settings As New PclSettings()
settings.ScalingMode = PclScalingMode.FitJob name
The JobName property sets the name of the print job, which is written into the PJL header as @PJL JOB NAME="…". The library normalizes the value before it is written, so the value returned by the getter is exactly the name carried by the header.
-
null, an empty string, or a string with no usable characters becomes"PDF". - Quotes, control characters, and non-ASCII characters are removed.
- The result is truncated to 80 characters.
PclSettings settings = new PclSettings();
settings.JobName = "Invoice #2026";Dim settings As New PclSettings()
settings.JobName = "Invoice #2026"Copies
The Copies property sets the number of copies of the job to print. The valid range is 1 to 999, and the default value is 1. Assigning a value outside this range throws an ArgumentOutOfRangeException.
PclSettings settings = new PclSettings();
settings.Copies = 3;Dim settings As New PclSettings()
settings.Copies = 3Best practices
-
Reuse a single settings instance. The
PclSettingsobject is read during the conversion only, so it can be shared between saves as long as the values are not modified concurrently. -
Dispose the loaded document. Call the
Closemethod of thePdfLoadedDocumentclass after the conversion to release the memory consumed by the PDF DOM. -
Keep the caller-owned stream open. The library never closes a stream that you pass to the
SaveAsPclmethod, so you decide when the stream is disposed. - Do not modify the source document. The conversion never mutates the document that you pass, so the same document can be converted repeatedly or saved as PDF in parallel with a PCL XL export.