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.
Install the Pdfcrowd API client library for .NET.
In the following examples, do not forget to replace "username"
and "apikey"
with your username and API key.
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()); } } }
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);
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)
try { // .. // call the API } catch(pdfcrowd.Error why) { // why.ToString() returns an error message }
Try ' ... ' call the API Catch why As pdfcrowd.Error ' why.ToString() returns an error message End Try
Provides access to the Pdfcrowd API from your .NET applications.
public Client(string username, string apikey)
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)
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)
public void setFooterHtml(string html)
public void setFooterUrl(string url)
public void setHeaderHtml(string html)
public void setHeaderUrl(string url)
public void setHeaderFooterPageExcludeList(string exclist)
public void setPageNumberingOffset(int offset)
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).
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_PAGE
CONTINUOUS
CONTINUOUS_FACING
public void setPageMode(int value)
FULLSCREEN
- Full-screen mode.public void setInitialPdfZoomType(int value)
FIT_WIDTH
FIT_HEIGHT
FIT_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)
public void setWatermark(string url, string offset_x, string offset_y)
public void setWatermarkRotation(double angle)
public void setWatermarkInBackground(bool value)
public void useSSL(bool use_ssl)
public int numTokens()
public void setMaxPages(int npages)
public void setFailOnNon200(bool value)
Derived from System.Exception
. It is thrown when an error
occurs. Use ToString()
method to get the error message.
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"
.