PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 31, 2024 11:45 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 16 posts ] 
Author Message
PostPosted: Mon Nov 01, 2010 2:30 pm 
Offline

Joined: Mon Nov 01, 2010 2:23 pm
Posts: 17
Location: Prague, Czech Republic
Hello,

I am using your library to create PDF and RTF documents. Sometimes I need to insert an image which is not a file (it is dynamically created at runtime). What is the preferred method of adding it? Since your library only allows adding an image from a file, I always need to save the dynamic image to a temporary file, then add it and later delete this file. It works fine but sometimes I get an exception when trying to delete the temporary files. I do it right after the call to RtfDocumentRenderer.Render() finishes, but some of the files are still in use. When it is safe to delete them? Or is there a cleaner way to add such images?


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 14, 2010 11:22 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
I found it to be a very simple change to enhance the image class to have an AddImage overload that took a stream and a name instead of a filepath.

Essentially:
Code:
// Add a new Image constructor that takes a MemoryStream.
public Image(MemoryStream imageStream) : this()
{
    ImageSteam = imageStream;   // Added new property to hold the stream
   FileName = String.Empty;         // When no filename, we'll look for the stream.
}

// for convenience, added Image class property to indicate whether this image is file or stream
public bool StreamBased
{
    get { return ImageStream != null; }
}

// Altered Image.GetFilePath to return string.empty if the image is StreamBased.
// ---------------------------------------------------------------------

///Another file I needed to make changes to was in the ImageRenderer class  (ImageRenderer.cs)

1)  internal override void Format(Area area, FormatInfo previousFormatInfo)   needed to be altered slightly
     only perform the "does a bit of searching to try to locate the image" behavior when we're a file based image

2) in the Render() method, we needed to add a conditional for when image is StreamBased.
      if (image.StreamBased)
         xImage = XImage.FromStream(image.ImageStream)     <<-- XImage already knows how to load from stream so call it!
      else
          xImage = XImage.FromFile(formatInfo.ImagePath)

3) Exact same thing in the CalculateImageDimensions() method.



While a set of diffs would be the *best* way to convey this change info, this should get you on your way. It's really simple.

Cheers,
-Jeff


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 15, 2010 7:21 am 
Offline

Joined: Mon Nov 01, 2010 2:23 pm
Posts: 17
Location: Prague, Czech Republic
Thanks for the tip! I'll look into it. It would be worth suggesting the PDFsharp team to merge this code into the official codebase :wink:


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 22, 2012 5:35 pm 
Offline

Joined: Fri Feb 26, 2010 3:50 pm
Posts: 4
@jeffhare - any chance you could provide the full code for your changes that allow streams/memory based images please?

Thanks, TJ.


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 23, 2012 6:32 am 
Offline

Joined: Fri Apr 13, 2012 6:33 am
Posts: 10
This is how I do it (GDI version), no changes to pdfsharp required:

Code:
            // byte[] jpegdata contains the jpeg image
            using (MemoryStream ms = new MemoryStream(jpegdata)) {
                using (Image img = Image.FromStream(ms)) {
                    using (var xi = XImage.FromGdiPlusImage(img)) {
                        _pdfGfx.DrawImage(xi, ip.dx, ip.dy, ip.dw, ip.dh);
                    }
                }
            }

Danny


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 23, 2012 8:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
Hi!
dheijl wrote:
This is how I do it (GDI version), no changes to pdfsharp required
Your solution works with PDFsharp.
This thread is about MigraDoc. Jeff's solution works with MigraDoc unless you have to convert the document to MDDDL (and that's why the official codebase still does not support dynamic images with MigraDoc).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 23, 2012 11:12 am 
Offline

Joined: Fri Feb 26, 2010 3:50 pm
Posts: 4
Thanks Danny / Thomas, the solution may be straight forward and simple for some, but not for me. If anyone can provide the changes necessary to make this work, I'd be very grateful.

TJ


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 23, 2012 11:38 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
Jeff's post shows a new Image c'tor you have to add to Image.cs.
It also shows the changes that have to be done in ImageRenderer.cs.
Finally you have to add a new AddImage in Paragraph.cs that takes a stream (also requires a change in ParagraphElements.cs). Also AddImage to other classes where you need it (e.g. Section).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 25, 2013 7:23 am 
Offline

Joined: Fri Jan 25, 2013 7:20 am
Posts: 9
Hi,

one question....the suggested post with a new c'tor is from Dec 14, 2010 !
What is the reason that it's still not implemented in version 1.32?


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 28, 2013 9:29 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
wasers wrote:
one question....the suggested post with a new c'tor is from Dec 14, 2010!
What is the reason that it's still not implemented in version 1.32?
As mentioned above, this does not work with persistence in MDDDL files.
A good solution requires more than just this c’tor.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 06, 2013 8:24 am 
Offline

Joined: Tue Nov 05, 2013 5:08 am
Posts: 1
Hi Thomas,

We have just started looking at using MigraDoc and PDFSharp in our (commercial, closed-source) product. We rapidly came across the need to load images from Stream and encountered the problems with MDDDL, because we need to use DocumentPreview.

I've implemented support for a byte array containing the image data, following the syntax suggested in ParseImage(). This works well for the limited scenarios I have tested, and is certainly sufficient for our needs - documents of only a few pages, with images used for header and footer only.

Do you accept patch submissions?


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 06, 2013 9:31 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
stormcrow79 wrote:
Do you accept patch submissions?
I am not the Designer of MigraDoc.
If you submit the changed files we'll review them, test them, and maybe include the changes.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 29, 2014 5:44 pm 
Offline

Joined: Tue Jul 29, 2014 5:41 pm
Posts: 1
I too am struggling with this. XImage doesn't have a FromStream method. The full code would really help.


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 04, 2014 9:41 am 
Offline

Joined: Mon Aug 04, 2014 9:34 am
Posts: 1
I am also struggling with this exact same issue and I am having a hard time opening the solution file in VS2010 to make the changes. For some reason all the projects immediately get unloaded.


Regards,
Julien


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 23, 2015 8:58 pm 
Offline

Joined: Thu Apr 23, 2015 8:55 pm
Posts: 1
So a couple things with the solution provided above...

1. XImage doesn't have a FromStream method...atleast not in 1.32. Ok...So No Problem can do an XImage.FromGdiPlusImage(Image.FromStream(ms))

2. If the the Migra Doc Image object holds a reference to the stream, what happens when, externally someone disposes of that stream... I would suggest do a copy of the stream(resource intensive), or to make sure the user does not close the stream till after the pdf is rendered..


Thoughts, Ideas?


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 22, 2015 7:55 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1000
Location: CCAA
Hi!
crypto_rsa wrote:
Sometimes I need to insert an image which is not a file (it is dynamically created at runtime). What is the preferred method of adding it? Since your library only allows adding an image from a file, I always need to save the dynamic image to a temporary file, then add it and later delete this file.
With version 1.50 beta 2, there is a solution for this problem: pass the image as a string when you call AddImage():
http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
This method applies to MigraDoc only.
With PDFsharp you can create an XImage from a Stream.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 39 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