public SetTextRise ( float rise ) : |
||
rise | float | the displacement in points |
Résultat |
public Chunk CreateChunk(String text, ChainedProperties props) { Font font = GetFont(props); float size = font.Size; size /= 2; Chunk ck = new Chunk(text, font); if (props.HasProperty("sub")) ck.SetTextRise(-size); else if (props.HasProperty("sup")) ck.SetTextRise(size); ck.SetHyphenation(GetHyphenation(props)); return ck; }
// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter.GetInstance(document, stream).InitialLeading = 16; // step 3 document.Open(); // add the ID in another font Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE); // step 4 using (var c = AdoDB.Provider.CreateConnection()) { c.ConnectionString = AdoDB.CS; using (DbCommand cmd = c.CreateCommand()) { cmd.CommandText = "SELECT country,id FROM film_country ORDER BY country"; c.Open(); using (var r = cmd.ExecuteReader()) { while (r.Read()) { var country = r.GetString(0); var ID = r.GetString(1); document.Add(new Chunk(country)); document.Add(new Chunk(" ")); Chunk id = new Chunk(ID, font); // with a background color id.SetBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f); // and a text rise id.SetTextRise(6); document.Add(id); document.Add(Chunk.NEWLINE); } } } } } }
/** * Creates an iText Chunk * @param content the content of the Chunk * @param chain the hierarchy chain * @return a Chunk */ public Chunk CreateChunk(String content, ChainedProperties chain) { Font font = GetFont(chain); Chunk ck = new Chunk(content, font); if (chain.HasProperty(HtmlTags.SUB)) ck.SetTextRise(-font.Size / 2); else if (chain.HasProperty(HtmlTags.SUP)) ck.SetTextRise(font.Size / 2); ck.SetHyphenation(GetHyphenation(chain)); return ck; }
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; }
private byte[] createPdfWithSupescript(String regularText, String superscriptText) { MemoryStream byteStream = new MemoryStream(); Document document = new Document(); PdfWriter.GetInstance(document, byteStream); document.Open(); document.Add(new Chunk(regularText)); Chunk c2 = new Chunk(superscriptText); c2.SetTextRise(7.0f); document.Add(c2); document.Close(); byte[] pdfBytes = byteStream.ToArray(); return pdfBytes; }
static void MainExample() { var doc = new Document (); PdfWriter.GetInstance(doc, new FileStream(@"Document.pdf", FileMode.Create)); doc.Open(); //Normal text string text = "Normal Text. "; Paragraph par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 15; i++) { par.Add (text); } doc.Add (par); //Bold text text = "Bold Text. "; BaseFont baseFont = BaseFont.CreateFont (BaseFont.HELVETICA_BOLD, BaseFont.CP1257, false); iTextSharp.text.Font font = new iTextSharp.text.Font (baseFont, textFontSize); Chunk chunk = new Chunk (text, font); par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 15; i++) { par.Add (chunk); } doc.Add (par); //Italic Text text = "Italic Text. "; baseFont = BaseFont.CreateFont (BaseFont.TIMES_ITALIC, BaseFont.CP1257, false); font = new iTextSharp.text.Font (baseFont, textFontSize); chunk = new Chunk (text, font); par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 15; i++) { par.Add (chunk); } doc.Add (par); //Underline text text = "Underline Text. "; baseFont = BaseFont.CreateFont (BaseFont.HELVETICA, BaseFont.CP1257, false); font = new iTextSharp.text.Font (baseFont, textFontSize, iTextSharp.text.Font.UNDERLINE); chunk = new Chunk (text, font); par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 15; i++) { par.Add (chunk); } doc.Add (par); //Strikethrough text text = "Strikethrough Text. "; baseFont = BaseFont.CreateFont (BaseFont.HELVETICA, BaseFont.CP1257, false); font = new iTextSharp.text.Font (baseFont, textFontSize, iTextSharp.text.Font.STRIKETHRU); chunk = new Chunk (text, font); par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 15; i++) { par.Add (chunk); } doc.Add (par); //Hyperlink text text = "Hyperlink Text. "; baseFont = BaseFont.CreateFont (BaseFont.HELVETICA, BaseFont.CP1257, false); //font = new iTextSharp.text.Font (baseFont, textFontSize, iTextSharp.text.Font.UNDERLINE); font = FontFactory.GetFont("Arial", textFontSize, iTextSharp.text.Font.UNDERLINE, new iTextSharp.text.BaseColor(0, 0, 255)); Anchor link = new Anchor (text, font); link.Reference = "http://google.com"; par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 15; i++) { par.Add (link); } doc.Add (par); //Russian Text //Warning text = "Русский Текст. "; font = FontFactory.GetFont(GetFontByName("Arial"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); chunk = new Chunk (text, font); par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 15; i++) { par.Add (chunk); } doc.Add (par); //Bold & Italic & Underline & Hyperlink Text text = "Bold & Italic & Underline & Hyperlink Text. "; font = FontFactory.GetFont( GetFontByName("Times New Roman"), textFontSize, iTextSharp.text.Font.UNDERLINE | iTextSharp.text.Font.BOLD | iTextSharp.text.Font.ITALIC, new iTextSharp.text.BaseColor(0, 0, 255) ); link = new Anchor (text, font); link.Reference = "http://google.com"; par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 7; i++) { par.Add (link); } doc.Add (par); //Before (After) line break text="Before line break. "; par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; for (int i = 0; i < 13; i++) { par.Add (text); } par.Add (Chunk.NEWLINE); par.Add ("After line break."); doc.Add (par); //Span text text="Span text. "; par = new Paragraph (); par.FirstLineIndent = firstLineIndent; par.SpacingAfter = spacingAfter; Font font1 = FontFactory.GetFont (GetFontByName ("Arial")); Font font2 = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.BOLD); Font font3 = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.ITALIC); Font font4 = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.UNDERLINE); Font font5 = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.UNDERLINE, new BaseColor(0, 0, 255)); link = new Anchor (text, font5); link.Reference = "http://google.com"; par.Add (new Chunk (text, font1)); par.Add (new Chunk (text, font2)); par.Add (new Chunk (text, font3)); par.Add (new Chunk (text, font4)); par.Add (" "); par.Add (link); doc.Add (par); //Image par = new Paragraph (); par.SpacingAfter = spacingAfter; Image jpg1 = Image.GetInstance ("images.jpg"); Image jpg2 = Image.GetInstance ("images.jpg"); Image jpg3 = Image.GetInstance ("images.jpg"); Image jpg4 = Image.GetInstance ("images.jpg"); jpg1.ScalePercent (scalePercent); jpg1.RotationDegrees = 0; jpg2.ScalePercent (scalePercent); jpg2.RotationDegrees = -90; jpg3.ScalePercent (scalePercent); jpg3.RotationDegrees = -180; jpg4.ScalePercent (scalePercent); jpg4.RotationDegrees = -270; par.Add (new Chunk (jpg1, 0, 0, true)); par.Add (new Chunk (jpg2, 0, 0, true)); par.Add (new Chunk (jpg3, 0, 0, true)); par.Add (new Chunk (jpg4, 0, 0, true)); doc.Add (par); //Page break doc.Add (new Paragraph("Before page break.")); doc.Add (Chunk.NEXTPAGE); doc.Add (new Paragraph("After page break.")); //List with 'None' marker style: par = new Paragraph(); par.SpacingBefore = spacingAfter; par.Add ("List with 'None' marker style:"); doc.Add(par); List list = new List (List.UNORDERED, symbolIndent); list.SetListSymbol (""); list.IndentationLeft = indentationLeft; list.Add ("Item1"); list.Add ("Item2"); list.Add ("Item3"); doc.Add (list); //List with 'Disc' marker style: par = new Paragraph(); par.SpacingBefore = spacingAfter; par.Add ("List with 'Disc' marker style:"); doc.Add(par); list = new List (List.UNORDERED, symbolIndent); list.SetListSymbol (symbolDisk); list.IndentationLeft = indentationLeft; list.Add ("Item1"); list.Add ("Item2"); list.Add ("Item3"); doc.Add (list); //List with 'Box' marker style: //Not working... par = new Paragraph(); par.SpacingBefore = spacingAfter; par.Add ("List with 'Box' marker style:"); doc.Add(par); list = new List (List.UNORDERED, symbolIndent); list.SetListSymbol (symbolBox); list.IndentationLeft = indentationLeft; list.Add ("Item1"); list.Add ("Item2"); list.Add ("Item3"); doc.Add (list); //List with 'LowerRoman' marker style: //Warning par = new Paragraph(); par.SpacingBefore = spacingAfter; par.Add ("List with 'LowerRoman' marker style:"); doc.Add(par); RomanList romanList = new RomanList (true, symbolIndent); //true == Roman Lower romanList.IndentationLeft = indentationLeft; romanList.Add ("Item1"); romanList.Add ("Item2"); romanList.Add ("Item3"); doc.Add (romanList); //List with 'UpperRoman' marker style: //Warning par = new Paragraph(); par.SpacingBefore = spacingAfter; par.Add ("List with 'UpperRoman' marker style:"); doc.Add(par); romanList = new RomanList (false, symbolIndent); //true == Roman Lower romanList.IndentationLeft = indentationLeft; romanList.Add ("Item1"); romanList.Add ("Item2"); romanList.Add ("Item3"); romanList.Add ("Item4"); romanList.Add ("Item5"); romanList.Add ("Item6"); doc.Add (romanList); //Table //http://www.mikesdotnetting.com/Article/86/itextsharp-introducing-tables par = new Paragraph(); par.SpacingBefore = spacingAfter; par.Add ("Table:"); doc.Add(par); PdfPTable table = new PdfPTable (5); //5 Columns table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin; table.LockedWidth = true; float columt1Width = 50f; float columt2Width = 80f; float delta = (table.TotalWidth - columt1Width - columt2Width) / 3; float[] widths = { columt1Width, columt2Width, delta, delta, delta }; table.SetWidths (widths); table.SpacingBefore = spacingAfter; PdfPCell cell = new PdfPCell(new Phrase("Colspan = Rowspan = 2")); cell.Colspan = 2; cell.Rowspan = 2; table.AddCell (cell); cell = new PdfPCell (new Phrase("Rowspan = 4")); cell.Rowspan = 4; table.AddCell (cell); cell = new PdfPCell (new Phrase("Colspan = 2")); cell.Colspan = 2; table.AddCell (cell); for (int i = 0; i < 20; i++) table.AddCell ("Text " + (i + 1).ToString ()); doc.Add (table); //Before (After) line //Warning par = new Paragraph("Before line"); par.SpacingBefore = spacingAfter; doc.Add (par); cell = new PdfPCell(); cell.BorderWidth = 0f; cell.BorderWidthBottom = 0.5f; table = new PdfPTable (1); table.AddCell (cell); table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin; table.LockedWidth = true; table.SpacingBefore = spacingAfter; doc.Add (table); par = new Paragraph("After line"); doc.Add (par); //Fonts (Arial) par = new Paragraph(); par.SpacingBefore = spacingAfter; text="Arial 10pt. "; font = FontFactory.GetFont (GetFontByName ("Arial"), 10); par.Add (new Chunk (text, font)); text="Arial 15pt. "; font = FontFactory.GetFont (GetFontByName ("Arial"), 15); par.Add (new Chunk (text, font)); text="Arial 20pt. "; font = FontFactory.GetFont (GetFontByName ("Arial"), 20); par.Add (new Chunk (text, font)); doc.Add (par); //Fonts (Tahoma) par = new Paragraph(); par.SpacingBefore = spacingAfter; text="Tahoma 10pt. "; font = FontFactory.GetFont (GetFontByName ("Tahoma"), 10); par.Add (new Chunk (text, font)); text="Tahoma 15pt. "; font = FontFactory.GetFont (GetFontByName ("Tahoma"), 15); par.Add (new Chunk (text, font)); text="Tahoma 20pt. "; font = FontFactory.GetFont (GetFontByName ("Tahoma"), 20); par.Add (new Chunk (text, font)); doc.Add (par); //Fonts (Courier New) par = new Paragraph(); par.SpacingBefore = spacingAfter; text="Courier New 10pt. "; font = FontFactory.GetFont (GetFontByName ("Courier New"), 10); par.Add (new Chunk (text, font)); text="Courier New 15pt. "; font = FontFactory.GetFont (GetFontByName ("Courier New"), 15); par.Add (new Chunk (text, font)); text="Courier New 20pt. "; font = FontFactory.GetFont (FontFactory.COURIER, 20); //GetFontByName ("Courier New") == FontFactory.COURIER par.Add (new Chunk (text, font)); doc.Add (par); //Normal, Italic, Oblique //Not Finished par = new Paragraph(); par.SpacingBefore = spacingAfter; par.SpacingAfter = spacingAfter; text = "Normal. "; font = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.NORMAL); par.Add (new Chunk (text, font)); text = "Italic. "; font = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, Font.ITALIC); par.Add (new Chunk (text, font)); text = "Oblique. (Not Working)"; font = FontFactory.GetFont (GetFontByName ("Arial"), textFontSize); par.Add (new Chunk (text, font)); doc.Add (par); //Normal. Subscript. Superscript. //H 2 O doc.Add(new Paragraph("Normal. Subscript. Superscript.")); par = new Paragraph (); par.SpacingBefore = spacingAfter; chunk = new Chunk ("H", FontFactory.GetFont (GetFontByName ("Arial"))); par.Add (chunk); chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial"))); chunk.SetTextRise (-5f); par.Add (chunk); chunk = new Chunk ("O", FontFactory.GetFont (GetFontByName ("Arial"))); par.Add (chunk); doc.Add (par); //C 2 H 5 OH par = new Paragraph (); par.SpacingBefore = spacingAfter; chunk = new Chunk ("C", FontFactory.GetFont (GetFontByName ("Arial"))); par.Add (chunk); chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial"))); chunk.SetTextRise (-5f); par.Add (chunk); chunk = new Chunk ("H", FontFactory.GetFont (GetFontByName ("Arial"))); par.Add (chunk); chunk = new Chunk ("5", FontFactory.GetFont (GetFontByName ("Arial"))); chunk.SetTextRise (-5f); par.Add (chunk); chunk = new Chunk ("OH", FontFactory.GetFont (GetFontByName ("Arial"))); par.Add (chunk); doc.Add (par); //a^2+b^2=c^2 par = new Paragraph (); par.SpacingBefore = spacingAfter; chunk = new Chunk ("a", FontFactory.GetFont (GetFontByName ("Arial"))); par.Add (chunk); chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial"))); chunk.SetTextRise (5f); par.Add (chunk); chunk = new Chunk (" + b", FontFactory.GetFont (GetFontByName ("Arial"))); par.Add (chunk); chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial"))); chunk.SetTextRise (5f); par.Add (chunk); chunk = new Chunk (" = c", FontFactory.GetFont (GetFontByName ("Arial"))); par.Add (chunk); chunk = new Chunk ("2", FontFactory.GetFont (GetFontByName ("Arial"))); chunk.SetTextRise (5f); par.Add (chunk); doc.Add (par); //Foreground and Background (text color and cell backgroudcolor) (table) //Warning table=new PdfPTable(3); chunk = new Chunk ("Green foreground", FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, new BaseColor (0, 200, 0))); cell = new PdfPCell (new Phrase (chunk)); cell.BorderWidth = 0f; table.AddCell (cell); cell = new PdfPCell (new Phrase ("Red Background")); cell.BackgroundColor = new BaseColor (255, 0, 0); cell.BorderWidth = 0f; table.AddCell (cell); chunk = new Chunk ("Green foreground & Red background", FontFactory.GetFont (GetFontByName ("Arial"), textFontSize, new BaseColor (0, 200, 0))); cell = new PdfPCell (new Phrase (chunk)); cell.BackgroundColor = new BaseColor (255, 0, 0); cell.BorderWidth = 0f; table.AddCell (cell); table.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin; table.LockedWidth = true; table.SpacingBefore = spacingAfter; doc.Add (table); //Left text par = new Paragraph(); par.SpacingBefore = spacingAfter; par.FirstLineIndent = firstLineIndent; for (int i = 0; i < 15; i++) par.Add ("Left text. "); par.Alignment = Element.ALIGN_LEFT; doc.Add (par); //Rigth text par = new Paragraph(); par.SpacingBefore = spacingAfter; par.FirstLineIndent = firstLineIndent; for (int i = 0; i < 15; i++) par.Add ("Rigth text. "); par.Alignment = Element.ALIGN_RIGHT; doc.Add (par); //Center text par = new Paragraph(); par.SpacingBefore = spacingAfter; for (int i = 0; i < 15; i++) par.Add ("Center text. "); par.Alignment = Element.ALIGN_CENTER; doc.Add (par); //Justify text par = new Paragraph(); par.SpacingBefore = spacingAfter; par.FirstLineIndent = firstLineIndent; for (int i = 0; i < 15; i++) par.Add ("Justify text. "); par.Alignment = Element.ALIGN_JUSTIFIED_ALL; doc.Add (par); //Boxes //Warning PdfPTable box1 = new PdfPTable (1); PdfPTable box2 = new PdfPTable (1); PdfPCell addCellInBox2 = new PdfPCell (new Phrase ("Section(table) & !Margin & Padding & Border & Background")); addCellInBox2.Padding = 20f; addCellInBox2.BackgroundColor = new BaseColor (255, 255, 0); addCellInBox2.BorderWidth = 4f; addCellInBox2.BorderColor = new BaseColor (0, 0, 255); box2.AddCell (addCellInBox2); PdfPCell addCellinBox1 = new PdfPCell (box2); addCellinBox1.Padding = 20f; addCellinBox1.BackgroundColor = new BaseColor (0, 200, 0); addCellinBox1.BorderWidth = 4f; addCellinBox1.BorderColor = new BaseColor (255, 0, 0); box1.AddCell (addCellinBox1); box1.SpacingBefore = spacingAfter; box1.TotalWidth = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin; box1.LockedWidth = true; doc.Add (box1); doc.Close (); System.Diagnostics.Process.Start ("Document.pdf"); }
private static void ReportBuilder(int cropYear, string shid, string fromShid, string toShid, string filePath, string logoUrl) { const string METHOD_NAME = "ReportBuilder"; Document document = null; PdfWriter writer = null; iTextSharp.text.Image imgLogo = null; NoticeOfPassthroughEvent pgEvent = null; string rptTitle = "NOTICE OF PASSTHROUGH OF DOMESTIC PRODUCTION ACTIVITIES\n" +"DEDUCTION FROM THE WESTERN SUGAR COOPERATIVE TO PATRONS "; try { List<ListNoticeOfPassthrough> stateList = WSCReportsExec.PassthroughGetBySHID(cropYear, shid, fromShid, toShid); if (stateList.Count > 0) { using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { foreach (ListNoticeOfPassthrough state in stateList) { if (document == null) { // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!! // *** US LETTER: 612 X 792 *** //document = new Document(iTextSharp.text.PageSize.LETTER, 36, 36, 54, 72); document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin, PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin, PortraitPageSize.PgBottomMargin); // we create a writer that listens to the document // and directs a PDF-stream to a file writer = PdfWriter.GetInstance(document, fs); ; imgLogo = PdfReports.GetImage(logoUrl, 127, 50, iTextSharp.text.Element.ALIGN_CENTER); // Attach my override event handler(s) pgEvent = new NoticeOfPassthroughEvent(); pgEvent.FillEvent(state, 0, rptTitle, imgLogo); writer.PageEvent = pgEvent; // Open the document document.Open(); } else { pgEvent.FillEvent(state, 0, rptTitle, imgLogo); document.NewPage(); } // ====================================================== // Fill in body of letter // ====================================================== PdfPTable table = PdfReports.CreateTable(_bodyLayout, 0); Paragraph para = new Paragraph("The purpose of this notice is to inform you of your allocable share of the domestic production activities " + "deduction (also known as the Section 199 deduction) which The Western Sugar Cooperative (“WSC”) is passing " + "through to patrons. The deduction will be reported on your " + state.TaxYear.ToString() + " Form 1099-PATR.", _normalFont); PdfReports.AddText2Table(table, para, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); para = new Paragraph("Your share of the passthrough domestic production activities deduction amount is detailed below:", _normalFont); PdfReports.AddText2Table(table, para, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, "Domestic Production Activities Deduction Allocated to Patron", _labelFont); PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _normalFont); para = new Paragraph("SHID: " + state.SHID + "\n" + "Member Name: " + state.MemberName + "\n" + "Patron’s share of WSC Domestic Production Activities Deduction: $" + state.PatronShareOfDeduction.ToString("#,###.00"), _normalFont); PdfReports.AddText2Table(table, para, 2); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); PdfReports.AddText2Table(table, "Explanation", _labelFont, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); para = new Paragraph("Created by Congress, the domestic production activities deduction has been in effect since 2005. It is designed " + "to encourage businesses like WSC to engage in production and/or manufacturing activities in the United States " + "and to employ workers in those activities.", _normalFont); PdfReports.AddText2Table(table, para, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); para = new Paragraph("Section 199(d)(3) of the Internal Revenue Code contains special rules governing how cooperatives and their " + "patrons must compute the deduction. The Code permits a cooperative to elect to pass through to patrons all or a " + "portion of its domestic production activities deduction. Cooperatives typically pass through to patrons whatever " + "part of the deduction they can not themselves use.", _normalFont); PdfReports.AddText2Table(table, para, _bodyLayout.Length); string pctToApply = "All"; if (state.PercentageToApply != 100) { pctToApply = state.PercentageToApply.ToString("N3"); if (pctToApply.EndsWith("000")) { pctToApply = state.PercentageToApply.ToString("N0"); } pctToApply += "%"; } PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); para = new Paragraph("WSC is passing through to its patrons " + pctToApply + " of the domestic production activities deduction it earned " + "for the fiscal year ending " + state.FiscalYearEndDate.ToShortDateString() + ". The passthrough is being allocated to patrons based on sugar " + "beet tonnage for Crop Year " + state.CropYear.ToString() + " (fiscal year " + state.FiscalYear.ToString() + "). This notice identifies your share of the " + "passthrough. ", _normalFont); PdfReports.AddText2Table(table, para, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); para = new Paragraph("A domestic production activities deduction passed through to a patron can generally be used to reduce the " + "patron’s taxable income. In order to claim this passed-through deduction, a patron must include a Form 8903 " + "(Domestic Production Activities Deduction) with the patron’s income tax return and report the passthrough " + "deduction on the appropriate line of that form.", _normalFont); PdfReports.AddText2Table(table, para, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, "We strongly advise you consult with your tax advisor to determine how to take\n" + "advantage of this potential tax benefit.", _labelFont, "center"); PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); PdfReports.AddText2Table(table, " ", _subFont, _bodyLayout.Length); para = new Paragraph("______________________________\n\n", _subFont); Chunk ck = new Chunk("1", _subFont); ck.SetTextRise(5); para.Add(ck); Chunk chunk = new Chunk(" This notice is issued pursuant to Section 199(d)(3) of the Internal Revenue Code.", _subFont); para.Add(chunk); PdfReports.AddText2Table(table, para, _bodyLayout.Length); PdfReports.AddTableNoSplit(document, pgEvent, table); } // ====================================================== // Close document // ====================================================== if (document != null) { pgEvent.IsDocumentClosing = true; document.Close(); document = null; } if (writer == null) { // Warn that we have no data. WSCIEMP.Common.CWarning warn = new WSCIEMP.Common.CWarning("No records matched your report criteria."); throw (warn); } } } else { // No recs qualified for query. WSCIEMP.Common.CWarning warn = new WSCIEMP.Common.CWarning("No records matched your query."); throw(warn); } } catch (System.Exception ex) { string errMsg = "cropYear: " + cropYear.ToString(); WSCIEMP.Common.CException wscEx = new WSCIEMP.Common.CException(MOD_NAME + "." + METHOD_NAME + ": " + errMsg, ex); throw (wscEx); } finally { if (document != null) { pgEvent.IsDocumentClosing = true; document.Close(); } if (writer != null) { writer.Close(); } } }
/** * * @param c the Chunk to apply CSS to. * @param t the tag containing the chunk data * @return the styled chunk */ virtual public Chunk Apply(Chunk c, Tag t) { Font f = ApplyFontStyles(t); float size = f.Size; String value = null; IDictionary<String, String> rules = t.CSS; foreach (KeyValuePair<String, String> entry in rules) { String key = entry.Key; value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.OBLIQUE, value)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { String letterSpacing = entry.Value; float letterSpacingValue = 0f; if (utils.IsRelativeValue(value)) { letterSpacingValue = utils.ParseRelativeValue(letterSpacing, f.Size); } else if (utils.IsMetricValue(value)) { letterSpacingValue = utils.ParsePxInCmMmPcToPt(letterSpacing); } c.SetCharacterSpacing(letterSpacingValue); } else if (Util.EqualsIgnoreCase(CSS.Property.XFA_FONT_HORIZONTAL_SCALE, key)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling( float.Parse(value.Replace("%", ""))/100); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.TryGetValue(CSS.Property.VERTICAL_ALIGN, out value)) { if (Util.EqualsIgnoreCase(CSS.Value.SUPER, value) || Util.EqualsIgnoreCase(CSS.Value.TOP, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_TOP, value)) { c.SetTextRise((float) (size/2 + 0.5)); } else if (Util.EqualsIgnoreCase(CSS.Value.SUB, value) || Util.EqualsIgnoreCase(CSS.Value.BOTTOM, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_BOTTOM, value)) { c.SetTextRise(-size/2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; if (rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale)) { if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", ""))/100; c.SetHorizontalScaling(100/float.Parse(xfaVertScale.Replace("%", ""))); } } if (rules.TryGetValue(CSS.Property.TEXT_DECORATION, out value)) { String[] splitValues = new Regex(@"\s+").Split(value); foreach (String curValue in splitValues) { if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, curValue)) { c.SetUnderline(0.75f, -size/8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, curValue)) { c.SetUnderline(0.75f, size/4f); } } } if (rules.TryGetValue(CSS.Property.BACKGROUND_COLOR, out value)) { c.SetBackground(HtmlUtilities.DecodeColor(value)); } f.Size = size; c.Font = f; float? leading = null; value = null; if (rules.TryGetValue(CSS.Property.LINE_HEIGHT, out value)) { if (utils.IsNumericValue(value)) { leading = float.Parse(value) * c.Font.Size; } else if (utils.IsRelativeValue(value)) { leading = utils.ParseRelativeValue(value, c.Font.Size); } else if (utils.IsMetricValue(value)) { leading = utils.ParsePxInCmMmPcToPt(value); } } if (leading != null) { c.setLineHeight((float)leading); } return c; }
/** * * @param c the Chunk to apply CSS to. * @param t the tag containing the chunk data * @return the styled chunk */ public Chunk Apply(Chunk c, Tag t) { Font f = ApplyFontStyles(t); float size = f.Size; String value = null; IDictionary<String, String> rules = t.CSS; foreach (KeyValuePair<String, String> entry in rules) { String key = entry.Key; value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.OBLIQUE, value)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { c.SetCharacterSpacing(utils.ParsePxInCmMmPcToPt(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.XFA_FONT_HORIZONTAL_SCALE, key)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling( float.Parse(value.Replace("%", ""))/100); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.TryGetValue(CSS.Property.VERTICAL_ALIGN, out value)) { if (Util.EqualsIgnoreCase(CSS.Value.SUPER, value) || Util.EqualsIgnoreCase(CSS.Value.TOP, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_TOP, value)) { c.SetTextRise((float) (size/2 + 0.5)); } else if (Util.EqualsIgnoreCase(CSS.Value.SUB, value) || Util.EqualsIgnoreCase(CSS.Value.BOTTOM, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_BOTTOM, value)) { c.SetTextRise(-size/2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; if (rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale)) { if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", ""))/100; c.SetHorizontalScaling(100/float.Parse(xfaVertScale.Replace("%", ""))); } } if (rules.TryGetValue(CSS.Property.TEXT_DECORATION, out value)) { // Restriction? In html a underline and a line-through is possible on one piece of text. A Chunk can set an underline only once. if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, value)) { c.SetUnderline(0.75f, -size/8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, value)) { c.SetUnderline(0.75f, size/4f); } } if (rules.TryGetValue(CSS.Property.BACKGROUND_COLOR, out value)) { c.SetBackground(HtmlUtilities.DecodeColor(value)); } f.Size = size; c.Font = f; return c; }
//// --------------------------------------------------------------------------- // /** // * Creates a PdfPCell for a specific day // * @param dt a DateTime // * @return a PdfPCell // */ public PdfPCell GetDayCell(DateTime dt) { PdfPCell cell = new PdfPCell(); cell.Padding = 3; // set the background color, based on the type of day cell.BackgroundColor = IsSunday(dt) ? BaseColor.GRAY : IsSpecialDay(dt) ? BaseColor.LIGHT_GRAY : BaseColor.WHITE ; // set the content in the language of the locale Chunk chunk = new Chunk(dt.ToString("ddd"), small); chunk.SetTextRise(8); // a paragraph with the day Paragraph p = new Paragraph(chunk); // a separator p.Add(new Chunk(new VerticalPositionMark())); // and the number of the day p.Add(new Chunk(dt.ToString("%d"), normal)); cell.AddElement(p); return cell; }
public Chunk CreateChunk(String text, ChainedProperties props) { Chunk ck = new Chunk(text, GetFont(props)); if (props.HasProperty("sub")) ck.SetTextRise(-6); else if (props.HasProperty("sup")) ck.SetTextRise(6); return ck; }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, * com.itextpdf.tool.xml.Tag) */ public Chunk Apply(Chunk c, Tag t) { String fontName = null; String encoding = BaseFont.CP1252; float size = new FontSizeTranslator().GetFontSize(t); int style = Font.UNDEFINED; BaseColor color = null; IDictionary<String, String> rules = t.CSS; foreach (KeyValuePair<String, String> entry in rules) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_WEIGHT, key)) { if (CSS.Value.BOLD.Contains(value)) { if (style == Font.ITALIC) { style = Font.BOLDITALIC; } else { style = Font.BOLD; } } else { if (style == Font.BOLDITALIC) { style = Font.ITALIC; } else { style = Font.NORMAL; } } } else if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ITALIC)) { if (style == Font.BOLD) style = Font.BOLDITALIC; else style = Font.ITALIC; } if (Util.EqualsIgnoreCase(value, CSS.Value.OBLIQUE)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.FONT_FAMILY, key)) { if (value.Contains(",")){ String[] fonts = value.Split(','); foreach (String s in fonts) { string s2 = s.Trim(); if (!Util.EqualsIgnoreCase(FontFactory.GetFont(s2).Familyname, "unknown")){ fontName = s2; break; } } } else { fontName = value; } } else if (Util.EqualsIgnoreCase(CSS.Property.COLOR, key)) { color = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { c.SetCharacterSpacing(utils.ParsePxInCmMmPcToPt(value)); } else if (rules.ContainsKey(CSS.Property.XFA_FONT_HORIZONTAL_SCALE)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling(float.Parse(rules[CSS.Property.XFA_FONT_HORIZONTAL_SCALE].Replace("%", ""), CultureInfo.InvariantCulture)/100f); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.ContainsKey(CSS.Property.VERTICAL_ALIGN)) { String value = rules[CSS.Property.VERTICAL_ALIGN]; if (Util.EqualsIgnoreCase(value, CSS.Value.SUPER)||Util.EqualsIgnoreCase(value, CSS.Value.TOP)||Util.EqualsIgnoreCase(value, CSS.Value.TEXT_TOP)) { c.SetTextRise((float) (size / 2 + 0.5)); } else if (Util.EqualsIgnoreCase(value, CSS.Value.SUB)||Util.EqualsIgnoreCase(value, CSS.Value.BOTTOM)||Util.EqualsIgnoreCase(value, CSS.Value.TEXT_BOTTOM)) { c.SetTextRise(-size / 2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale); if (null != xfaVertScale) { // only % allowed; need a catch block NumberFormatExc? if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture)/100; c.SetHorizontalScaling(100/float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture)); } } if (rules.ContainsKey(CSS.Property.TEXT_DECORATION)) { // Restriction? In html a underline and a line-through is possible on one piece of text. A Chunk can set an underline only once. String value = rules[CSS.Property.TEXT_DECORATION]; if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, value)) { c.SetUnderline(0.75f, -size/8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, value)) { c.SetUnderline(0.75f, size/4f); } } if (rules.ContainsKey(CSS.Property.BACKGROUND_COLOR)) { c.SetBackground(HtmlUtilities.DecodeColor(rules[CSS.Property.BACKGROUND_COLOR])); } Font f = FontFactory.GetFont(fontName, encoding, BaseFont.EMBEDDED, size, style, color); c.Font = f; return c; }
/** * Creates a PdfPCell for a specific day * @param dt a DateTime * @return a PdfPCell */ public new PdfPCell GetDayCell(DateTime dt) { PdfPCell cell = new PdfPCell(); // set the event to draw the background cell.CellEvent = cellBackground; // set the event to draw a special border if (IsSunday(dt) || IsSpecialDay(dt)) cell.CellEvent = roundRectangle; cell.Padding = 3; cell.Border = PdfPCell.NO_BORDER; // set the content in the language of the locale Chunk chunk = new Chunk(dt.ToString("ddd"), small); chunk.SetTextRise(8); // a paragraph with the day Paragraph p = new Paragraph(chunk); // a separator p.Add(new Chunk(new VerticalPositionMark())); // and the number of the day p.Add(new Chunk(dt.ToString("%d"), normal)); cell.AddElement(p); return cell; }
public static void Genera(string sourcePath,string content) { sourcePath = HttpContext.Current.Server.MapPath(sourcePath); //定义一个Document,并设置页面大小为A4,竖向 iTextSharp.text.Document doc = new Document(PageSize.A4); try { //写实例 PdfWriter.GetInstance(doc, new FileStream(sourcePath, FileMode.Create)); //打开document doc.Open(); //载入字体 BaseFont baseFont = BaseFont.CreateFont( "C:\\WINDOWS\\FONTS\\SIMHEI.TTF", //黑体 BaseFont.IDENTITY_H, //横向字体 BaseFont.NOT_EMBEDDED); iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 9); //写入一个段落, Paragraph doc.Add(new Paragraph("第一段:" + content, font)); doc.Add(new Paragraph("这是第二段 !", font)); #region 图片 //以下代码用来添加图片,此时图片是做为背景加入到pdf文件当中的: Stream inputImageStream = new FileStream(HttpContext.Current.Server.MapPath("~/images/logo.jpg"), FileMode.Open, FileAccess.Read, FileShare.Read); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream); image.SetAbsolutePosition(0, 0); image.Alignment = iTextSharp.text.Image.UNDERLYING; //这里可以设定图片是做为背景还是做为元素添加到文件中 doc.Add(image); #endregion #region 其他元素 doc.Add(new Paragraph("Hello World")); //另起一行。有几种办法建立一个段落,如: Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.\n", FontFactory.GetFont(FontFactory.HELVETICA, 12))); Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12))); Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12)); //所有有些对象将被添加到段落中: p1.Add("you can add string here\n\t"); p1.Add(new Chunk("you can add chunks \n")); p1.Add(new Phrase("or you can add phrases.\n")); doc.Add(p1); doc.Add(p2); doc.Add(p3); //创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块: Chunk chunk = new Chunk("创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块", FontFactory.GetFont(FontFactory.COURIER, 20, iTextSharp.text.Font.COURIER, new iTextSharp.text.Color(255, 0, 0))); doc.Add(chunk); //如果你希望一些块有下划线或删除线,你可以通过改变字体风格简单做到: Chunk chunk1 = new Chunk("This text is underlined", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED)); Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.ITALIC | iTextSharp.text.Font.STRIKETHRU)); //改变块的背景 chunk2.SetBackground(new iTextSharp.text.Color(0xFF, 0xFF, 0x00)); //上标/下标 chunk1.SetTextRise(5); doc.Add(chunk1); doc.Add(chunk2); //外部链接示例: Anchor anchor = new Anchor("website", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED, new iTextSharp.text.Color(0, 0, 255))); anchor.Reference = "http://itextsharp.sourceforge.net"; anchor.Name = "website"; //内部链接示例: Anchor anchor1 = new Anchor("This is an internal link\n\n"); anchor1.Name = "link1"; Anchor anchor2 = new Anchor("Click here to jump to the internal link\n\f"); anchor2.Reference = "#link1"; doc.Add(anchor); doc.Add(anchor1); doc.Add(anchor2); //排序列表示例: List list = new List(true, 20); list.Add(new ListItem("First line")); list.Add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?")); list.Add(new ListItem("Third line")); doc.Add(list); //文本注释: Annotation a = new Annotation("authors", "Maybe its because I wanted to be an author myself that I wrote iText."); doc.Add(a); //包含页码没有任何边框的页脚。 iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new Phrase("This is page: "), true); footer.Border = iTextSharp.text.Rectangle.NO_BORDER; doc.Footer = footer; //Chapter对象和Section对象自动构建一个树: iTextSharp.text.Font f1 = new iTextSharp.text.Font(); f1.SetStyle(iTextSharp.text.Font.BOLD); Paragraph cTitle = new Paragraph("This is chapter 1", f1); Chapter chapter = new Chapter(cTitle, 1); Paragraph sTitle = new Paragraph("This is section 1 in chapter 1", f1); Section section = chapter.AddSection(sTitle, 1); doc.Add(chapter); //构建了一个简单的表: Table aTable = new Table(4, 4); aTable.AutoFillEmptyCells = true; aTable.AddCell("2.2", new System.Drawing.Point(2, 2)); aTable.AddCell("3.3", new System.Drawing.Point(3, 3)); aTable.AddCell("2.1", new System.Drawing.Point(2, 1)); aTable.AddCell("1.3", new System.Drawing.Point(1, 3)); doc.Add(aTable); //构建了一个不简单的表: Table table = new Table(3); table.BorderWidth = 1; table.BorderColor = new iTextSharp.text.Color(0, 0, 255); table.Cellpadding = 5; table.Cellspacing = 5; Cell cell = new Cell("header"); cell.Header = true; cell.Colspan = 3; table.AddCell(cell); cell = new Cell("example cell with colspan 1 and rowspan 2"); cell.Rowspan = 2; cell.BorderColor = new iTextSharp.text.Color(255, 0, 0); table.AddCell(cell); table.AddCell("1.1"); table.AddCell("2.1"); table.AddCell("1.2"); table.AddCell("2.2"); table.AddCell("cell test1"); cell = new Cell("big cell"); cell.Rowspan = 2; cell.Colspan = 2; cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0); table.AddCell(cell); table.AddCell("cell test2"); // 改变了单元格“big cell”的对齐方式: cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.VerticalAlignment = Element.ALIGN_MIDDLE; doc.Add(table); #endregion //关闭document doc.Close(); //打开PDF,看效果 //Process.Start(sourcePath); } catch (DocumentException de) { Console.WriteLine(de.Message); Console.ReadKey(); } catch (IOException io) { Console.WriteLine(io.Message); Console.ReadKey(); } }