Export image to PDF in Silverlight

After a few hours serching a way to put a image into pdf in silverlight, something worked out, here goes:

Libraries used:
Imports silverPDF
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports iTextSharp.text
Imports ImageTools
Imports ImageTools.IO.Jpeg
Imports ImageTools.ImageBase
Imports ImageTools.Helpers
Imports ImageTools.ImageExtensions

Code snippet (vb):

        Dim d As New SaveFileDialog()
        d.Filter = "PDF file format|*.pdf"
        ' Save the document...
        If d.ShowDialog() = True Then
            Dim document As New PdfDocument()
            Dim page As PdfPage = document.AddPage()
            Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
            Dim img As ImageTools.ImageBase = Me.canvas1.ToImage()
            Dim mstream As New MemoryStream()
            Dim encoder As New JpegEncoder()
            encoder.Encode(img, mstream)
            mstream.Seek(0, SeekOrigin.Begin)
            Dim pdfImg As XImage = XImage.FromStream(mstream)
            gfx.DrawImage(pdfImg, 0, 0)
        End If

CodeSnippet (C#):

SaveFileDialog d = new SaveFileDialog();
d.Filter = "PDF file format|*.pdf";

// Save the document...
if (d.ShowDialog() == true) {
	PdfDocument document = new PdfDocument();
	PdfPage page = document.AddPage();
	XGraphics gfx = XGraphics.FromPdfPage(page);

	ImageTools.ImageBase img = this.canvas1.ToImage();
	MemoryStream mstream = new MemoryStream();
	JpegEncoder encoder = new JpegEncoder();
	encoder.Encode(img, mstream);
	mstream.Seek(0, SeekOrigin.Begin);
	XImage pdfImg = XImage.FromStream(mstream);
	gfx.DrawImage(pdfImg, 0, 0);



Deixe uma resposta

O seu endereço de email não será publicado. Campos obrigatórios marcados com *