I'm pretty sure that I'm just missing something simple with this and am going to smack my forehead when someone answers but the dent in my desk is getting pretty large at this point.
I'm tasked with building a organizational library for PdfSharpCore so that our developers can all sync up how we are using it and to allow for swapping it out if we find a better or more useful tool (loose coupling with PdfSharpCore)
We do a lot with stamps on PDFs so I have a structure that we will use to format dynamic stamps. We build the stamps at runtime so that they include the datetime that the document was stamped along with who processed the document
So currently I have the following class built to hold the stamp structure.
public class StampLayout {
// Class Properties
public Border border { get; set; }
public TextObject stampTitle { get; set; }
public TextObject supplementalText { get; set; }
public TextObject username { get; set; }
public DateTime timeStamp { get; set; }
public struct Border {
public int XCor { get; set; }
public int YCor { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Thickness { get; set; }
public int Color { get; set; }
}
public struct TextObject {
public string FontName { get; set; }
public int FontSize { get; set; }
public string FontStyle { get; set; }
public string TextValue { get; set; }
public string FontColor { get; set; }
public int XCor { get; set; }
public int YCor { get; set; }
public string FontFormat { get; set; }
}
}
The problem that I am having is holding the values for Color and FontColor. They look like they are enums so it should be a matter of just converting the color to an int and storing that. When I try using it, I get the error "Cannot convert int to PdfSharpCore.Drawing.Color"
Changing the variable type now strongly couples the developer's code to PdfSharpCore, which we want to avoid. My next step would to be to build a conversion method in the library and call it every time I needed to draw something. That seems a bit hackish to me and I would have to make sure color's were not added, removed or moved for each version of PdfSharpCore.
I just wondered if anyone could think of any simpler means of storing the value of the enum.
PDFSharp uses the type XColor
for colors. There is no enumeration named Color
in PDFSharp as far as I can see.
The type XColor
is not a simple enumeration but a struct.
The probably best way to store an XColor value is by storing a set of A,R,G,B values, especially if you maybe later on want to use a different library with a different color type. All will support converting to and from ARGB values.
The XColor
type has this static method to create a XColor value from a set of ARGB values:
public static XColor FromArgb(int alpha, int red, int green, int blue)
To convert a XColor value to ARGB values, just read its properties A, R, G and B.
The A property is a floating point value from 0 to 1. To get the integer representation of a value from 0 to 255 that is expected by the FromArgb
method, multiply it with 255.