PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Apr 25, 2024 12:40 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Sat Feb 23, 2013 3:26 pm 
Offline

Joined: Sat Feb 23, 2013 3:07 pm
Posts: 4
Hi,
in the export image example, i only get:
// TODO: You can put the code here that converts vom PDF internal image format to a Windows bitmap

// and use GDI+ to save it in PNG format.

// It is the work of a day or two for the most important formats. Take a look at the file

// PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.

// We don't need that feature at the moment and therefore will not implement it.

// If you write the code for exporting images I would be pleased to publish it in a future release

// of PDFsharp.
I want to know how can i convert the /FlateDecode Image to Image object, so I can display it on the picturebox control.
Thanks!


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 25, 2013 9:13 am 
Offline
PDFsharp Guru
User avatar

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

"/FlateDecode" gives you a Bitmap in PDF format. Convert it to e.g. a Windows Bitmap and display it.
PDF bitmaps can have 1 bit, 8 bits, 24 bits, 32 bits and other variations.

See also:
viewtopic.php?p=5811#p5811
viewtopic.php?p=5564#p5564

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 01, 2013 5:37 am 
Offline

Joined: Sat Feb 23, 2013 3:07 pm
Posts: 4
Thomas, Thanks!
If i have other question, I will give you a post.


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 02, 2013 8:52 am 
Offline

Joined: Sat Feb 23, 2013 3:07 pm
Posts: 4
static Image ExportAsPngImage(PdfDictionary image, ref int count)
{
int width = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.Width);
int height = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.Height);
int bitsPerComponent = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.BitsPerComponent);
System.Drawing.Imaging.PixelFormat pixelFormat = new System.Drawing.Imaging.PixelFormat();

PdfSharp.Pdf.PdfArray arr = image.Elements.GetArray(PdfSharp.Pdf.Advanced.PdfImage.Keys.ColorSpace);
switch (bitsPerComponent)
{
case 1:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}

PdfSharp.Pdf.Filters.FlateDecode fd = new PdfSharp.Pdf.Filters.FlateDecode();

byte[] origineel_byte_boundary = image.Stream.UnfilteredValue;//fd.Decode(image.Stream.Value);
byte[] resultaat_byte_boundary = null;
int new_width = width;
int allignment = 4;

if (new_width % allignment != 0)
//Image data in BMP files always starts at a DWORD boundary, in PDF it starts at a BYTE boundary.
//Most images have a width that is a multiple of 4, so there is no problem with them.
//You must copy the image data line by line and start each line at the DWORD boundary.
{
while (new_width % allignment != 0)
new_width++;
byte[,] copy_dword_boundary = new byte[height, new_width];

for (int y = 0; y < height; y++)
{
for (int x = 0; x < new_width; x++)
{
if (x <= width && (x + (y * width) != origineel_byte_boundary.Length))
// while not at end of line, take orignale array
copy_dword_boundary[y, x] = origineel_byte_boundary[x + (y * width)];
else //fill new array with ending 0
copy_dword_boundary[y, x] = 0;
}
}
resultaat_byte_boundary = new byte[new_width * height];
int counter = 0;
for (int x = 0; x < copy_dword_boundary.GetLength(0); x++)
{
for (int y = 0; y < copy_dword_boundary.GetLength(1); y++)
{ //put 2dim array back in 1dim array
resultaat_byte_boundary[counter] = copy_dword_boundary[x, y];
counter++;
}
}
}
else
{
resultaat_byte_boundary = new byte[new_width * height];
origineel_byte_boundary.CopyTo(resultaat_byte_boundary, 0);
}
Bitmap bmp = new Bitmap(new_width, height, pixelFormat);
System.Drawing.Imaging.BitmapData bmd = bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.WriteOnly,bmp.PixelFormat);
System.Runtime.InteropServices.Marshal.Copy(resultaat_byte_boundary, 0, bmd.Scan0, resultaat_byte_boundary.Length);
bmp.UnlockBits(bmd);
using (FileStream fs = new FileStream("C:\\" + String.Format("Image{0}.bmp", count), FileMode.Create, FileAccess.Write))
{
bmp.Save(fs, ImageFormat.Bmp);
count++;
}
return (Image)bmp;

// TODO: You can put the code here that converts vom PDF internal image format to a Windows bitmap

// and use GDI+ to save it in PNG format.

// It is the work of a day or two for the most important formats. Take a look at the file

// PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.

