使用SilverPDF导出Silverlight UI的多页PDF

时间:2011-03-26 10:56:51

标签: silverlight pdf itextsharp

这是我的第一篇文章,我真的很沮丧使用Silverlight只因为我是新手。

我有三个Silverlight UI(基本上是StackPanels)“stkMain1”,“stkMain2”和“stkMain3”。

我必须将这三个堆栈面板转换为PDF。我正在使用silverPDF(我猜它还使用了iTextSharp和PDFSharp。)

我写了以下代码:

Private Sub cmdImage_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles cmdImage.Click
    Dim d As New SaveFileDialog()
    d.Filter = "PDF file format|*.pdf"

    ' Save the document...
    If d.ShowDialog() = True Then
        stkMain.Children.Clear()
        stkMain.Children.Add(stkMain1)

        Dim document As New PdfDocument()
        Dim page As PdfPage = document.AddPage
        Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
        Dim img As ImageTools.ExtendedImage = BillPage1.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
End Sub

这样可以解决所有问题并提供一页PDF文件。精湛的输出和竖起大拇指。 现在真正的问题开始了:

Private Sub cmdImage_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles cmdImage.Click
    Dim d As New SaveFileDialog()
    d.Filter = "PDF file format|*.pdf"

    ' Save the document...
    If d.ShowDialog() = True Then
        stkMain.Children.Clear()
        stkMain.Children.Add(BillPage1)

        Dim document As New PdfDocument()
        Dim page As PdfPage = document.AddPage
        Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
        Dim img As ImageTools.ExtendedImage = BillPage1.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)


        document.Pages.Add()

        Dim page1 As PdfPage = document.AddPage
        Dim gfx1 As XGraphics = XGraphics.FromPdfPage(page1)
        Dim img1 As ImageTools.ExtendedImage = BillPage2.ToImage
        Dim mstream1 As New MemoryStream()
        Dim encoder1 As New JpegEncoder()
        encoder1.Encode(img1, mstream1)
        mstream1.Seek(0, SeekOrigin.Begin)
        Dim pdfImg1 As XImage = XImage.FromStream(mstream1)
        gfx1.DrawImage(pdfImg1, 0, 0)


        document.Save(d.OpenFile())
    End If
End Sub

现在这会创建两个页面,我得到一个混乱的输出,堆栈面板的内容相互重叠。

如何解决这个问题?我需要帮助太多。该项目是自己的,我必须在2011年3月26日(星期一)之前提交。

提前致谢 拉维

1 个答案:

答案 0 :(得分:1)

试试这个:

If d.ShowDialog() = True Then
    Dim document As New PdfDocument()
    Dim page As PdfPage = document.AddPage
    Dim pdfImg As XImage = DrawUI(stkMain1)
    gfx.DrawImage(pdfImg, 20, 20)
    page = document.AddPage()
    gfx = XGraphics.FromPdfPage(page)
    pdfImg = DrawUI(stkMain2)
    page = document.AddPage()
    gfx = XGraphics.FromPdfPage(page)
    gfx.DrawImage(pdfImg, 20, 20)
    pdfImg = DrawUI(stkMain3)
    gfx.DrawImage(pdfImg, 20, 20)
    document.Save(d.OpenFile())
End If


Private Function DrawUI(ByVal oControl As Object) As XImage
    Dim img As ImageTools.ExtendedImage = ImageExtensions.ToImage(oControl)
    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)
    Return pdfImg
End Function