PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Jul 04, 2024 4:24 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed May 15, 2013 1:37 am 
Offline

Joined: Mon May 13, 2013 5:55 pm
Posts: 3
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!


Top
 Profile  
Reply with quote  
PostPosted: Wed May 15, 2013 8:33 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed May 15, 2013 2:00 pm 
Offline

Joined: Mon May 13, 2013 5:55 pm
Posts: 3
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 8443 times ]
Top
 Profile  
Reply with quote  
PostPosted: Wed May 15, 2013 3:00 pm 
Offline

Joined: Mon May 13, 2013 5:55 pm
Posts: 3
Ok, persistence pays off, problem solved! I had left out a bit of code in the ImageRender class. :roll:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 248 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group