public void Generate(List <Factor> factors) { Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create)); document.Open(); foreach (var factor in factors) { Paragraph p = new Paragraph(factor.ToString()); document.Add(p); RomanList romanList = new RomanList(true, 20); romanList.IndentationLeft = 15; foreach (var detail in factor.FactorDetails) { romanList.Add(detail.ToString()); } document.Add(romanList); } /*iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5); * * PdfPTable table = new PdfPTable(dt.Columns.Count); * PdfPRow row = null; * float[] widths = new float[] { 2f, 2f,2f}; * * table.SetWidths(widths); * * table.WidthPercentage = 100; * int iCol = 0; * string colname = ""; * PdfPCell cell = new PdfPCell(new Phrase("Factors")); * * cell.Colspan = dt.Columns.Count; * * foreach (DataColumn c in dt.Columns) * { * * table.AddCell(new Phrase(c.ColumnName, font5)); * } * * foreach (DataRow r in dt.Rows) * { * if (dt.Rows.Count > 0) * { * table.AddCell(new Phrase(r[0].ToString(), font5)); * table.AddCell(new Phrase(r[1].ToString(), font5)); * table.AddCell(new Phrase(r[2].ToString(), font5)); * table.AddCell(new Phrase(r[3].ToString(), font5)); * } * }*/ //document.Add(table); document.Close(); }
private static Section GetUniversitySection() { //Now starting with the real example var mainSec = new Section($"Report for: {John.Name}"); var persInfo = new Section("Personal Information", 2); mainSec.AddSection(persInfo); //Adding a pic with an external link (should works the same with an offline path) persInfo.Add(new Image("https://i.stack.imgur.com/l60Hf.png", "This should be a real pic", "Student photo", 100, 80)); persInfo.AddParagraph(); persInfo.AddQuote($"This is the profile pic of {John.Name}"); //A paragraph with a on-a-fly italic word persInfo.AddParagraph("This personal information is automatically mapped in the List reading the properties of the " + TextFormat.Ital("Student") + " class"); //Creating and auto-populating the Letterlist var autoLetterList = new NumberListAutoMap <Student>(); autoLetterList.SetItem(John); //autoLetterList.Type = LetterType.LOWERCASE; persInfo.Add(autoLetterList); //Now to the auto-parsed Exams summary table var examsSummarySec = new Section("Exams summary", 2); mainSec.AddSection(examsSummarySec); examsSummarySec.AddParagraph("The following table is automatically mapped from the list of " + TextFormat.Ital("Exam")); var tableExams = new TableAutoMap <Exam>(Exams.ToArray()); //this table will auto detect property names tableExams.SetAlignement(TableAlignment.CENTER); //this will set center for all the columns, you can set it for each every column examsSummarySec.Add(tableExams); //Last section: exams detail var examDetailsSec = new Section("Exams Detail", 2); mainSec.AddSection(examDetailsSec); examDetailsSec.AddParagraph("In this section we iterate over the list of exams and build a subsection of heading level 3 to have some details for each exam. I have also put an horizontal line between them."); var descTemplate = TemplateFromYaml.ReadFromYaml(EXAM_DETAIL_DESC_YAML); descTemplate.RenderMode = TemplateRenderMode.SINGLE; for (int i = 0; i < Exams.Count; i++) { var detailSec = new Section(Exams[i].Argument, 3); //create subsection detailSec.AddParagraph("Chapters:"); //adding a simple paragraph var chapList = new RomanList(); //adding the chapter list foreach (string chapter in ExamsDetails[i].Chapters) { chapList.AddItem(chapter); } detailSec.Add(chapList); descTemplate.ResultSingleID = Exams[i].Argument; //choose the correct ID for the Result of the template detailSec.AddParagraph(descTemplate.Render()); //adding the rendered template as parahraph examDetailsSec.AddSection(detailSec); //adding the subsection of level 3 to level 2 } return(mainSec); }
public ActionResult Pdf(FormCollection form) { Document doc = new Document(new iTextSharp.text.Rectangle(288f, 144f), 10, 10, 10, 10); //yeni bir döküman oluşturuyoruz. PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(sinavım.SınavTipi + ".pdf", FileMode.Create)); // dosyamıza bir ad veriyoruz ve yazılabilir hale getiriyoruz. doc.SetPageSize(iTextSharp.text.PageSize.A4); //pdfin ebatlarını belirliyoruz. doc.Open(); //dosyayla olan bağlantıyı açıyoruz ve böylelikle artık içerisine bir şeyler yazabilir hale getiriyoruz. Paragraph paragraf = new Paragraph("Trakya Üniversitesi" + "Bilgisayar Mühendisliği" + sinavım.DersAdi + sinavım.SınavTipi + "Sınavı"); doc.Add(paragraf); RomanList roman = new RomanList(true, 20); roman.IndentationLeft = 30f; foreach (var item in sinavım.SinavKagıdı) { if (item.Sorular.Sturu == "Test") { roman.Add(item.Sorular.Smetni); roman.Add(item.Cevaplar.A); roman.Add(item.Cevaplar.B); roman.Add(item.Cevaplar.C); roman.Add(item.Cevaplar.D); roman.Add(item.Cevaplar.E); } else { roman.Add(item.Sorular.Smetni); } } List list = new List(List.ORDERED, 40f); list.IndentationLeft = 40f; list.Add(roman); doc.Add(list); doc.Close(); System.Diagnostics.Process.Start(@sinavım.SınavTipi + ".pdf"); return(View()); }
/** * The ListCssApplier has the capabilities to change the type of the given {@link List} dependable on the css. * This means: <strong>Always replace your list with the returned one and add content to the list after applying!</strong> */ // not implemented: list-style-type:armenian, georgian, decimal-leading-zero. virtual public List Apply(List list, Tag t, IImageProvider htmlPipelineContext) { float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t); List lst = list; IDictionary <String, String> css = t.CSS; String styleType; css.TryGetValue(CSS.Property.LIST_STYLE_TYPE, out styleType); BaseColor color = HtmlUtilities.DecodeColor(css.ContainsKey(CSS.Property.COLOR) ? css[CSS.Property.COLOR] : null); if (null == color) { color = BaseColor.BLACK; } if (null != styleType) { if (Util.EqualsIgnoreCase(styleType, CSS.Value.NONE)) { lst.Lettered = false; lst.Numbered = false; lst.SetListSymbol(""); } else if (Util.EqualsIgnoreCase(CSS.Value.DECIMAL, styleType)) { lst = new List(List.ORDERED); SynchronizeSymbol(fontSize, lst, color); } else if (Util.EqualsIgnoreCase(CSS.Value.DISC, styleType)) { lst = new ZapfDingbatsList(108); lst.Autoindent = false; lst.SymbolIndent = 7.75f; Chunk symbol = lst.Symbol; symbol.SetTextRise(1.5f); Font font = symbol.Font; font.Size = 4.5f; font.Color = color; } else if (Util.EqualsIgnoreCase(CSS.Value.SQUARE, styleType)) { lst = new ZapfDingbatsList(110); ShrinkSymbol(lst, fontSize, color); } else if (Util.EqualsIgnoreCase(CSS.Value.CIRCLE, styleType)) { lst = new ZapfDingbatsList(109); lst.Autoindent = false; lst.SymbolIndent = 7.75f; Chunk symbol = lst.Symbol; symbol.SetTextRise(1.5f); Font font = symbol.Font; font.Size = 4.5f; font.Color = color; } else if (CSS.Value.LOWER_ROMAN.Equals(styleType)) { lst = new RomanList(true, 0); lst.Autoindent = true; SynchronizeSymbol(fontSize, lst, color); } else if (CSS.Value.UPPER_ROMAN.Equals(styleType)) { lst = new RomanList(false, 0); SynchronizeSymbol(fontSize, lst, color); lst.Autoindent = true; } else if (CSS.Value.LOWER_GREEK.Equals(styleType)) { lst = new GreekList(true, 0); SynchronizeSymbol(fontSize, lst, color); lst.Autoindent = true; } else if (CSS.Value.UPPER_GREEK.Equals(styleType)) { lst = new GreekList(false, 0); SynchronizeSymbol(fontSize, lst, color); lst.Autoindent = true; } else if (CSS.Value.LOWER_ALPHA.Equals(styleType) || CSS.Value.LOWER_LATIN.Equals(styleType)) { lst = new List(List.ORDERED, List.ALPHABETICAL); SynchronizeSymbol(fontSize, lst, color); lst.Lowercase = true; lst.Autoindent = true; } else if (CSS.Value.UPPER_ALPHA.Equals(styleType) || CSS.Value.UPPER_LATIN.Equals(styleType)) { lst = new List(List.ORDERED, List.ALPHABETICAL); SynchronizeSymbol(fontSize, lst, color); lst.Lowercase = false; lst.Autoindent = true; } } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.OL)) { lst = new List(List.ORDERED); SynchronizeSymbol(fontSize, lst, color); lst.Autoindent = true; } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.UL)) { lst = new List(List.UNORDERED); ShrinkSymbol(lst, fontSize, color); } if (css.ContainsKey(CSS.Property.LIST_STYLE_IMAGE) && !Util.EqualsIgnoreCase(css[CSS.Property.LIST_STYLE_IMAGE], CSS.Value.NONE)) { lst = new List(); String url = utils.ExtractUrl(css[CSS.Property.LIST_STYLE_IMAGE]); iTextSharp.text.Image img = null; try { if (htmlPipelineContext == null) { img = new ImageRetrieve().RetrieveImage(url); } else { try { img = new ImageRetrieve(htmlPipelineContext).RetrieveImage(url); } catch (NoImageException) { if (LOG.IsLogging(Level.TRACE)) { LOG.Trace(String.Format(LocaleMessages.GetInstance().GetMessage("css.applier.list.noimage"))); } img = new ImageRetrieve().RetrieveImage(url); } } lst.ListSymbol = new Chunk(img, 0, 0, false); lst.SymbolIndent = img.Width; if (LOG.IsLogging(Level.TRACE)) { LOG.Trace(String.Format(LocaleMessages.GetInstance().GetMessage("html.tag.list"), url)); } } catch (IOException e) { if (LOG.IsLogging(Level.ERROR)) { LOG.Error(String.Format(LocaleMessages.GetInstance().GetMessage("html.tag.list.failed"), url), e); } lst = new List(List.UNORDERED); } catch (NoImageException e) { if (LOG.IsLogging(Level.ERROR)) { LOG.Error(e.Message, e); } lst = new List(List.UNORDERED); } lst.Autoindent = false; } lst.Alignindent = false; float leftIndent = 0; if (css.ContainsKey(CSS.Property.LIST_STYLE_POSITION) && Util.EqualsIgnoreCase(css[CSS.Property.LIST_STYLE_POSITION], CSS.Value.INSIDE)) { leftIndent += 30; } else { leftIndent += 15; } leftIndent += css.ContainsKey(CSS.Property.MARGIN_LEFT)?utils.ParseValueToPt(css[CSS.Property.MARGIN_LEFT], fontSize):0; leftIndent += css.ContainsKey(CSS.Property.PADDING_LEFT)?utils.ParseValueToPt(css[CSS.Property.PADDING_LEFT], fontSize):0; lst.IndentationLeft = leftIndent; String startAtr = null; t.Attributes.TryGetValue(HTML.Attribute.START, out startAtr); if (startAtr != null) { try { int start = int.Parse(startAtr); lst.First = start; } catch (FormatException exc) { } } return(lst); }
//Creating the pdf document using the data from the datagridview public static void createPDFDocument(DataGridView d) { Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("test.pdf", FileMode.Create)); doc.Open(); iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("pic.jpg"); //png.ScalePercent(200);//sizing png.ScaleToFit(50f, 70f); png.Border = iTextSharp.text.Rectangle.BOX; png.BorderColor = iTextSharp.text.BaseColor.YELLOW; png.BorderWidth = 3; //png.SetAbsolutePosition(doc.PageSize.Width - 36f - 72f, doc.PageSize.Width - 40f - 72f);//positioning doc.Add(png); Paragraph p = new Paragraph("This is my test document"); doc.Add(p); //create and add a list to the pdf file List list = new List(List.UNORDERED); //other list type: RomanList list.IndentationLeft = 30f; list.Add("one"); list.Add("twoo"); list.Add("three"); list.Add("four"); RomanList rl = new RomanList(true, 20); rl.Add("one"); rl.Add("twoo"); rl.Add("List"); rl.Add(list); rl.Add("four"); doc.Add(rl); PdfPTable t = new PdfPTable(3); // The bellow code is for adding a normala table to the pdf //PdfPCell cell = new PdfPCell(new Phrase("this is a phare")); //cell.Colspan = 3; //cell.HorizontalAlignment = 1; // 0-left, 1-centre, 2 right //cell.BackgroundColor = new iTextSharp.text.BaseColor(0, 150, 0); //t.AddCell(cell); //t.AddCell("col 2 row 1"); //t.AddCell("col 3 row 1"); //t.AddCell("col 1 row 2"); //t.AddCell("col 2 row 2"); //t.AddCell("col 3 row 2"); //doc.Add(t); //loading tha table if (d.Columns.Count == 0) { MessageBox.Show("load the table first stupid, or it will throw an exception and the table will not be loaded"); } else { PdfPTable ta = new PdfPTable(d.Columns.Count); for (int j = 0; j < d.Columns.Count; j++) { ta.AddCell(new Phrase(d.Columns[j].HeaderText)); } ta.HeaderRows = 1; for (int i = 0; i < d.Columns.Count; i++) { for (int k = 0; k < d.Columns.Count; k++) { if (d[k, i].Value != null) { ta.AddCell(new Phrase(d[k, i].Value.ToString())); } } } doc.Add(ta); //Opening the PDF file System.Diagnostics.Process.Start("test.pdf"); } doc.Close(); }
public IActionResult ArquivoMemoria() { Document doc = new Document(PageSize.A4, 100, 70, 100, 70); MemoryStream stream = new MemoryStream(); PdfWriter pdf = PdfWriter.GetInstance(doc, stream); pdf.CloseStream = false; var fonteTitulo = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16); var fonte12 = FontFactory.GetFont(FontFactory.HELVETICA, 12); var fonte12Bold = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12); var fonte12Azul = FontFactory.GetFont(FontFactory.HELVETICA, 12, BaseColor.Blue); var fonte20Vermelho = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20, BaseColor.Red); TwoColumnHeaderFooter cabecalhoRodape = new TwoColumnHeaderFooter(); pdf.PageEvent = cabecalhoRodape; cabecalhoRodape.Title = "AULA SOBRE iTEXTSHARP"; cabecalhoRodape.HeaderLeft = "UNOESTE"; cabecalhoRodape.HeaderRight = "FIPP"; cabecalhoRodape.HeaderFont = fonte20Vermelho; doc.Open(); Paragraph linhaBranco = new Paragraph(" "); Uri urlImagemUnoeste = new Uri("http://www.unoeste.br/Content/imagens/imgpadrao.jpg"); Image imgUnoeste = Image.GetInstance(urlImagemUnoeste); imgUnoeste.ScalePercent(30); //Para acessibilidade //Ainda não preparado para o .net core (itextsharp) //var c = new Chunk(imgUnoeste, 0, -24); //c.setAccessibleAttribute(PdfName.Alt, new PdfString("Logo da Unoeste")); string caminhoImgFusca = _env.WebRootPath + "\\imagens\\fusca.jpg"; Image imgFusca = Image.GetInstance(caminhoImgFusca); imgFusca.ScaleAbsoluteWidth(50); imgFusca.ScaleAbsoluteHeight(32); imgFusca.SetAbsolutePosition(doc.PageSize.Width - 100, doc.PageSize.Height - 50); Paragraph pTitulo1 = new Paragraph("UNOESTE - Universidade do Oeste Paulista", fonteTitulo); doc.Add(pTitulo1); Paragraph pTitulo2 = new Paragraph("FIPP - Faculdade de Informática de Presidente Prudente", fonteTitulo); doc.Add(pTitulo2); doc.Add(imgUnoeste); for (int i = 1; i <= 10; i++) { Paragraph p = new Paragraph("Conteúdo da linha: " + i, fonte12); doc.Add(p); } doc.Add(linhaBranco); doc.Add(imgFusca); Phrase frase = new Phrase(); frase.Add(new Chunk("Olá ", fonte12)); frase.Add(new Chunk("FULANO DE TAL ", fonte12Azul)); frase.Add(new Chunk("hoje é segunda-feira", fonte12Bold)); doc.Add(frase); PdfPTable tabela = new PdfPTable(3); tabela.WidthPercentage = 100; PdfPCell celulaTitulo = new PdfPCell(new Phrase("TÍTULO DA TABELA", fonte12Bold)); celulaTitulo.Colspan = 3; celulaTitulo.HorizontalAlignment = 1; celulaTitulo.BackgroundColor = new BaseColor(System.Drawing.Color.FloralWhite); celulaTitulo.MinimumHeight = 35; tabela.AddCell(celulaTitulo); tabela.AddCell("Linha 1 x Coluna 1"); tabela.AddCell("Linha 1 x Coluna 2"); tabela.AddCell("Linha 1 x Coluna 3"); tabela.AddCell("Linha 2 x Coluna 1"); tabela.AddCell(imgUnoeste); tabela.AddCell("Linha 2 x Coluna 3"); tabela.AddCell(celulaTitulo); tabela.AddCell("Linha 3 x Coluna 1"); tabela.AddCell("Linha 3 x Coluna 2"); tabela.AddCell("Linha 3 x Coluna 3"); doc.Add(tabela); doc.NewPage(); List lista1 = new List(List.LOWERCASE); lista1.Add("Fusca"); lista1.Add("Passat Pointer"); lista1.Add("SP2"); lista1.Add("Variant"); lista1.Add("Brasilia"); doc.Add(linhaBranco); doc.Add(lista1); List lista2 = new List(List.ALPHABETICAL, true); lista2.Add("Fusca"); lista2.Add("Passat Pointer"); lista2.Add("SP2"); lista2.Add("Variant"); lista2.Add("Brasilia"); doc.Add(linhaBranco); doc.Add(lista2); List lista3 = new List(List.UNORDERED); lista3.Add("Fusca"); lista3.Add("Passat Pointer"); lista3.Add("SP2"); lista3.Add("Variant"); lista3.Add("Brasilia"); doc.Add(linhaBranco); doc.Add(lista3); List lista4 = new List(List.ORDERED, 15); lista4.Add("Fusca"); lista4.Add("Passat Pointer"); lista4.Add(lista3); lista4.Add("SP2"); lista4.Add("Variant"); lista4.Add("Brasilia"); doc.Add(linhaBranco); doc.Add(lista4); RomanList lista5 = new RomanList(true, 15); lista5.Add("Fusca"); lista5.Add("Passat Pointer"); lista5.Add("SP2"); lista5.Add(lista4); lista5.Add("Variant"); lista5.Add("Brasilia"); doc.Add(linhaBranco); doc.Add(lista5); doc.Close(); stream.Flush(); stream.Position = 0; return(File(stream, "application/pdf", "Contrato-Memoria.pdf")); }
// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 var SQL = @"SELECT DISTINCT mc.country_id, c.country, count(*) AS c FROM film_country c, film_movie_country mc WHERE c.id = mc.country_id GROUP BY mc.country_id, country ORDER BY c DESC"; // Create a list for the countries List list = new RomanList(); // loop over the countries using (var c = AdoDB.Provider.CreateConnection()) { c.ConnectionString = AdoDB.CS; using (DbCommand cmd = c.CreateCommand()) { cmd.CommandText = SQL; c.Open(); using (var r = cmd.ExecuteReader()) { while (r.Read()) { // create a list item for the country ListItem item = new ListItem( string.Format("{0}: {1} movies", r["country"].ToString(), r["c"].ToString() ) ); // Create a list for the movies List movielist = new GreekList(); movielist.Lowercase = List.LOWERCASE; // Loop over the movies foreach (Movie movie in PojoFactory.GetMovies(r["country_id"].ToString()) ) { ListItem movieitem = new ListItem(movie.MovieTitle); // Create a list for the directors List directorlist = new ZapfDingbatsNumberList(0); // Loop over the directors foreach (Director director in movie.Directors) { directorlist.Add(String.Format("{0}, {1}", director.Name, director.GivenName )); } movieitem.Add(directorlist); movielist.Add(movieitem); } item.Add(movielist); list.Add(item); } document.Add(list); } } } } }
public void NestedListAtTheEndOfAnotherNestedList() { String pdfFile = "nestedListAtTheEndOfAnotherNestedList.pdf"; // step 1 Document document = new Document(); // step 2 PdfWriter.GetInstance(document, File.Create(DEST_FOLDER + pdfFile)); // step 3 document.Open(); // step 4 PdfPTable table = new PdfPTable(1); PdfPCell cell = new PdfPCell(); cell.BackgroundColor = BaseColor.ORANGE; RomanList romanlist = new RomanList(true, 20); romanlist.IndentationLeft = 10f; romanlist.Add("One"); romanlist.Add("Two"); romanlist.Add("Three"); RomanList romanlist2 = new RomanList(true, 20); romanlist2.IndentationLeft = 10f; romanlist2.Add("One"); romanlist2.Add("Two"); romanlist2.Add("Three"); romanlist.Add(romanlist2); //romanlist.add("Four"); List list = new List(List.ORDERED, 20f); list.SetListSymbol("\u2022"); list.IndentationLeft = 20f; list.Add("One"); list.Add("Two"); list.Add("Three"); list.Add("Four"); list.Add("Roman List"); list.Add(romanlist); list.Add("Five"); list.Add("Six"); cell.AddElement(list); table.AddCell(cell); document.Add(table); // step 5 document.Close(); CompareTool compareTool = new CompareTool(); String error = compareTool.CompareByContent(DEST_FOLDER + pdfFile, SOURCE_FOLDER + pdfFile, DEST_FOLDER, "diff_"); if (error != null) { Assert.Fail(error); } }