PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Embedding ICC profiles
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1188
Page 1 of 1

Author:  Soldier-B [ Tue May 18, 2010 4:33 pm ]
Post subject:  Embedding ICC profiles

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

Author:  JeffJohnson [ Tue May 18, 2010 4:38 pm ]
Post subject:  Re: Embedding ICC profiles

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.

Author:  Soldier-B [ Tue May 18, 2010 4:59 pm ]
Post subject:  Re: Embedding ICC profiles

Yikes...I'm not sure how I missed those Filter classes. I'll give that a try.

Author:  JeffJohnson [ Tue May 18, 2010 5:27 pm ]
Post subject:  Re: Embedding ICC profiles

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....

Author:  Soldier-B [ Tue May 18, 2010 5:45 pm ]
Post subject:  Re: Embedding ICC profiles

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.

Author:  aleq_99 [ Fri Feb 03, 2017 1:50 pm ]
Post subject:  Re: Embedding ICC profiles

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

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/