Back to Devexpress

How to: Export an Image to PDF Format

officefileapi-119894-word-processing-document-api-examples-export-how-to-export-an-image-to-pdf-format.md

latest2.6 KB
Original Source

How to: Export an Image to PDF Format

  • Sep 19, 2023

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.
csharp
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);
  }
}
vb
Using server As New RichEditDocumentServer()
  'Insert an image
  Dim docImage As DocumentImage = 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 fs As New FileStream("result.pdf", FileMode.OpenOrCreate)
     server.ExportToPdf(fs)
  End Using
End Using