PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

MigraDoc - Create doc and send straight to printer
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2448
Page 1 of 1

Author:  dementedgeek [ Wed May 15, 2013 1:37 am ]
Post subject:  MigraDoc - Create doc and send straight to printer

Hello, I am trying to create a MigraDoc document (combination of text and images) and send it straight to a printer, no preview, no saving just straight to the printer. I had everything working just great until I tried to insert pictures as my pictures are streamed and do not reside on the hard disk. I have seen the post here and I have added the suggested lines of code to the classes. I am working with the solution file found in PDFSharp-MigraDocFoundation-1_32\MigraDoc\dev. I have made changes to the following files:

DocumentElements.cs
Section.cs
XImage.cs
ImageRenderer.cs
Image.cs

After making the suggested changes, the project builds without problems however, when I swap out the dll's and try to create and print the document, I receive a DefaultPageSetup must not be modified error. I am really close to accomplishing what I need to do here but these images are tripping me up! Any assistance would be greatly appreciated. Thanks!

Author:  Thomas Hoevel [ Wed May 15, 2013 8:33 am ]
Post subject:  Re: MigraDoc - Create doc and send straight to printer

Hi!
dementedgeek wrote:
I receive a DefaultPageSetup must not be modified error.
This is not an error, it is a Debug Assert (only showing with the DEBUG build, but not with the RELEASE build).

You should only modify clones of the DefaultPageSetup, not the DefaultPageSetup itself. Modifying the Default can lead to problems if you create two or more documents with different settings (no problem if all your documents use the same settings, but still not the way it is meant to be used).

Author:  dementedgeek [ Wed May 15, 2013 2:00 pm ]
Post subject:  Re: MigraDoc - Create doc and send straight to printer

Hello Thomas and thanks for the response. Ok, the clone method took care of my debug.assert issue however, I am still not able to add images from a stream. I am not using MDDDL's, is there not a set of compiled assemblies out there with this function built in? I have attached a printout of what I receive when trying to add pictures. My relevant code is posted below. Thanks in advance for any assistance!

Print Function
Code:
Public Sub Print(ByVal tempFile As String)
        If Not _bunkToPrint Is Nothing Then
            Dim myDoc As Document = CreateBunkDocument(tempFile)
            Dim render As DocumentRenderer = New DocumentRenderer(myDoc)
            Dim printDoc As MigraDocPrintDocument = New MigraDocPrintDocument()
            printDoc.Renderer = render
            printDoc.Renderer.PrepareDocument()
            printDoc.Print()
        End If
    End Sub


Create Document
Code:
Private Function CreateBunkDocument(ByVal template As String) As Document
        Dim mDoc As Document = New Document()
        Dim pgSetup As PageSetup = mDoc.DefaultPageSetup.Clone()
        pgSetup.TopMargin = 0
        pgSetup.LeftMargin = 0

        Dim xDoc As New XmlDocument()
        xDoc.Load(template)
        Dim root = xDoc.DocumentElement

        pgSetup.PageHeight = Convert.ToInt32(root.SelectSingleNode("Height").InnerText)
        pgSetup.PageWidth = Convert.ToInt32(root.SelectSingleNode("Width").InnerText)

        Dim section As Section = mDoc.AddSection()
        section.PageSetup = pgSetup

        Dim lblNodeList As XmlNodeList = root.GetElementsByTagName("Label")
        If lblNodeList.Count > 0 Then
            IterateLabelNodeList(lblNodeList, section)
        End If

        Dim ImgNodeList As XmlNodeList = root.GetElementsByTagName("Picture")
        If ImgNodeList.Count > 0 Then
            IterateImgNodeList(ImgNodeList, section)
        End If
       
        Return mDoc
    End Function


Get Image Values
Code:
Private Sub IterateImgNodeList(ByVal nodeList As XmlNodeList, docSection As Section)
        For Each element As XmlElement In nodeList
            Dim pic As TicketImage = New TicketImage()
            For Each node As XmlNode In element.ChildNodes
                Select Case node.Name
                    Case "Image"
                        pic.Img = node.InnerText
                    Case "Top"
                        pic.Y = Convert.ToInt32(node.InnerText)
                    Case "Left"
                        pic.X = Convert.ToInt32(node.InnerText)
                    Case "Height"
                        pic.Height = Convert.ToInt32(node.InnerText)
                    Case "Width"
                        pic.Width = Convert.ToInt32(node.InnerText)
                End Select
            Next
            CreateSectionImage(pic, docSection)
        Next
    End Sub


Add Image to Document Section
Code:
Private Sub CreateSectionImage(ByVal ti As TicketImage, sec As Section)
        Dim img As Image = sec.AddImage(PicStream(ti.Img))
        img.RelativeVertical = RelativeVertical.Page
        img.Height = ti.Height
        img.Width = ti.Width
        img.Top = ti.Y
        img.Left = ti.X
    End Sub


Image Stream
Code:
Private Function PicStream(ByVal xmlString As String) As MemoryStream
        Dim bytes As Byte() = Convert.FromBase64String(xmlString)
        Dim stream As MemoryStream = New MemoryStream(bytes, 0, bytes.Length)
        Return stream
    End Function


Attachments:
File comment: Images are not showing?
migra.jpg
migra.jpg [ 28.11 KiB | Viewed 8439 times ]

Author:  dementedgeek [ Wed May 15, 2013 3:00 pm ]
Post subject:  Re: MigraDoc - Create doc and send straight to printer

Ok, persistence pays off, problem solved! I had left out a bit of code in the ImageRender class. :roll:

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/