Przejdź do treści stopki
PRZEWODNIKI MIGRACJI

Jak przeprowadzić migrację z RawPrint do IronPDF w języku C#

Przejście z RawPrint naIronPDFzmienia proces obsługi dokumentów z niskopoziomowej transmisji bajtów do drukarki na kompleksowe rozwiązanie do tworzenia i drukowania plików PDF. Niniejszy przewodnik zawiera kompletną, szczegółową ścieżkę migracji, która umożliwia tworzenie, edycję i drukowanie plików PDF za pomocą zaawansowanych interfejsów API zamiast ręcznego zarządzania obiektami drukarki.

Dlaczego warto przejść z RawPrint na IronPDF

Zrozumienie RawPrint

RawPrint to zbiór implementacji umożliwiających wysyłanie surowych danych bezpośrednio do drukarki. Jest to niezbędne w przypadku aplikacji wymagających bezpośredniego przesyłania poleceń do drukarek, z pominięciem konwencjonalnych sterowników drukarek. Ta funkcja jest szczególnie przydatna w sytuacjach, w których wykorzystywane są specjalistyczne drukarki, takie jak drukarki etykiet wykorzystujące języki ZPL (Zebra Programming Language) lub EPL (Eltron Programming Language).

Jedną z zalet RawPrint jest prostota wysyłania strumieni danych bezpośrednio do drukarki. Dla programistów tworzących oprogramowanie przeznaczone dla środowiskWindowsi wymagających bezpośredniej komunikacji z drukarką, RawPrint oferuje wydajną ścieżkę omijającą warstwy pośredniczące, takie jak sterowniki lub interfejsy graficzne.

Jednak RawPrint NIE jest biblioteką PDF — po prostu przesyła dane do drukarek. To fundamentalne ograniczenie sprawia, że jest to niewłaściwy wybór w większości scenariuszy generowania dokumentów.

Główny problem: brak możliwości tworzenia plików PDF

RawPrint ma istotne ograniczenia, które sprawiają, że nie nadaje się do nowoczesnych procesów obsługi dokumentów:

  1. Brak tworzenia plików PDF: RawPrint skupia się wyłącznie na transmisji danych, nie posiada funkcji tworzenia, renderowania ani edycji plików PDF.

  2. Bardzo niski poziom: Ponieważ programiści mają do czynienia bezpośrednio z surowymi bajtami, muszą oni posiadać dogłębną znajomość języka poleceń drukarki, co sprawia, że rozwiązanie to jest mniej odpowiednie do prostych zadań związanych z drukowaniem dokumentów.

  3. Zależność od platformy: Rozwiązanie opiera się na buforach wydruku systemu Windows, co ogranicza jego zastosowanie na różnych platformach.

  4. Brak przetwarzania dokumentów: Tylko transmisja bajtów bez możliwości renderowania.

  5. Ograniczona kontrola: Minimalne opcje konfiguracji zadań drukowania.

Porównanie RawPrint i IronPDF

Funkcja RawPrint IronPDF
Funkcjonalność Wysyła surowe dane drukowania bezpośrednio do drukarki Kompleksowe tworzenie i edycja plików PDF
Przykład zastosowania Specjalistyczne wydruki, takie jak etykiety Ogólne zarządzanie dokumentami i ich tworzenie
Zależność od platformy Specyficzne dla systemuWindows Wieloplatformowe
Złożoność Niski poziom, wymaga znajomości poleceń drukarki Wysokopoziomowy, przyjazny dla użytkownika interfejs API
Tworzenie plików PDF Nie Tak
Utwórz plik PDF z HTML Nie Tak
Utwórz plik PDF z adresu URL Nie Tak
Edytuj/modyfikuj pliki PDF Nie Tak
Łączenie/dzielenie plików PDF Nie Tak
Wydrukuj istniejący plik PDF Tak (surowe bajty) Tak (API wysokiego poziomu)
Kontrola drukowania Podstawowe Pełne opcje
Idealny dla Wymagania dotyczące bezpośredniego dostępu do drukarki Zadania związane z plikami PDF w aplikacjach internetowych i desktopowych
Elastyczność Ograniczone ze względu na obsługę surowych bajtów Rozbudowany, z wieloma funkcjami

W przeciwieństwie do RawPrint,IronPDFoferuje solidny i wszechstronny interfejs API do kompleksowej obsługi plików PDF. Jako uznana marka w środowisku .NET,IronPDFumożliwia programistom łatwe tworzenie, edycję i konwersję plików PDF na różnych platformach.

