PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Embedding ICC profiles
PostPosted: Tue May 18, 2010 4:33 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
I've been reading the PDF 1.4 spec about how to embed an ICC profile as well as using the "Work on PDF Objects" sample to work my self through it, however I'm at a dead end. I can't seem to get any of the pdf objects I've created to actually end up in the pdf.

Here's the code I'm using:
Code:
Public Enum IccProfile As Integer
    Gray = 1
    RGB = 3
    CMYK = 4
End Enum

Public Sub AttachIccProfile(ByVal IccProfile As IccProfile, ByRef Document As PdfDocument)
    If Document Is Nothing Then Throw New ArgumentNullException("Document")

    Dim ProfilePath As String = ""

    Select Case IccProfile
        Case IccProfile.Gray
            ProfilePath = "grayscale.icc"
        Case IccProfile.RGB
            ProfilePath = "rgb.icc"
        Case IccProfile.CMYK
            ProfilePath = "cmyk.icc"
        Case Else
            Throw New ArgumentOutOfRangeException("IccProfile")
    End Select

    Dim IccArray As New PdfArray(Document)
    Dim IccDict As New PdfDictionary(Document)
    Dim FilterArray As New PdfArray(Document)

    FilterArray.Elements.Add(New PdfName("/FlateDecode"))

    Using EncodedIcc As New MemoryStream()
        Using ds As New DeflateStream(ipstream, CompressionMode.Compress, True)
            Using fs As New FileStream(ProfilePath, FileMode.Open, FileAccess.Read)
                Dim Buffer(CInt(fs.Length - 1)) As Byte, Size As Integer = 0
                Size = fs.Read(Buffer, 0, Buffer.Length)
                ds.Write(Buffer, 0, Size)
            End Using
        End Using

        IccDict.Elements("/N") = New PdfInteger(CInt(IccProfile))
        IccDict.Elements("/Filter") = FilterArray
        IccDict.Stream = IccDict.CreateStream(EncodedIcc.GetBuffer())
    End Using

    Document.Internals.AddObject(IccDict)

    IccArray.Elements.Add(New PdfName("/ICCBased"))
    IccArray.Elements.Add(PdfInternals.GetReference(IccDict))

    Document.Internals.AddObject(IccArray)
End Sub


Any help here would be greatly appreciated.

- B


Top
 Profile  
Reply with quote  
PostPosted: Tue May 18, 2010 4:38 pm 
Offline

Joined: Thu Feb 25, 2010 2:44 pm
Posts: 14
Just guessing, but perhaps the problem is that you're trying to deflate the stream yourself. Why not use the built-in (to PDFsharp) FlateDecode class? Despite the name, it's capable of encoding as well.


Top
 Profile  
Reply with quote  
PostPosted: Tue May 18, 2010 4:59 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
Yikes...I'm not sure how I missed those Filter classes. I'll give that a try.


Top
 Profile  
Reply with quote  
PostPosted: Tue May 18, 2010 5:27 pm 
Offline

Joined: Thu Feb 25, 2010 2:44 pm
Posts: 14
The only other thing I can think of is--and I'm still just speculating here--if nothing ever references the ICC object you added, perhaps PDFsharp realizes this and doesn't include it in the output. I've opened PDFs with unreachable objects before and when I import pages from them the unreachable objects don't come along. But then that's different from creating a PDF from scratch, so I dunno....


Top
 Profile  
Reply with quote  
PostPosted: Tue May 18, 2010 5:45 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
Yeah, I just tested by switching my encoding method to use the built-in FlateDecode as suggested...didn't make any difference in my output. My trouble is that I can't seem to create any root level objects in the document (which I'm starting to believe, as you mentioned, is because the object isn't referenced anywhere). In the PDF 1.4 spec, they have a sample that shows the structure for embedding a profile...other than that it's pretty vague (atleast to me). Here's their sample:
Code:
10 0 obj          % Color space
    [/ICCBased  15 0 R]
endobj

15 0 obj          % ICC profile stream
    <<  /N  3
        /Alternate  /DeviceRGB
        /Length  1605
        /Filter  /ASCIIHexDecode
    >>
stream
[stream data here...]
endstream
endobj


Perhaps I'm missing the part about referencing the profile through other objects? I'm not quite sure at this point.


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 03, 2017 1:50 pm 
Offline

Joined: Wed Feb 03, 2016 5:21 pm
Posts: 9
Is there any update on this topic? I also want to have the ICC profile from the images embedded in the PDF.
Using PDFsharp-gdi v1.50.4000-beta3b


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

All times are UTC


Who is online

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