Complete .NET examples for common Image to PDF tasks. See the .NET SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each .NET example can be copied and run independently.
Choose file, in-memory, or output-stream handling for the PDF result. Stream examples use a binary file stream as their destination.
Upload an existing logo.png file.
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.convertFileToFile("logo.png", "logo.pdf"); } }
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); byte[] pdf = client.convertFile("logo.png"); Console.WriteLine(pdf.Length); } }
using System; using System.IO; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); using (Stream output_stream = File.Create("logo.pdf")) { client.convertFileToStream("logo.png", output_stream); } } }
Fetch an image from its URL.
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.convertUrlToFile("https://your-server.com/logo.png", "logo.pdf"); } }
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); byte[] pdf = client.convertUrl("https://your-server.com/logo.png"); Console.WriteLine(pdf.Length); } }
using System; using System.IO; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); using (Stream output_stream = File.Create("logo.pdf")) { client.convertUrlToStream( "https://your-server.com/logo.png", output_stream ); } } }
Read logo.png as binary data. In an application, pass the bytes you already hold in memory instead.
using System; using System.IO; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); byte[] image_data_1 = File.ReadAllBytes("logo.png"); client.convertRawDataToFile(image_data_1, "logo.pdf"); } }
using System; using System.IO; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); byte[] image_data_1 = File.ReadAllBytes("logo.png"); byte[] pdf = client.convertRawData(image_data_1); Console.WriteLine(pdf.Length); } }
using System; using System.IO; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); using (Stream output_stream = File.Create("logo.pdf")) { byte[] image_data_1 = File.ReadAllBytes("logo.png"); client.convertRawDataToStream(image_data_1, output_stream); } } }
Open logo.png as a binary input stream and save the result to logo.pdf.
using System; using System.IO; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); using (Stream input_stream = File.OpenRead("logo.png")) { client.convertStreamToFile(input_stream, "logo.pdf"); } } }
These examples use scan.png, a 1224 × 1584 pixel image with text, a chart, and a uniform white border. The rotation example uses its sideways variant, scan-sideways.png.
Fit the whole image onto A4 portrait or Letter landscape paper with 12mm margins and 150 DPI. The fit mode preserves the image's aspect ratio. Setting a page size without it can clip a larger image.
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setPageSize("A4"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "a4.pdf"); } }
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setOrientation("landscape"); client.setPageSize("Letter"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "landscape.pdf"); } }
Fit scan.png onto a 4 × 6 inch page and save custom-page.pdf. Preserve the aspect ratio and fill the unused page area with a pale blue background.
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setPageBackgroundColor("EEF2F6"); client.setPageHeight("6in"); client.setPageWidth("4in"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "custom-page.pdf"); } }
Interpret scan.png at 300 pixels per inch without setting a fixed page size. Its 1224 × 1584 pixels produce a 4.08 × 5.28 inch page in print-size.pdf, with the original image pixels retained.
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(300); client.convertFileToFile("scan.png", "print-size.pdf"); } }
Extract the chart from scan.png into chart.pdf, or remove its uniform outer border and save trimmed.pdf.
The chart starts 96 pixels from the left and 370 pixels from the top, and measures 1024 × 384 pixels. Set 96 DPI so the px crop coordinates match the source image pixels; adjust the region for your image. Automatic border removal works on solid-color borders; use an explicit crop for uneven scan edges.
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setCropAreaHeight("384px"); client.setCropAreaWidth("1024px"); client.setCropAreaX("96px"); client.setCropAreaY("370px"); client.setDpi(96); client.convertFileToFile("scan.png", "chart.pdf"); } }
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setRemoveBorders(true); client.convertFileToFile("scan.png", "trimmed.pdf"); } }
Rotate scan-sideways.png 90 degrees clockwise to restore the upright orientation, then save upright.pdf.
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setRotate("90"); client.convertFileToFile("scan-sideways.png", "upright.pdf"); } }
Render scan.png onto A4 paper at 72 DPI and save compact.pdf. Keep the same fit and 12mm margins as the 150 DPI A4 example above, while reducing resolution for screen viewing. Smaller files come at the cost of fine detail.
using System; class Example { static void Main() { var client = new pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(72); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setPageSize("A4"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "compact.pdf"); } }