Dla zespołów planujących wdrożenie .NET 10 i C# 14 w latach 2025 i 2026,IronPDFzapewnia kompatybilność międzyplatformową, której RawPrint nie jest w stanie zaoferować.


Zanim zaczniesz

Wymagania wstępne

  1. Środowisko .NET: .NET Framework 4.6.2+ lub .NET Core 3.1+ / .NET 5/6/7/8/9+
  2. Dostęp do NuGet: Możliwość instalowania pakietów NuGet
  3. Licencja IronPDF: Uzyskaj klucz licencyjny na stronie ironpdf.com

Zmiany w pakiecie NuGet

# Remove RawPrint
dotnet remove package RawPrint

# Install IronPDF
dotnet add package IronPdf
# Remove RawPrint
dotnet remove package RawPrint

# Install IronPDF
dotnet add package IronPdf
SHELL

Konfiguracja licencji

// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
' Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

Znajdź zastosowanie RawPrint

# Identify all RawPrint usage
grep -r "using RawPrint" --include="*.cs" .
grep -r "Printer\|SendBytesToPrinter" --include="*.cs" .
# Identify all RawPrint usage
grep -r "using RawPrint" --include="*.cs" .
grep -r "Printer\|SendBytesToPrinter" --include="*.cs" .
SHELL

Kompletna dokumentacija API

Zmiany w przestrzeni nazw

// Before: RawPrint (Windows interop)
using System.Runtime.InteropServices;
using System.Drawing.Printing;

// After: IronPDF
using IronPdf;
// Before: RawPrint (Windows interop)
using System.Runtime.InteropServices;
using System.Drawing.Printing;

// After: IronPDF
using IronPdf;
' Before: RawPrint (Windows interop)
Imports System.Runtime.InteropServices
Imports System.Drawing.Printing

' After: IronPDF
Imports IronPdf
$vbLabelText   $csharpLabel

Mapowania podstawowych interfejsów API

RawPrint IronPDF Uwagi
Printer.SendBytesToPrinter() pdf.Print() Drukowanie na wysokim poziomie
Printer.OpenPrinter() Nie dotyczy Nie jest potrzebne
Printer.ClosePrinter() Nie dotyczy Automatyczne
Printer.StartDocPrinter() Nie dotyczy Automatyczne
Printer.WritePrinter() Nie dotyczy Automatyczne
Printer.EndDocPrinter() Nie dotyczy Automatyczne
Nie dotyczy ChromePdfRenderer Tworzenie plików PDF
Nie dotyczy PdfDocument.Merge() Łączenie plików PDF
Nie dotyczy pdf.ApplyWatermark() Dodaj znaki wodne

Przykłady migracji kodu

Przykład 1: Konwersja HTML do PDF

Before (RawPrint):

// NuGet: Install-Package System.Drawing.Common
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Text;

