PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Jul 16, 2024 5:47 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sun Oct 16, 2011 8:21 pm 
Offline

Joined: Sun Oct 16, 2011 7:57 am
Posts: 8
Hello I try to put it in a pdf file of images (one per page) and the problem is that the size of the pdf and the same as the original image.
I try to resize it but nothing did.
I wanted to put these images in PDF format in order to reduce weight but it is not won. :cry:
Thank you for your help
+ + @ Lolo818
Code:
                    PdfDocument document = new PdfDocument();
                    XFont font = new XFont("Times", 15, XFontStyle.Bold);
                    XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.WinAnsi, PdfFontEmbedding.None);
                    PdfPage page = document.AddPage();
                    page.Size = PageSize.A4;
                    XGraphics gfx = XGraphics.FromPdfPage(page);
                    page.Orientation = PageOrientation.Landscape;
                    gfx.DrawString("Photo 1", font,XBrushes.DarkRed, new XRect(0, 0, page.Width, page.Height),XStringFormats.TopCenter);
                    string NomFichier = openFileDialog1.Title.ToString();
                    XImage img1 = Pic1Resize;
                    Rectangle XRect= new Rectangle(0, 0, Pic1Resize.Width, Pic1Resize.Height);
                    gfx.DrawImage(Pic1Resize,XRect, XRect, XGraphicsUnit.Millimeter);


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 17, 2011 8:00 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Hi!

I'm afraid I don't understand what you try to achieve.
lolo818 wrote:
Code:
Rectangle XRect= new Rectangle(0, 0, Pic1Resize.Width, Pic1Resize.Height);
gfx.DrawImage(Pic1Resize,XRect, XRect, XGraphicsUnit.Millimeter);

AFAIK the method .Width() returns the size of the image in pixels. So I assume your DrawImage call draws the images with one pixel per millimetre. This will not work for large images.

You'd better use
public void DrawImage(XImage image, XRect rect)
or
public void DrawImage(XImage image, int x, int y, int width, int height)
and specify the area where you want the image to appear (all co-ordinates must be given in Points (not pixels). Use class Unit for conversion from e.g. millimetre.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 18, 2011 6:02 pm 
Offline

Joined: Sun Oct 16, 2011 7:57 am
Posts: 8
Hello, thank you for your help, the PDF file is created is heavier than the original or equal.
Purpose of my code is to reduce the weight of the image in the rasemblant in a pdf file.
example: a photo in jpeg 47K bytes to 247 Kbytes made ​​in pdf
I am a beginner please m'escuser if I make large coding error. :oops:

Code:
                    XImage img1 = Pic1Resize;
                    XRect Rec = new XRect(0, 0, Pic1Resize.Width, Pic1Resize.Height);
                    gfx.DrawImage(img1, Rec);


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 19, 2011 8:18 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
lolo818 wrote:
example: a photo in jpeg 47K bytes to 247 Kbytes made ​​in pdf
JPEG files are embedded into PDF without modification, so the PDF file will normally be larger than the JPEG file.
It shouldn't grow from 47 kB to 247 kB - but we'd need the JPEG and the PDF to tell where the 200 kB come from.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 19, 2011 6:20 pm 
Offline

Joined: Sun Oct 16, 2011 7:57 am
Posts: 8
Good evening, I remade a test, but without success, the original image is always lighter than the generated pdf.
I put a picture and the PDF created with this code, is there no way to compress to ease the pdf file?
Thank you for your help.

Code:
                        PdfDocument document = new PdfDocument();
                        PdfPage page = document.AddPage();
                        page.Size = PageSize.A4;
                        XGraphics gfx = XGraphics.FromPdfPage(page);
                        page.Orientation = PageOrientation.Landscape;
                        XImage img1 = Pic1Resize;
                        XRect Rec = new XRect(0, 0, Pic1Resize.Width, Pic1Resize.Height);
                        gfx.DrawImage(img1, Rec);
                        document.Save(saveFileDialog1.FileName);


Attachments:
Pic1.JPG
Pic1.JPG [ 15.33 KiB | Viewed 8477 times ]
test.zip [71.94 KiB]
Downloaded 455 times
Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 20, 2011 7:11 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
lolo818 wrote:
I remade a test, but without success, the original image is always lighter than the generated pdf.

The image included in the PDF file is not a JPEG image.

Your code snippet doesn't show where Pic1Resize comes from. I guess it's not loaded from a JPEG file (maybe it's originally loaded from a JPEG file, but modified later on).

If your code resizes the image: save that image as JPEG (specifying the image quality you need) and load it again. Then the PDF file will contain a JPEG compressed image (much smaller).
Saving to a memory stream and loading from that memory stream should do (no file I/O involved).

I tried this code:
Code:
XImage image = XImage.FromFile(@"C:\Users\thho\Desktop\$$$\$$$PDFsharp\PicResize\pic1.JPG");
gfx.DrawImage(image, 0, 0, image.PixelWidth * 2, image.PixelHeight * 2);

The resulting PDF file was 15 kiB in size (15,108 bytes) while your JPEG image has 15,700 bytes. PDF file is smaller here!
I include the PDF.

Calling "XImage.FromGdiPlusImage(Image.FromStream(stream))" should do the trick.

Or add a new "FromStream" to the XImage class:
Code:
/// <summary>
/// Creates an image from the specified stream.
/// </summary>
public static XImage FromStream(Stream stream)
{
  return new XImage(stream);
}


Attachments:
HelloWorld_tempfile.zip [14.58 KiB]
Downloaded 468 times

_________________
Regards
Thomas Hoevel
PDFsharp Team
Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 20, 2011 9:15 pm 
Offline

Joined: Sun Oct 16, 2011 7:57 am
Posts: 8
Good evening, I actually was retouched bitmap after loading, I reformatted with a JPEG stream now everything is ok.
Thank you for your help and your patiently!
:D


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

All times are UTC


Who is online

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