PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 3:08 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Img .JPG on Windows 7
PostPosted: Mon Apr 07, 2014 10:20 am 
Offline

Joined: Mon Apr 07, 2014 7:34 am
Posts: 7
I searched in the forum, but did not find any solution for the problem with the creation of PDF, using a JPG as background on Windows 7 64bit.
My image is .JPG, 32-bit CMYK.
The PdfSharp dll version that we use is ā€œ1.32.2608.0ā€

When my software creates the PDF, the background image loses color and repeats the image several times.
On XP this does not happen.

This is my code
Code:
try{
   PdfDocument document = new PdfDocument();
   PdfPage page = document.AddPage();
   document.Options.NoCompression = true;
   document.Options.ColorMode = PdfColorMode.Cmyk;
   document.Version = 14;
   page.Size = PdfSharp.PageSize.A4;
   XImage xImagePosterBackground = XImage.FromFile("c:\\img_test.jpg");
   XGraphics graphics = XGraphics.FromPdfPage(page);
   graphics.DrawImage(xImagePosterBackground, new XPoint(0, 0));
   string fileName = "test_jpg.pdf";
   document.Save("c:\\" + fileName);
}
catch (System.Exception ex)
{
   string s = ex.ToString();
   System.Diagnostics.Debug.Assert(false, "Error!");
}


Do you have any suggestions?


Attachments:
File comment: left side is an PDF created by Win7, right side is an PDF crated by XP
errore.png
errore.png [ 16.9 KiB | Viewed 8978 times ]
Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 11:26 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
You can try the code modification suggested by SiliconMind here:
viewtopic.php?p=6963#p6963

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 12:44 pm 
Offline

Joined: Mon Apr 07, 2014 7:34 am
Posts: 7
Iā€™m sincere, I did not understand what part of the code I should look
The problem is the DLL or the operating system?
By 2011, the problem has not been fixed?
How can I change posted code to get something that work?


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 12:53 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
File PdfImage.cs, class PdfImage, method "void InitializeJpeg()"

Look for "Save(memory, ImageFormat.Jpeg);" and replace with the suggested code.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 2:09 pm 
Offline

Joined: Mon Apr 07, 2014 7:34 am
Posts: 7
so,
I've download the entire solution, BuildAll-PDFsharp-VS2010.sln
Of class PDFImage.cs, I modified the method "void InitializeJpeg ()", changing the line
Code:
image.gdiImage.Save (memory, ImageFormat.Jpeg);

with
Code:
if (! image.path.StartsWith ("*"))
       {
           using (FileStream sourceFile = File.OpenRead (image.path))
           {
               int count = 0;
               byte [] buffer = new byte [1024];
               memory = new MemoryStream ((int) sourceFile.Length);
               do
               {
                   count = sourceFile.Read (buffer, 0, buffer.Length);
                   memory.Write (buffer, 0, buffer.Length);
               }
               while (count> 0);
           }
       }
       else
       {
           memory = new MemoryStream ();
           image.gdiImage.Save (memory, ImageFormat.Jpeg);
       }


I rebuilt project
I took the new dll and I have included it in my project.
The end result is not changed.


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 2:47 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Which DLL? "PdfSharp.dll" or "PdfSharp-WPF.dll"? The patch is for the former only.
If you are using "PdfSharp-WPF.dll", try "PdfSharp.dll" instead.
Make sure there are no old copies in the various BIN/DEBUG folders.

Tip: if you add a reference to the .CSPROJ files (instead of referencing the DLLs) you can step through PDFsharp.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 3:40 pm 
Offline

Joined: Mon Apr 07, 2014 7:34 am
Posts: 7
as I write you before, I'm using solution BuildAll-PdfSharp-VS2010.sln
I've downloaded it from sourceforge (sourceforge.net/projects/pdfsharp/files/pdfsharp/)
version: PDFsharp 1.32 - 2012-03-07

I've include my personal solution inside your solution and debug single steps just to be sure I'm using right source.
I think this produce "PdfSharp.dll" and not "PdfSharp-WPF.dll"

No changes for my output.


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 4:06 pm 
Offline

Joined: Mon Apr 07, 2014 7:34 am
Posts: 7
Code:
   int count = 0;
   byte[] buffer = new byte[1024];
   memory = new MemoryStream((int)sourceFile.Length); // sourceFile.Length = 2462478
   do
   {
     count = sourceFile.Read(buffer, 0, buffer.Length);
     memory.Write(buffer, 0, buffer.Length);
   }
   while (count > 0);
        // memory.Length = 2463744

and, if I use old command
Code:
image.gdiImage.Save(memory, ImageFormat.Jpeg);

the value assumed by the variable "memory" is 2462516

So, original image length is 2462478
using the new statement the length is 2463744
using original statement the length is 2462516


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 07, 2014 4:18 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
It could be the problem described in the other thread.
We don't have the JPEG image, we don't have an SSCCE, so there is not much we can do now.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 08, 2014 12:06 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
GDI+ supports the following image flags:
Code:
    ColorSpaceRgb = 16,
    ColorSpaceCmyk = 32,
    ColorSpaceGray = 64,
    ColorSpaceYcbcr = 128,
    ColorSpaceYcck = 256,


For your file, the flag ColorSpaceRgb is set. I presume under Windows XP the flag ColorSpaceYcck is set and image shows correctly.
PDFsharp lets Windows analyze the file. This sometimes fails under Windows 7 (and later).
Adobe Reader also does not analyze the file, it relies on the flags given in the PDF file.

If all your JPEG files are CMYK, then go to PdfImage.cs, method InitializeJpeg and replace this line
Code:
Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB");

with those:
Code:
Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK");
Elements["/Decode"] = new PdfLiteral("[1 0 1 0 1 0 1 0]");


If you use both RGB and CMYK JPEGs: sorry, I currently have no idea.

BTW: That image is a good candidate for lossless compression (e.g. PNG) instead of JPEG. Might even reduce the file size.


Re different sizes:
zoranm80 wrote:
count = sourceFile.Read(buffer, 0, buffer.Length);
memory.Write(buffer, 0, buffer.Length);
The second line should be
Code:
memory.Write(buffer, 0, count)

This resolves the size difference - but image still doesn't work.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 416 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