class RawPrinterHelper
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
    }

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes;
        Int32 dwCount;
        dwCount = szString.Length;
        pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        IntPtr hPrinter;
        if (OpenPrinter(szPrinterName, out hPrinter, IntPtr.Zero))
        {
            DOCINFOA di = new DOCINFOA();
            di.pDocName = "HTML Document";
            di.pDataType = "RAW";
            if (StartDocPrinter(hPrinter, 1, di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    Int32 dwWritten;
                    WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
        return false;
    }
}

class Program
{
    static void Main()
    {
        string html = "<html><body><h1>Hello World</h1></body></html>";
        // RawPrint cannot directly convert HTML to PDF
        // It sends raw data to printer, no PDF generation capability
        RawPrinterHelper.SendStringToPrinter("Microsoft Print to PDF", html);
    }
}
// NuGet: Install-Package System.Drawing.Common
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Text;

class RawPrinterHelper
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
    }

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes;
        Int32 dwCount;
        dwCount = szString.Length;
        pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        IntPtr hPrinter;
        if (OpenPrinter(szPrinterName, out hPrinter, IntPtr.Zero))
        {
            DOCINFOA di = new DOCINFOA();
            di.pDocName = "HTML Document";
            di.pDataType = "RAW";
            if (StartDocPrinter(hPrinter, 1, di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    Int32 dwWritten;
                    WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
        return false;
    }
}

class Program
{
    static void Main()
    {
        string html = "<html><body><h1>Hello World</h1></body></html>";
        // RawPrint cannot directly convert HTML to PDF
        // It sends raw data to printer, no PDF generation capability
        RawPrinterHelper.SendStringToPrinter("Microsoft Print to PDF", html);
    }
}
Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Imports System.Text

Class RawPrinterHelper
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Class DOCINFOA
        <MarshalAs(UnmanagedType.LPStr)> Public pDocName As String
        <MarshalAs(UnmanagedType.LPStr)> Public pOutputFile As String
        <MarshalAs(UnmanagedType.LPStr)> Public pDataType As String
    End Class

    <DllImport("winspool.Drv", EntryPoint:="OpenPrinterA", SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Function OpenPrinter(<MarshalAs(UnmanagedType.LPStr)> szPrinter As String, ByRef hPrinter As IntPtr, pd As IntPtr) As Boolean
    End Function

    <DllImport("winspool.Drv", EntryPoint:="ClosePrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Function ClosePrinter(hPrinter As IntPtr) As Boolean
    End Function

    <DllImport("winspool.Drv", EntryPoint:="StartDocPrinterA", SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Function StartDocPrinter(hPrinter As IntPtr, level As Integer, <[In], MarshalAs(UnmanagedType.LPStruct)> di As DOCINFOA) As Boolean
    End Function

    <DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Function EndDocPrinter(hPrinter As IntPtr) As Boolean
    End Function

    <DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Function StartPagePrinter(hPrinter As IntPtr) As Boolean
    End Function

    <DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Function EndPagePrinter(hPrinter As IntPtr) As Boolean
    End Function

    <DllImport("winspool.Drv", EntryPoint:="WritePrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Function WritePrinter(hPrinter As IntPtr, pBytes As IntPtr, dwCount As Integer, ByRef dwWritten As Integer) As Boolean
    End Function

    Public Shared Function SendStringToPrinter(szPrinterName As String, szString As String) As Boolean
        Dim pBytes As IntPtr
        Dim dwCount As Integer
        dwCount = szString.Length
        pBytes = Marshal.StringToCoTaskMemAnsi(szString)
        Dim hPrinter As IntPtr
        If OpenPrinter(szPrinterName, hPrinter, IntPtr.Zero) Then
            Dim di As New DOCINFOA()
            di.pDocName = "HTML Document"
            di.pDataType = "RAW"
            If StartDocPrinter(hPrinter, 1, di) Then
                If StartPagePrinter(hPrinter) Then
                    Dim dwWritten As Integer
                    WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
                    EndPagePrinter(hPrinter)
                End If
                EndDocPrinter(hPrinter)
            End If
            ClosePrinter(hPrinter)
            Marshal.FreeCoTaskMem(pBytes)
            Return True
        End If
        Return False
    End Function
End Class

Class Program
    Shared Sub Main()
        Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
        ' RawPrint cannot directly convert HTML to PDF
        ' It sends raw data to printer, no PDF generation capability
        RawPrinterHelper.SendStringToPrinter("Microsoft Print to PDF", html)
    End Sub
End Class
$vbLabelText   $csharpLabel

Po (IronPDF):

// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        string html = "<html><body><h1>Hello World</h1></body></html>";
        var pdf = renderer.RenderHtmlAsPdf(html);
        pdf.SaveAs("output.pdf");
        Console.WriteLine("PDF created successfully");
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        string html = "<html><body><h1>Hello World</h1></body></html>";
        var pdf = renderer.RenderHtmlAsPdf(html);
        pdf.SaveAs("output.pdf");
        Console.WriteLine("PDF created successfully");
    }
}
Imports IronPdf
Imports System

Class Program
    Shared Sub Main()
        Dim renderer = New ChromePdfRenderer()
        Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
        Dim pdf = renderer.RenderHtmlAsPdf(html)
        pdf.SaveAs("output.pdf")
        Console.WriteLine("PDF created successfully")
    End Sub
End Class
$vbLabelText   $csharpLabel

Ten przykład ilustruje podstawową różnicę architektoniczną. RawPrint requires 60+ lines of code with multiple DLL imports from winspool.Drv, manual printer handle management (OpenPrinter, StartDocPrinter, WritePrinter, EndDocPrinter, ClosePrinter), and memory marshaling—and it still cannot create a PDF. It only sends raw data to the printer with no rendering capability.

IronPDF accomplishes the task in 5 lines: create a ChromePdfRenderer, call RenderHtmlAsPdf(), and SaveAs(). Kompleksowe przykłady można znaleźć w dokumentacji dotyczącej konwersji HTML do PDF.

Przykład 2: Konwersja adresów URL do formatu PDF

Before (RawPrint):

// NuGet: Install-Package System.Drawing.Common
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;

class RawPrinterHelper
{
    // ... (same DLL imports as above) ...

    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        IntPtr hPrinter;
        if (OpenPrinter(szPrinterName, out hPrinter, IntPtr.Zero))
        {
            DOCINFOA di = new DOCINFOA();
            di.pDocName = "Web Page";
            di.pDataType = "RAW";
            if (StartDocPrinter(hPrinter, 1, di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    Int32 dwWritten;
                    WritePrinter(hPrinter, pBytes, szString.Length, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
        return false;
    }
}

class Program
{
    static void Main()
    {
        // RawPrint cannot render web pages - only sends raw text/data
        // This would just print HTML source code, not rendered content
        using (WebClient client = new WebClient())
        {
            string htmlSource = client.DownloadString("https://example.com");
            // This prints raw HTML, not a rendered PDF
            RawPrinterHelper.SendStringToPrinter("Microsoft Print to PDF", htmlSource);
            Console.WriteLine("Raw HTML sent to printer (not rendered)");
        }
    }
}
// NuGet: Install-Package System.Drawing.Common
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;

class RawPrinterHelper
{
    // ... (same DLL imports as above) ...

    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        IntPtr hPrinter;
        if (OpenPrinter(szPrinterName, out hPrinter, IntPtr.Zero))
        {
            DOCINFOA di = new DOCINFOA();
            di.pDocName = "Web Page";
            di.pDataType = "RAW";
            if (StartDocPrinter(hPrinter, 1, di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    Int32 dwWritten;
                    WritePrinter(hPrinter, pBytes, szString.Length, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
        return false;
    }
}

class Program
{
    static void Main()
    {
        // RawPrint cannot render web pages - only sends raw text/data
        // This would just print HTML source code, not rendered content
        using (WebClient client = new WebClient())
        {
            string htmlSource = client.DownloadString("https://example.com");
            // This prints raw HTML, not a rendered PDF
            RawPrinterHelper.SendStringToPrinter("Microsoft Print to PDF", htmlSource);
            Console.WriteLine("Raw HTML sent to printer (not rendered)");
        }
    }
}
' NuGet: Install-Package System.Drawing.Common
Imports System
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.Text

Class RawPrinterHelper
    ' ... (same DLL imports as above) ...

    Public Shared Function SendStringToPrinter(ByVal szPrinterName As String, ByVal szString As String) As Boolean
        Dim pBytes As IntPtr = Marshal.StringToCoTaskMemAnsi(szString)
        Dim hPrinter As IntPtr
        If OpenPrinter(szPrinterName, hPrinter, IntPtr.Zero) Then
            Dim di As New DOCINFOA()
            di.pDocName = "Web Page"
            di.pDataType = "RAW"
            If StartDocPrinter(hPrinter, 1, di) Then
                If StartPagePrinter(hPrinter) Then
                    Dim dwWritten As Integer
                    WritePrinter(hPrinter, pBytes, szString.Length, dwWritten)
                    EndPagePrinter(hPrinter)
                End If
                EndDocPrinter(hPrinter)
            End If
            ClosePrinter(hPrinter)
            Marshal.FreeCoTaskMem(pBytes)
            Return True
        End If
        Return False
    End Function
End Class

Class Program
    Shared Sub Main()
        ' RawPrint cannot render web pages - only sends raw text/data
        ' This would just print HTML source code, not rendered content
        Using client As New WebClient()
            Dim htmlSource As String = client.DownloadString("https://example.com")
            ' This prints raw HTML, not a rendered PDF
            RawPrinterHelper.SendStringToPrinter("Microsoft Print to PDF", htmlSource)
            Console.WriteLine("Raw HTML sent to printer (not rendered)")
        End Using
    End Sub
End Class
$vbLabelText   $csharpLabel

Po (IronPDF):

// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        // Render a live website directly to PDF with full CSS, JavaScript, and images
        var pdf = renderer.RenderUrlAsPdf("https://example.com");
        pdf.SaveAs("webpage.pdf");
        Console.WriteLine("Website rendered to PDF successfully");
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        // Render a live website directly to PDF with full CSS, JavaScript, and images
        var pdf = renderer.RenderUrlAsPdf("https://example.com");
        pdf.SaveAs("webpage.pdf");
        Console.WriteLine("Website rendered to PDF successfully");
    }
}
Imports IronPdf
Imports System

Class Program
    Shared Sub Main()
        Dim renderer = New ChromePdfRenderer()
        ' Render a live website directly to PDF with full CSS, JavaScript, and images
        Dim pdf = renderer.RenderUrlAsPdf("https://example.com")
        pdf.SaveAs("webpage.pdf")
        Console.WriteLine("Website rendered to PDF successfully")
    End Sub
End Class
$vbLabelText   $csharpLabel

RawPrint cannot render web pages—it only sends raw text/data. The RawPrint approach downloads HTML source and sends it directly to the printer, resulting in raw HTML code being printed, not rendered content. IronPDF's RenderUrlAsPdf() captures the fully rendered webpage with CSS, JavaScript, and images. Dowiedz się więcej z naszych samouczków.

Example 3: Document Formatting

Before (RawPrint):

// NuGet: Install-Package System.Drawing.Common
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Text;

class RawPrinterHelper
{
    // ... (same DLL imports) ...

    public static bool SendBytesToPrinter(string szPrinterName, byte[] pBytes)
    {
        IntPtr pUnmanagedBytes = Marshal.AllocCoTaskMem(pBytes.Length);
        Marshal.Copy(pBytes, 0, pUnmanagedBytes, pBytes.Length);
        IntPtr hPrinter;
        if (OpenPrinter(szPrinterName, out hPrinter, IntPtr.Zero))
        {
            DOCINFOA di = new DOCINFOA();
            di.pDocName = "Raw Document";
            di.pDataType = "RAW";
            if (StartDocPrinter(hPrinter, 1, di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    Int32 dwWritten;
                    WritePrinter(hPrinter, pUnmanagedBytes, pBytes.Length, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            return true;
        }
        return false;
    }
}

class Program
{
    static void Main()
    {
        // RawPrint requires manual PCL/PostScript commands for formatting
        string pclCommands = "\x1B&l0O\x1B(s0p16.66h8.5v0s0b3T";
        string text = "Plain text document - limited formatting";
        byte[] data = Encoding.ASCII.GetBytes(pclCommands + text);
        RawPrinterHelper.SendBytesToPrinter("HP LaserJet", data);
    }
}
// NuGet: Install-Package System.Drawing.Common
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Text;

class RawPrinterHelper
{
    // ... (same DLL imports) ...

    public static bool SendBytesToPrinter(string szPrinterName, byte[] pBytes)
    {
        IntPtr pUnmanagedBytes = Marshal.AllocCoTaskMem(pBytes.Length);
        Marshal.Copy(pBytes, 0, pUnmanagedBytes, pBytes.Length);
        IntPtr hPrinter;
        if (OpenPrinter(szPrinterName, out hPrinter, IntPtr.Zero))
        {
            DOCINFOA di = new DOCINFOA();
            di.pDocName = "Raw Document";
            di.pDataType = "RAW";
            if (StartDocPrinter(hPrinter, 1, di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    Int32 dwWritten;
                    WritePrinter(hPrinter, pUnmanagedBytes, pBytes.Length, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
            Marshal.FreeCoTaskMem(pUnmanagedBytes);
            return true;
        }
        return false;
    }
}

class Program
{
    static void Main()
    {
        // RawPrint requires manual PCL/PostScript commands for formatting
        string pclCommands = "\x1B&l0O\x1B(s0p16.66h8.5v0s0b3T";
        string text = "Plain text document - limited formatting";
        byte[] data = Encoding.ASCII.GetBytes(pclCommands + text);
        RawPrinterHelper.SendBytesToPrinter("HP LaserJet", data);
    }
}
' NuGet: Install-Package System.Drawing.Common
Imports System
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Imports System.Text

Class RawPrinterHelper
    ' ... (same DLL imports) ...

    Public Shared Function SendBytesToPrinter(szPrinterName As String, pBytes As Byte()) As Boolean
        Dim pUnmanagedBytes As IntPtr = Marshal.AllocCoTaskMem(pBytes.Length)
        Marshal.Copy(pBytes, 0, pUnmanagedBytes, pBytes.Length)
        Dim hPrinter As IntPtr
        If OpenPrinter(szPrinterName, hPrinter, IntPtr.Zero) Then
            Dim di As New DOCINFOA()
            di.pDocName = "Raw Document"
            di.pDataType = "RAW"
            If StartDocPrinter(hPrinter, 1, di) Then
                If StartPagePrinter(hPrinter) Then
                    Dim dwWritten As Int32
                    WritePrinter(hPrinter, pUnmanagedBytes, pBytes.Length, dwWritten)
                    EndPagePrinter(hPrinter)
                End If
                EndDocPrinter(hPrinter)
            End If
            ClosePrinter(hPrinter)
            Marshal.FreeCoTaskMem(pUnmanagedBytes)
            Return True
        End If
        Return False
    End Function
End Class

Class Program
    Shared Sub Main()
        ' RawPrint requires manual PCL/PostScript commands for formatting
        Dim pclCommands As String = Chr(&H1B) & "&l0O" & Chr(&H1B) & "(s0p16.66h8.5v0s0b3T"
        Dim text As String = "Plain text document - limited formatting"
        Dim data As Byte() = Encoding.ASCII.GetBytes(pclCommands & text)
        RawPrinterHelper.SendBytesToPrinter("HP LaserJet", data)
    End Sub
End Class
$vbLabelText   $csharpLabel

Po (IronPDF):

// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        string html = @"
            <html>
            <head>
                <style>
                    body { font-family: Arial; margin: 40px; }
                    h1 { color: #2c3e50; font-size: 24px; }
                    p { line-height: 1.6; color: #34495e; }
                    .highlight { background-color: yellow; font-weight: bold; }
                </style>
            </head>
            <body>
                <h1>Formatted Document</h1>
                <p>This is a <span class='highlight'>beautifully formatted</span> document with CSS styling.</p>
                <p>Complex layouts, fonts, colors, and images are fully supported.</p>
            </body>
            </html>";

        var pdf = renderer.RenderHtmlAsPdf(html);
        pdf.SaveAs("formatted.pdf");
        Console.WriteLine("Formatted PDF created successfully");
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        string html = @"
            <html>
            <head>
                <style>
                    body { font-family: Arial; margin: 40px; }
                    h1 { color: #2c3e50; font-size: 24px; }
                    p { line-height: 1.6; color: #34495e; }
                    .highlight { background-color: yellow; font-weight: bold; }
                </style>
            </head>
            <body>
                <h1>Formatted Document</h1>
                <p>This is a <span class='highlight'>beautifully formatted</span> document with CSS styling.</p>
                <p>Complex layouts, fonts, colors, and images are fully supported.</p>
            </body>
            </html>";

        var pdf = renderer.RenderHtmlAsPdf(html);
        pdf.SaveAs("formatted.pdf");
        Console.WriteLine("Formatted PDF created successfully");
    }
}
Imports IronPdf
Imports System

Class Program
    Shared Sub Main()
        Dim renderer = New ChromePdfRenderer()
        Dim html As String = "
            <html>
            <head>
                <style>
                    body { font-family: Arial; margin: 40px; }
                    h1 { color: #2c3e50; font-size: 24px; }
                    p { line-height: 1.6; color: #34495e; }
                    .highlight { background-color: yellow; font-weight: bold; }
                </style>
            </head>
            <body>
                <h1>Formatted Document</h1>
                <p>This is a <span class='highlight'>beautifully formatted</span> document with CSS styling.</p>
                <p>Complex layouts, fonts, colors, and images are fully supported.</p>
            </body>
            </html>"

        Dim pdf = renderer.RenderHtmlAsPdf(html)
        pdf.SaveAs("formatted.pdf")
        Console.WriteLine("Formatted PDF created successfully")
    End Sub
End Class
$vbLabelText   $csharpLabel

RawPrint requires manual PCL/PostScript commands ("\x1B&l0O\x1B(s0p16.66h8.5v0s0b3T") for even basic formatting. Developers must have deep understanding of the printer's command language.IronPDFuses standard HTML and CSS for formatting—complex layouts, fonts, colors, and images are fully supported without printer-specific knowledge.


Podsumowanie porównania funkcji

Funkcja RawPrint IronPDF
:PDF Creation: HTML do PDF Nie Tak
URL do pliku PDF Nie Tak
Stwórz od podstaw Nie Tak
:PDF Manipulation: Łączenie plików PDF Nie Tak
Podział plików PDF Nie Tak
Dodaj znaki wodne Nie Tak
Edytuj istniejący Nie Tak
:Printing: PRINT PDF Tak (surowy) Tak (ogólne)
Okno dialogowe drukowania Nie Tak
Wiele kopii Ograniczone Tak
Kontrola DPI Nie Tak
Duplex Nie Tak
:Platform: Windows Tak Tak
Linux Nie Tak
macOS Nie Tak
Docker Nie Tak
:Other: Bezpieczeństwo Nie Tak
Podpisy cyfrowe Nie Tak
PDF/A Nie Tak

Nowe możliwości po migracji

After migrating to IronPDF, you gain capabilities that RawPrint cannot provide:

Łączenie plików PDF

var pdfs = pdfFiles.Select(f => PdfDocument.FromFile(f)).ToList();
var merged = PdfDocument.Merge(pdfs);
merged.SaveAs("complete.pdf");
var pdfs = pdfFiles.Select(f => PdfDocument.FromFile(f)).ToList();
var merged = PdfDocument.Merge(pdfs);
merged.SaveAs("complete.pdf");
Dim pdfs = pdfFiles.Select(Function(f) PdfDocument.FromFile(f)).ToList()
Dim merged = PdfDocument.Merge(pdfs)
merged.SaveAs("complete.pdf")
$vbLabelText   $csharpLabel

Print with Settings

var pdf = PdfDocument.FromFile("report.pdf");

pdf.Print(new PrintOptions
{
    PrinterName = "HP LaserJet",
    NumberOfCopies = 2,
    DPI = 300,
    GrayScale = false
});
var pdf = PdfDocument.FromFile("report.pdf");

pdf.Print(new PrintOptions
{
    PrinterName = "HP LaserJet",
    NumberOfCopies = 2,
    DPI = 300,
    GrayScale = false
});
Dim pdf = PdfDocument.FromFile("report.pdf")

pdf.Print(New PrintOptions With {
    .PrinterName = "HP LaserJet",
    .NumberOfCopies = 2,
    .DPI = 300,
    .GrayScale = False
})
$vbLabelText   $csharpLabel

Create and Print in One Workflow

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
    <h1>Invoice #12345</h1>
    <p>Customer: John Doe</p>
    <p>Amount: $150.00</p>
");

// Print directly
pdf.Print("HP LaserJet");

// Or save first
pdf.SaveAs("invoice.pdf");
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
    <h1>Invoice #12345</h1>
    <p>Customer: John Doe</p>
    <p>Amount: $150.00</p>
");

// Print directly
pdf.Print("HP LaserJet");

// Or save first
pdf.SaveAs("invoice.pdf");
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("
    <h1>Invoice #12345</h1>
    <p>Customer: John Doe</p>
    <p>Amount: $150.00</p>
")

' Print directly
pdf.Print("HP LaserJet")

' Or save first
pdf.SaveAs("invoice.pdf")
$vbLabelText   $csharpLabel

Lista kontrolna migracji

Przed migracją

  • Identify all RawPrint usage (SendBytesToPrinter, OpenPrinter, etc.)
  • Document printer names used in your application
  • Note any external PDF creation code that can be consolidated
  • Uzyskaj klucz licencyjnyIronPDFze strony ironpdf.com

Code Updates

  • Remove RawPrint package: dotnet remove package RawPrint
  • Install IronPdf package: dotnet add package IronPdf
  • Replace raw printing with pdf.Print()
  • Remove manual handle management (OpenPrinter, ClosePrinter, etc.)
  • Consolidate PDF creation and printing into single workflow
  • Dodaj inicjalizację licencji podczas uruchamiania aplikacji

Testowanie

  • Test printing to target printers
  • Verify print quality
  • Test multiple copies
  • Test silent printing
  • Wieloplatformowe verification if needed

Curtis Chau
Autor tekstów technicznych

Curtis Chau posiada tytuł licencjata z informatyki (Uniwersytet Carleton) i specjalizuje się w front-endowym rozwoju, z ekspertką w Node.js, TypeScript, JavaScript i React. Pasjonuje się tworzeniem intuicyjnych i estetycznie przyjemnych interfejsów użytkownika, Curtis cieszy się pracą z nowoczesnymi frameworkami i tworzeniem dobrze zorganizowanych, atrakcyjnych wizualnie podrę...

Czytaj więcej

Zespol wsparcia Iron

Jestesmy online 24 godziny, 5 dni w tygodniu.
Czat
Email
Zadzwon do mnie