// We don't need that feature at the moment and therefore will not implement it.

// If you write the code for exporting images I would be pleased to publish it in a future release

// of PDFsharp.

}

Hi, Thomas. I get the picture by this functuion. but i get the picture of black background.
Can you give me some help?


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 04, 2013 6:56 am 
Offline

Joined: Sat Feb 23, 2013 3:07 pm
Posts: 4
Hiļ¼ŒThomas:
I use the code bellow to create the png file:
Code:
            int width = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.Width);
            int height = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.Height);
            int bitsPerComponent = image.Elements.GetInteger(PdfSharp.Pdf.Advanced.PdfImage.Keys.BitsPerComponent);
            System.Drawing.Imaging.PixelFormat pixelFormat = new System.Drawing.Imaging.PixelFormat();

            PdfSharp.Pdf.PdfArray arr = image.Elements.GetArray(PdfSharp.Pdf.Advanced.PdfImage.Keys.ColorSpace);
            switch (bitsPerComponent)
            {
                case 1:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
                    break;
                case 8:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
                    break;
                case 24:
                    pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
                    break;
                default:
                    throw new Exception("Unknown pixel format " + bitsPerComponent);
            }

            PdfSharp.Pdf.Filters.FlateDecode fd = new PdfSharp.Pdf.Filters.FlateDecode();

            byte[] origineel_byte_boundary = image.Stream.UnfilteredValue;//fd.Decode(image.Stream.Value);
            byte[] resultaat_byte_boundary = null;
            int new_width = width;
            int allignment = 4;

            if (new_width % allignment != 0)
            //Image data in BMP files always starts at a DWORD boundary, in PDF it starts at a BYTE boundary.
            //Most images have a width that is a multiple of 4, so there is no problem with them.
            //You must copy the image data line by line and start each line at the DWORD boundary.
            {
                while (new_width % allignment != 0)
                    new_width++;
                byte[,] copy_dword_boundary = new byte[height, new_width];

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < new_width; x++)
                    {
                        if (x <= width && (x + (y * width) != origineel_byte_boundary.Length))
                            // while not at end of line, take orignale array
                            copy_dword_boundary[y, x] = origineel_byte_boundary[x + (y * width)];
                        else //fill new array with ending 0
                            copy_dword_boundary[y, x] = 0;
                    }
                }
                resultaat_byte_boundary = new byte[new_width * height];
                int counter = 0;
                for (int x = 0; x < copy_dword_boundary.GetLength(0); x++)
                {
                    for (int y = 0; y < copy_dword_boundary.GetLength(1); y++)
                    {   //put 2dim array back in 1dim array
                        resultaat_byte_boundary[counter] = copy_dword_boundary[x, y];
                        counter++;
                    }
                }
            }
            else
            {
                resultaat_byte_boundary = new byte[new_width * height];
                origineel_byte_boundary.CopyTo(resultaat_byte_boundary, 0);
            }
            Bitmap bmp = new Bitmap(new_width, height, pixelFormat);
            //string mystr = image.Elements.GetName("/ColorSpace");
            //bmp.MakeTransparent(Color.White);
            System.Drawing.Imaging.BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
            System.Runtime.InteropServices.Marshal.Copy(resultaat_byte_boundary, 0, bmd.Scan0, resultaat_byte_boundary.Length);
            bmp.UnlockBits(bmd);
            using (FileStream fs = new FileStream("C:\\" + String.Format("Image{0}.png", count), FileMode.Create, FileAccess.Write))
            {
                bmp.Save(fs, ImageFormat.Png);
                count++;
            }


but I only get the black-white picutre, how can I get the color picutre?


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 04, 2013 10:18 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
The color palette is a separate object in the PDF file. The entry for the picture contains a reference to the palette.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 12, 2016 8:13 am 
Offline

Joined: Tue Jul 12, 2016 8:09 am
Posts: 3
Hi Thomas

Could you please supply a working method with the color pallet as I failed to find ref on how to do this.
Currently the image comes out with weird colors instead of the actual correct PDF colors.

Thanks for your help
Shahar


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 12, 2016 10:18 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
Hi!
shahar wrote:
Could you please supply a working method with the color pallet as I failed to find ref on how to do this.
The references from Adobe can be found here:
http://www.adobe.com/devnet/pdf/pdf_ref ... chive.html

I cannot help with sample code.

_________________
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  [ 8 posts ] 

All times are UTC


Who is online

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