Skip to main content

How to: Export an Image to PDF Format

Follow the steps below to use the RichEditDocumentServer to export an inline image to PDF format:

  1. Call the DocumentImageCollection.Append method to append an inline image. The DocumentImageSource.FromFile method creates an image source object from the given file.
  2. Set the SectionPage.Width and SectionPage.Height properties to adjust the page height and width to the image size. You can use the DocumentImage.Size property to resize the image itself.
  3. Use the RichEditDocumentServer.ExportToPdf method to export the result to PDF format.
using (RichEditDocumentServer server = new RichEditDocumentServer())
{
  //Insert an image
  DocumentImage docImage = server.Document.Images.Append(DocumentImageSource.FromFile("Image.jpg"));

  //Adjust the page width and height to the image's size
  server.Document.Sections[0].Page.Width = docImage.Size.Width + server.Document.Sections[0].Margins.Right + server.Document.Sections[0].Margins.Left;
  server.Document.Sections[0].Page.Height = docImage.Size.Height + server.Document.Sections[0].Margins.Top + server.Document.Sections[0].Margins.Bottom;

  //Export the result to PDF
  using (FileStream fs = new FileStream("result.pdf", FileMode.OpenOrCreate))
  {
     server.ExportToPdf(fs);
  }
}