Initial Commit

This commit is contained in:
2023-06-21 12:46:23 -04:00
commit c70248a520
1352 changed files with 336780 additions and 0 deletions

View File

@@ -0,0 +1,533 @@
using System;
using System.Collections;
using System.util;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.factories;
/*
* $Id: ElementFactory.cs,v 1.9 2008/05/13 11:25:14 psoares33 Exp $
*
*
* Copyright 2007 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.factories {
/**
* This class is able to create Element objects based on a list of properties.
*/
public class ElementFactory {
public static Chunk GetChunk(Properties attributes) {
Chunk chunk = new Chunk();
chunk.Font = FontFactory.GetFont(attributes);
String value;
value = attributes[ElementTags.ITEXT];
if (value != null) {
chunk.Append(value);
}
value = attributes[ElementTags.LOCALGOTO];
if (value != null) {
chunk.SetLocalGoto(value);
}
value = attributes[ElementTags.REMOTEGOTO];
if (value != null) {
String page = attributes[ElementTags.PAGE];
if (page != null) {
chunk.SetRemoteGoto(value, int.Parse(page));
}
else {
String destination = attributes[ElementTags.DESTINATION];
if (destination != null) {
chunk.SetRemoteGoto(value, destination);
}
}
}
value = attributes[ElementTags.LOCALDESTINATION];
if (value != null) {
chunk.SetLocalDestination(value);
}
value = attributes[ElementTags.SUBSUPSCRIPT];
if (value != null) {
chunk.SetTextRise(float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo));
}
value = attributes[Markup.CSS_KEY_VERTICALALIGN];
if (value != null && value.EndsWith("%")) {
float p = float.Parse(value.Substring(0, value.Length - 1), System.Globalization.NumberFormatInfo.InvariantInfo) / 100f;
chunk.SetTextRise(p * chunk.Font.Size);
}
value = attributes[ElementTags.GENERICTAG];
if (value != null) {
chunk.SetGenericTag(value);
}
value = attributes[ElementTags.BACKGROUNDCOLOR];
if (value != null) {
chunk.SetBackground(Markup.DecodeColor(value));
}
return chunk;
}
public static Phrase GetPhrase(Properties attributes) {
Phrase phrase = new Phrase();
phrase.Font = FontFactory.GetFont(attributes);
String value;
value = attributes[ElementTags.LEADING];
if (value != null) {
phrase.Leading = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[Markup.CSS_KEY_LINEHEIGHT];
if (value != null) {
phrase.Leading = Markup.ParseLength(value);
}
value = attributes[ElementTags.ITEXT];
if (value != null) {
Chunk chunk = new Chunk(value);
if ((value = attributes[ElementTags.GENERICTAG]) != null) {
chunk.SetGenericTag(value);
}
phrase.Add(chunk);
}
return phrase;
}
public static Anchor GetAnchor(Properties attributes) {
Anchor anchor = new Anchor(GetPhrase(attributes));
String value;
value = attributes[ElementTags.NAME];
if (value != null) {
anchor.Name = value;
}
value = (String)attributes.Remove(ElementTags.REFERENCE);
if (value != null) {
anchor.Reference = value;
}
return anchor;
}
public static Paragraph GetParagraph(Properties attributes) {
Paragraph paragraph = new Paragraph(GetPhrase(attributes));
String value;
value = attributes[ElementTags.ALIGN];
if (value != null) {
paragraph.SetAlignment(value);
}
value = attributes[ElementTags.INDENTATIONLEFT];
if (value != null) {
paragraph.IndentationLeft = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.INDENTATIONRIGHT];
if (value != null) {
paragraph.IndentationRight = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
return paragraph;
}
public static ListItem GetListItem(Properties attributes) {
ListItem item = new ListItem(GetParagraph(attributes));
return item;
}
public static List GetList(Properties attributes) {
List list = new List();
list.Numbered = Utilities.CheckTrueOrFalse(attributes, ElementTags.NUMBERED);
list.Lettered = Utilities.CheckTrueOrFalse(attributes, ElementTags.LETTERED);
list.Lowercase = Utilities.CheckTrueOrFalse(attributes, ElementTags.LOWERCASE);
list.Autoindent = Utilities.CheckTrueOrFalse(attributes, ElementTags.AUTO_INDENT_ITEMS);
list.Alignindent = Utilities.CheckTrueOrFalse(attributes, ElementTags.ALIGN_INDENTATION_ITEMS);
String value;
value = attributes[ElementTags.FIRST];
if (value != null) {
char character = value[0];
if (char.IsLetter(character) ) {
list.First = (int)character;
}
else {
list.First = int.Parse(value);
}
}
value = attributes[ElementTags.LISTSYMBOL];
if (value != null) {
list.ListSymbol = new Chunk(value, FontFactory.GetFont(attributes));
}
value = attributes[ElementTags.INDENTATIONLEFT];
if (value != null) {
list.IndentationLeft = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.INDENTATIONRIGHT];
if (value != null) {
list.IndentationRight = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.SYMBOLINDENT];
if (value != null) {
list.SymbolIndent = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
return list;
}
public static Cell GetCell(Properties attributes) {
Cell cell = new Cell();
String value;
cell.SetHorizontalAlignment(attributes[ElementTags.HORIZONTALALIGN]);
cell.SetVerticalAlignment(attributes[ElementTags.VERTICALALIGN]);
value = attributes[ElementTags.WIDTH];
if (value != null) {
cell.SetWidth(value);
}
value = attributes[ElementTags.COLSPAN];
if (value != null) {
cell.Colspan = int.Parse(value);
}
value = attributes[ElementTags.ROWSPAN];
if (value != null) {
cell.Rowspan = int.Parse(value);
}
value = attributes[ElementTags.LEADING];
if (value != null) {
cell.Leading = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
cell.Header = Utilities.CheckTrueOrFalse(attributes, ElementTags.HEADER);
if (Utilities.CheckTrueOrFalse(attributes, ElementTags.NOWRAP)) {
cell.MaxLines = 1;
}
SetRectangleProperties(cell, attributes);
return cell;
}
/**
* Creates an Table object based on a list of properties.
* @param attributes
* @return a Table
*/
public static Table GetTable(Properties attributes) {
String value;
Table table;
value = attributes[ElementTags.WIDTHS];
if (value != null) {
StringTokenizer widthTokens = new StringTokenizer(value, ";");
ArrayList values = new ArrayList();
while (widthTokens.HasMoreTokens()) {
values.Add(widthTokens.NextToken());
}
table = new Table(values.Count);
float[] widths = new float[table.Columns];
for (int i = 0; i < values.Count; i++) {
value = (String)values[i];
widths[i] = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
table.Widths = widths;
}
else {
value = attributes[ElementTags.COLUMNS];
try {
table = new Table(int.Parse(value));
}
catch {
table = new Table(1);
}
}
table.Border = Table.BOX;
table.BorderWidth = 1;
table.DefaultCell.Border = Table.BOX;
value = attributes[ElementTags.LASTHEADERROW];
if (value != null) {
table.LastHeaderRow = int.Parse(value);
}
value = attributes[ElementTags.ALIGN];
if (value != null) {
table.SetAlignment(value);
}
value = attributes[ElementTags.CELLSPACING];
if (value != null) {
table.Spacing = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.CELLPADDING];
if (value != null) {
table.Padding = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.OFFSET];
if (value != null) {
table.Offset = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.WIDTH];
if (value != null) {
if (value.EndsWith("%"))
table.Width = float.Parse(value.Substring(0, value.Length - 1), System.Globalization.NumberFormatInfo.InvariantInfo);
else {
table.Width = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
table.Locked = true;
}
}
table.TableFitsPage = Utilities.CheckTrueOrFalse(attributes, ElementTags.TABLEFITSPAGE);
table.CellsFitPage = Utilities.CheckTrueOrFalse(attributes, ElementTags.CELLSFITPAGE);
table.Convert2pdfptable = Utilities.CheckTrueOrFalse(attributes, ElementTags.CONVERT2PDFP);
SetRectangleProperties(table, attributes);
return table;
}
/**
* Sets some Rectangle properties (for a Cell, Table,...).
*/
private static void SetRectangleProperties(Rectangle rect, Properties attributes) {
String value;
value = attributes[ElementTags.BORDERWIDTH];
if (value != null) {
rect.BorderWidth = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
int border = 0;
if (Utilities.CheckTrueOrFalse(attributes, ElementTags.LEFT)) {
border |= Rectangle.LEFT_BORDER;
}
if (Utilities.CheckTrueOrFalse(attributes, ElementTags.RIGHT)) {
border |= Rectangle.RIGHT_BORDER;
}
if (Utilities.CheckTrueOrFalse(attributes, ElementTags.TOP)) {
border |= Rectangle.TOP_BORDER;
}
if (Utilities.CheckTrueOrFalse(attributes, ElementTags.BOTTOM)) {
border |= Rectangle.BOTTOM_BORDER;
}
rect.Border = border;
String r = attributes[ElementTags.RED];
String g = attributes[ElementTags.GREEN];
String b = attributes[ElementTags.BLUE];
if (r != null || g != null || b != null) {
int red = 0;
int green = 0;
int blue = 0;
if (r != null) red = int.Parse(r);
if (g != null) green = int.Parse(g);
if (b != null) blue = int.Parse(b);
rect.BorderColor = new Color(red, green, blue);
}
else {
rect.BorderColor = Markup.DecodeColor(attributes[ElementTags.BORDERCOLOR]);
}
r = (String)attributes.Remove(ElementTags.BGRED);
g = (String)attributes.Remove(ElementTags.BGGREEN);
b = (String)attributes.Remove(ElementTags.BGBLUE);
value = attributes[ElementTags.BACKGROUNDCOLOR];
if (r != null || g != null || b != null) {
int red = 0;
int green = 0;
int blue = 0;
if (r != null) red = int.Parse(r);
if (g != null) green = int.Parse(g);
if (b != null) blue = int.Parse(b);
rect.BackgroundColor = new Color(red, green, blue);
}
else if (value != null) {
rect.BackgroundColor = Markup.DecodeColor(value);
}
else {
value = attributes[ElementTags.GRAYFILL];
if (value != null) {
rect.GrayFill = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
}
}
public static ChapterAutoNumber GetChapter(Properties attributes) {
ChapterAutoNumber chapter = new ChapterAutoNumber("");
SetSectionParameters(chapter, attributes);
return chapter;
}
public static Section GetSection(Section parent, Properties attributes) {
Section section = parent.AddSection("");
SetSectionParameters(section, attributes);
return section;
}
private static void SetSectionParameters(Section section, Properties attributes) {
String value;
value = attributes[ElementTags.NUMBERDEPTH];
if (value != null) {
section.NumberDepth = int.Parse(value);
}
value = attributes[ElementTags.INDENT];
if (value != null) {
section.Indentation = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.INDENTATIONLEFT];
if (value != null) {
section.IndentationLeft = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.INDENTATIONRIGHT];
if (value != null) {
section.IndentationRight = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
}
/// <summary>
/// Returns an Image that has been constructed taking in account
/// the value of some attributes.
/// </summary>
/// <param name="attributes">Some attributes</param>
/// <returns>an Image</returns>
public static Image GetImage(Properties attributes) {
String value;
value = attributes[ElementTags.URL];
if (value == null)
throw new ArgumentException("The URL of the image is missing.");
Image image = Image.GetInstance(value);
value = attributes[ElementTags.ALIGN];
int align = 0;
if (value != null) {
if (Util.EqualsIgnoreCase(ElementTags.ALIGN_LEFT, value))
align |= Image.ALIGN_LEFT;
else if (Util.EqualsIgnoreCase(ElementTags.ALIGN_RIGHT, value))
align |= Image.ALIGN_RIGHT;
else if (Util.EqualsIgnoreCase(ElementTags.ALIGN_MIDDLE, value))
align |= Image.ALIGN_MIDDLE;
}
if (Util.EqualsIgnoreCase("true", attributes[ElementTags.UNDERLYING]))
align |= Image.UNDERLYING;
if (Util.EqualsIgnoreCase("true", attributes[ElementTags.TEXTWRAP]))
align |= Image.TEXTWRAP;
image.Alignment = align;
value = attributes[ElementTags.ALT];
if (value != null) {
image.Alt = value;
}
String x = attributes[ElementTags.ABSOLUTEX];
String y = attributes[ElementTags.ABSOLUTEY];
if ((x != null) && (y != null)) {
image.SetAbsolutePosition(float.Parse(x, System.Globalization.NumberFormatInfo.InvariantInfo),
float.Parse(y, System.Globalization.NumberFormatInfo.InvariantInfo));
}
value = attributes[ElementTags.PLAINWIDTH];
if (value != null) {
image.ScaleAbsoluteWidth(float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo));
}
value = attributes[ElementTags.PLAINHEIGHT];
if (value != null) {
image.ScaleAbsoluteHeight(float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo));
}
value = attributes[ElementTags.ROTATION];
if (value != null) {
image.Rotation = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
return image;
}
/**
* Creates an Annotation object based on a list of properties.
* @param attributes
* @return an Annotation
*/
public static Annotation GetAnnotation(Properties attributes) {
float llx = 0, lly = 0, urx = 0, ury = 0;
String value;
value = attributes[ElementTags.LLX];
if (value != null) {
llx = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.LLY];
if (value != null) {
lly = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.URX];
if (value != null) {
urx = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
value = attributes[ElementTags.URY];
if (value != null) {
ury = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
}
String title = attributes[ElementTags.TITLE];
String text = attributes[ElementTags.CONTENT];
if (title != null || text != null) {
return new Annotation(title, text, llx, lly, urx, ury);
}
value = attributes[ElementTags.URL];
if (value != null) {
return new Annotation(llx, lly, urx, ury, value);
}
value = attributes[ElementTags.NAMED];
if (value != null) {
return new Annotation(llx, lly, urx, ury, int.Parse(value));
}
String file = attributes[ElementTags.FILE];
String destination = attributes[ElementTags.DESTINATION];
String page = (String) attributes.Remove(ElementTags.PAGE);
if (file != null) {
if (destination != null) {
return new Annotation(llx, lly, urx, ury, file, destination);
}
if (page != null) {
return new Annotation(llx, lly, urx, ury, file, int.Parse(page));
}
}
if (title == null)
title = "";
if (text == null)
text = "";
return new Annotation(title, text, llx, lly, urx, ury);
}
}
}

View File

@@ -0,0 +1,122 @@
using System;
using iTextSharp.text;
/*
* $Id: GreekAlphabetFactory.cs,v 1.2 2008/05/13 11:25:14 psoares33 Exp $
*
*
* Copyright 2007 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.factories {
/**
* This class can produce String combinations representing a number built with
* Greek letters (from alpha to omega, then alpha alpha, alpha beta, alpha gamma).
* We are aware of the fact that the original Greek numbering is different;
* See http://www.cogsci.indiana.edu/farg/harry/lan/grknum.htm#ancient
* but this isn't implemented yet; the main reason being the fact that we
* need a font that has the obsolete Greek characters qoppa and sampi.
*/
public class GreekAlphabetFactory {
/**
* Changes an int into a lower case Greek letter combination.
* @param index the original number
* @return the letter combination
*/
public static String GetString(int index) {
return GetString(index, true);
}
/**
* Changes an int into a lower case Greek letter combination.
* @param index the original number
* @return the letter combination
*/
public static String GetLowerCaseString(int index) {
return GetString(index);
}
/**
* Changes an int into a upper case Greek letter combination.
* @param index the original number
* @return the letter combination
*/
public static String GetUpperCaseString(int index) {
return GetString(index).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
}
/**
* Changes an int into a Greek letter combination.
* @param index the original number
* @return the letter combination
*/
public static String GetString(int index, bool lowercase) {
if (index < 1) return "";
index--;
int bytes = 1;
int start = 0;
int symbols = 24;
while (index >= symbols + start) {
bytes++;
start += symbols;
symbols *= 24;
}
int c = index - start;
char[] value = new char[bytes];
while (bytes > 0) {
bytes--;
value[bytes] = (char)(c % 24);
if (value[bytes] > 16) value[bytes]++;
value[bytes] += (char)(lowercase ? 945 : 913);
value[bytes] = SpecialSymbol.GetCorrespondingSymbol(value[bytes]);
c /= 24;
}
return new String(value);
}
}
}

View File

@@ -0,0 +1,122 @@
using System;
/*
* $Id: RomanAlphabetFactory.cs,v 1.2 2008/05/13 11:25:14 psoares33 Exp $
*
*
* Copyright 2007 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.factories {
/**
* This class can produce String combinations representing a number.
* "a" to "z" represent 1 to 26, "AA" represents 27, "AB" represents 28,
* and so on; "ZZ" is followed by "AAA".
*/
public class RomanAlphabetFactory {
/**
* Translates a positive integer (not equal to zero)
* into a String using the letters 'a' to 'z';
* 1 = a, 2 = b, ..., 26 = z, 27 = aa, 28 = ab,...
*/
public static String GetString(int index) {
if (index < 1) throw new FormatException("You can't translate a negative number into an alphabetical value.");
index--;
int bytes = 1;
int start = 0;
int symbols = 26;
while (index >= symbols + start) {
bytes++;
start += symbols;
symbols *= 26;
}
int c = index - start;
char[] value = new char[bytes];
while (bytes > 0) {
value[--bytes] = (char)( 'a' + (c % 26));
c /= 26;
}
return new String(value);
}
/**
* Translates a positive integer (not equal to zero)
* into a String using the letters 'a' to 'z';
* 1 = a, 2 = b, ..., 26 = z, 27 = aa, 28 = ab,...
*/
public static String GetLowerCaseString(int index) {
return GetString(index);
}
/**
* Translates a positive integer (not equal to zero)
* into a String using the letters 'A' to 'Z';
* 1 = A, 2 = B, ..., 26 = Z, 27 = AA, 28 = AB,...
*/
public static String GetUpperCaseString(int index) {
return GetString(index).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
}
/**
* Translates a positive integer (not equal to zero)
* into a String using the letters 'a' to 'z'
* (a = 1, b = 2, ..., z = 26, aa = 27, ab = 28,...).
*/
public static String GetString(int index, bool lowercase) {
if (lowercase) {
return GetLowerCaseString(index);
}
else {
return GetUpperCaseString(index);
}
}
}
}

View File

@@ -0,0 +1,180 @@
using System;
using System.Text;
/*
* $Id: RomanNumberFactory.cs,v 1.2 2008/05/13 11:25:14 psoares33 Exp $
*
*
* Copyright 2007 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.factories {
/**
* This class can produce String combinations representing a roman number.
*/
public class RomanNumberFactory {
/**
* Helper class for Roman Digits
*/
internal class RomanDigit {
/** part of a roman number */
public char digit;
/** value of the roman digit */
public int value;
/** can the digit be used as a prefix */
public bool pre;
/**
* Constructs a roman digit
* @param digit the roman digit
* @param value the value
* @param pre can it be used as a prefix
*/
internal RomanDigit(char digit, int value, bool pre) {
this.digit = digit;
this.value = value;
this.pre = pre;
}
}
/**
* Array with Roman digits.
*/
private static RomanDigit[] roman = {
new RomanDigit('m', 1000, false),
new RomanDigit('d', 500, false),
new RomanDigit('c', 100, true),
new RomanDigit('l', 50, false),
new RomanDigit('x', 10, true),
new RomanDigit('v', 5, false),
new RomanDigit('i', 1, true)
};
/**
* Changes an int into a lower case roman number.
* @param index the original number
* @return the roman number (lower case)
*/
public static String GetString(int index) {
StringBuilder buf = new StringBuilder();
// lower than 0 ? Add minus
if (index < 0) {
buf.Append('-');
index = -index;
}
// greater than 3000
if (index > 3000) {
buf.Append('|');
buf.Append(GetString(index / 1000));
buf.Append('|');
// remainder
index = index - (index / 1000) * 1000;
}
// number between 1 and 3000
int pos = 0;
while (true) {
// loop over the array with values for m-d-c-l-x-v-i
RomanDigit dig = roman[pos];
// adding as many digits as we can
while (index >= dig.value) {
buf.Append(dig.digit);
index -= dig.value;
}
// we have the complete number
if (index <= 0) {
break;
}
// look for the next digit that can be used in a special way
int j = pos;
while (!roman[++j].pre);
// does the special notation apply?
if (index + roman[j].value >= dig.value) {
buf.Append(roman[j].digit).Append(dig.digit);
index -= dig.value - roman[j].value;
}
pos++;
}
return buf.ToString();
}
/**
* Changes an int into a lower case roman number.
* @param index the original number
* @return the roman number (lower case)
*/
public static String GetLowerCaseString(int index) {
return GetString(index);
}
/**
* Changes an int into an upper case roman number.
* @param index the original number
* @return the roman number (lower case)
*/
public static String GetUpperCaseString(int index) {
return GetString(index).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
}
/**
* Changes an int into a roman number.
* @param index the original number
* @return the roman number (lower case)
*/
public static String GetString(int index, bool lowercase) {
if (lowercase) {
return GetLowerCaseString(index);
}
else {
return GetUpperCaseString(index);
}
}
}
}