Legacy Pdfcrowd API v1 for .NET
This is the documentation of the .NET client library for the legacy Pdfcrowd API v1. We strongly recommend the new improved Pdfcrowd API v2 for new integrations.
Installation
Install the Pdfcrowd API client library for .NET.
Getting Started
In the following examples, do not forget to replace "username" and "apikey" with your username and API key.
HTML to PDF C# Example Application
The following code shows how to convert a web page, raw HTML code, and a local HTML file:
using System; using System.IO; public class PdfcrowdTest { static void Main() { try { FileStream fileStream; // create an API client instance pdfcrowd.Client client = new pdfcrowd.Client( "your_username", "your_apikey" ); // convert a web page and save the PDF to a file fileStream = new FileStream("google_com.pdf", FileMode.CreateNew); client.convertURI("http://www.google.com", fileStream); fileStream.Close(); // convert an HTML string and store the PDF into a memory stream MemoryStream memStream = new MemoryStream(); string html = "<head></head><body>My HTML Layout</body>"; client.convertHtml(html, memStream); // convert an HTML file fileStream = new FileStream("file.pdf", FileMode.CreateNew); client.convertFile("c:/local/file.html", fileStream); fileStream.Close(); // retrieve the number of credits in your account int ncredits = client.numTokens(); } catch(pdfcrowd.Error why) { System.Console.WriteLine(why.ToString()); } } }
HTML to PDF in ASP.NET - C#
The following code converts a web page using C# ASP.NET and sends the generated PDF as an HTTP response:
<%-- file: PdfGenerator.aspx --%> <%@ Page Language="C#" CodeFile="PdfGenerator.aspx.cs" Inherits="Website.PdfGenerator" AutoEventWireup="true" %>
// file: PdfGenerator.aspx.cs using System; using System.IO; namespace Website { public partial class PdfGenerator : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response; try { // create an API client instance pdfcrowd.Client client = new pdfcrowd.Client( "your_username", "your_apikey" ); // convert a web page and write the generated PDF to a memory stream MemoryStream Stream = new MemoryStream(); client.convertURI("http://www.google.com", Stream); // set HTTP response headers Response.Clear(); Response.AddHeader("Content-Type", "application/pdf"); Response.AddHeader("Cache-Control", "max-age=0"); Response.AddHeader("Accept-Ranges", "none"); Response.AddHeader( "Content-Disposition", "attachment; filename=google_com.pdf" ); // send the generated PDF Stream.WriteTo(Response.OutputStream); Stream.Close(); Response.Flush(); Response.End(); } catch(pdfcrowd.Error why) { Response.Write(why.ToString()); } } } }
You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():
client.convertHtml("<head></head><body>My HTML Layout</body>", Stream);
The API lets you also convert a local HTML file:
client.convertFile("c:/MyLayout.html", Stream);
HTML to PDF in ASP.NET - Visual Basic
The following code converts a web page using Visual Basic ASP.NET and sends the generated PDF as an HTTP response:
<%-- file: PdfGenerator.aspx --%> <%@ Page Language="VB" CodeFile="PdfGenerator.aspx.vb" Inherits="Website.PdfGenerator" AutoEventWireup="true" %>
' file: PdfGenerator.aspx.vb Imports System Imports System.IO Namespace Website Public Partial Class PdfGenerator Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim Response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response Try ' create an API client instance Dim client As New pdfcrowd.Client("your_username", "your_apikey") ' convert a web page and write the generated PDF to a memory stream Dim Stream As New MemoryStream client.convertURI("http://www.google.com", Stream) ' set HTTP response headers Response.Clear() Response.AddHeader("Content-Type", "application/pdf") Response.AddHeader("Cache-Control", "max-age=0") Response.AddHeader("Accept-Ranges", "none") Response.AddHeader("Content-Disposition", "attachment; filename=google_com.pdf") ' send the generated PDF Stream.WriteTo(Response.OutputStream) Stream.Close() Response.Flush() Response.End() Catch why As pdfcrowd.Error Response.Write(why.ToString()) End Try End Sub End Class End Namespace
You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():
client.convertHtml("<head></head><body>My HTML Layout</body>", Stream)
The API lets you also convert a local HTML file:
client.convertFile("c:/MyLayout.html", Stream)
Error Handling
C#
try { // .. // call the API } catch(pdfcrowd.Error why) { // why.ToString() returns an error message }
Visual Basic
Try ' ... ' call the API Catch why As pdfcrowd.Error ' why.ToString() returns an error message End Try
API Reference
class pdfcrowd.Client
Provides access to the Pdfcrowd API from your .NET applications.
Constructor
public Client(string username, string apikey)
Conversion
public void convertHtml(string html, System.IO.Stream outstream)
public void convertFile(string fpath, System.IO.Stream outstream)
public void convertURI(string url, System.IO.Stream outstream)
Page Setup
public void setPageWidth(double/string value)
public void setPageHeight(double/string value)
public void setPageMargins(string top, string right, string bottom, string left)
public void setHorizontalMargin(double/string value)
public void setVerticalMargin(double/string value)
Header and Footer
public void setFooterHtml(string html)
- %u - URL to convert.
- %p - The current page number.
- %n - Total number of pages.
public void setFooterUrl(string url)
public void setHeaderHtml(string html)
public void setHeaderUrl(string url)
public void setHeaderFooterPageExcludeList(string exclist)
Example: "1,-1" will not print the header and footer on the first and the last page.
public void setPageNumberingOffset(int offset)
Example: if set to "1" then the page numbering will start with 1 on the second page.
HTML options
public void enableImages(bool value)
public void enableBackgrounds(bool value)
public void setHtmlZoom(float value)
public void enableJavaScript(bool value)
public void enableHyperlinks(bool value)
public void setDefaultTextEncoding(string value)
utf-8.
public void usePrintMedia(bool value)
print CSS media type
is used (if available).
PDF options
public void setEncrypted(bool value)
public void setUserPassword(string pwd)
public void setOwnerPassword(string pwd)
public void setNoPrint(bool value)
public void setNoModify(bool value)
public void setNoCopy(bool value)
public void setPageLayout(int value)
SINGLE_PAGECONTINUOUSCONTINUOUS_FACING
public void setPageMode(int value)
FULLSCREEN- Full-screen mode.
public void setInitialPdfZoomType(int value)
FIT_WIDTHFIT_HEIGHTFIT_PAGE
public void setInitialPdfExactZoom(float value)
public void setPdfScalingFactor(float value)
public void setPageBackgroundColor(string value)
public void setTransparentBackground(bool value)
body {background-color:rgba(255,255,255,0.0);}
public void setAuthor(string author)
Watermark
public void setWatermark(string url, string offset_x, string offset_y)
public void setWatermarkRotation(double angle)
public void setWatermarkInBackground(bool value)
Miscellaneous
public void useSSL(bool use_ssl)
public int numTokens()
public void setMaxPages(int npages)
public void setFailOnNon200(bool value)
class pdfcrowd.Error
Derived from System.Exception. It is thrown when an error
occurs. Use ToString() method to get the error message.
Units
Page dimensions and margins can be specified in inches (in), millimeters (mm),
centimeters (cm), or points (pt). If no units are specified, points are
assumed. Examples: "210mm", "8.5in".