public void Write(string outputPath, FlowDocument doc) { Document pdfDoc = new Document(PageSize.A4, 50, 50, 50, 50); Font textFont = new Font(baseFont, DEFAULT_FONTSIZE, Font.NORMAL, BaseColor.BLACK); Font headingFont = new Font(baseFont, DEFAULT_HEADINGSIZE, Font.BOLD, BaseColor.BLACK); using (FileStream stream = new FileStream(outputPath, FileMode.Create)) { PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); foreach (Block i in doc.Blocks) { if (i is System.Windows.Documents.Paragraph) { TextRange range = new TextRange(i.ContentStart, i.ContentEnd); Console.WriteLine(i.Tag); switch (i.Tag as string) { case "Paragraph": iTextSharp.text.Paragraph par = new iTextSharp.text.Paragraph(range.Text, textFont); par.Alignment = Element.ALIGN_JUSTIFIED; pdfDoc.Add(par); break; case "Heading": iTextSharp.text.Paragraph head = new iTextSharp.text.Paragraph(range.Text, headingFont); head.Alignment = Element.ALIGN_CENTER; head.SpacingAfter = 10; pdfDoc.Add(head); break; default: iTextSharp.text.Paragraph def = new iTextSharp.text.Paragraph(range.Text, textFont); def.Alignment = Element.ALIGN_JUSTIFIED; pdfDoc.Add(def); break; } } else if (i is System.Windows.Documents.List) { iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); list.IndentationLeft = 15f; foreach (var li in (i as System.Windows.Documents.List).ListItems) { iTextSharp.text.ListItem listitem = new iTextSharp.text.ListItem(); TextRange range = new TextRange(li.Blocks.ElementAt(0).ContentStart, li.Blocks.ElementAt(0).ContentEnd); string text = range.Text.Substring(1); iTextSharp.text.Paragraph par = new iTextSharp.text.Paragraph(text, textFont); listitem.SpacingAfter = 10; listitem.Add(par); list.Add(listitem); } pdfDoc.Add(list); } } if (pdfDoc.PageNumber == 0) { iTextSharp.text.Paragraph par = new iTextSharp.text.Paragraph(" "); par.Alignment = Element.ALIGN_JUSTIFIED; pdfDoc.Add(par); } pdfDoc.Close(); } }
public void Build(List <Hold> hold, string outputPath, string year) { var doc = new Document(); PdfWriter.GetInstance(doc, new FileStream(outputPath, FileMode.Create)); doc.Open(); AddHeader(year, doc, false); int cnt = 0; iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); list.IndentationLeft = 20f; foreach (var team in hold.Where(h => h.Udskudt == false)) { cnt++; AddTeam(team, doc, cnt % 2 == 1); if (cnt % 2 == 0) { doc.NewPage(); } } addLargeFooter(doc); doc.Close(); }
/// <summary> /// Add a page that includes a bullet list. /// </summary> /// <param name="doc"></param> private void AddPageWithBulletList(iTextSharp.text.Document doc, List <string> data, string listName, string warningMessage) { // Add a new page to the document doc.NewPage(); // The header at the top of the page is an anchor linked to by the table of contents. iTextSharp.text.Anchor contentsAnchor = new iTextSharp.text.Anchor(listName, this.largeFont); contentsAnchor.Name = "research"; // Add the header anchor to the page this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, this.largeFont, contentsAnchor); // Create an unordered bullet list. The 10f argument separates the bullet from the text by 10 points iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); // Set the bullet symbol (without this a hypen starts each list item) list.IndentationLeft = 20f; // Indent the list 20 points foreach (var emp in data) { list.Add(new ListItem(emp, this.standardFont)); } doc.Add(list); // Add the list to the page // Add some white space and another heading this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, this.largeFont, new Chunk("\n\n\n")); this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, this.largeFont, new Chunk("WARNING (!)\n\n")); // Add some final text to the page this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_LEFT, this.standardFont, new Chunk(warningMessage)); }
/// <summary> /// Add a page that includes a bullet list. /// </summary> /// <param name="doc"></param> private void AddPageWithBulletList(iTextSharp.text.Document doc) { // Add a new page to the document doc.NewPage(); // The header at the top of the page is an anchor linked to by the table of contents. iTextSharp.text.Anchor contentsAnchor = new iTextSharp.text.Anchor("Beginning page\n\n", _largeFont); contentsAnchor.Name = "start"; // Add the header anchor to the page this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, contentsAnchor); // Create an unordered bullet list. The 10f argument separates the bullet from the text by 10 points iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); // Set the bullet symbol (without this a hypen starts each list item) list.IndentationLeft = 20f; // Indent the list 20 points list.Add(new ListItem("Print document", _standardFont)); list.Add(new ListItem("Route to mail room", _standardFont)); list.Add(new ListItem("Route to accounting", _standardFont)); list.Add(new ListItem("Check approval", _standardFont)); list.Add(new ListItem("Send the check", _standardFont)); doc.Add(list); // Add the list to the page // Add some white space and another heading this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("\n\n\n")); this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("Error condition\n\n")); // Add some final text to the page this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_LEFT, _standardFont, new Chunk("In case of error, check will be manually approved")); }
/// <summary> /// Add a page that includes a bullet list. /// </summary> /// <param name="doc"></param> private void AddPageWithBulletList(iTextSharp.text.Document doc) { // Add a new page to the document doc.NewPage(); // The header at the top of the page is an anchor linked to by the table of contents. iTextSharp.text.Anchor contentsAnchor = new iTextSharp.text.Anchor("RESEARCH\n\n", _largeFont); contentsAnchor.Name = "research"; // Add the header anchor to the page this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, contentsAnchor); // Create an unordered bullet list. The 10f argument separates the bullet from the text by 10 points iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); // Set the bullet symbol (without this a hypen starts each list item) list.IndentationLeft = 20f; // Indent the list 20 points list.Add(new iTextSharp.text.ListItem("Lift, thrust, drag, and gravity are the four forces that act on a plane.", _standardFont)); list.Add(new iTextSharp.text.ListItem("A plane should be light to help fight against gravity's pull to the ground.", _standardFont)); list.Add(new iTextSharp.text.ListItem("Gravity will have less effect on a plane built from the lightest materials available.", _standardFont)); list.Add(new iTextSharp.text.ListItem("In order to fly well, airplanes must be stable.", _standardFont)); list.Add(new iTextSharp.text.ListItem("A plane that is unstable will either pitch up into a stall, or nose-dive.", _standardFont)); doc.Add(list); // Add the list to the page // Add some white space and another heading this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("\n\n\nHYPOTHESIS\n\n")); // Add some final text to the page this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_LEFT, _standardFont, new Chunk("Given five paper airplanes made out of newspaper, printer paper, construction paper, paper towel, and posterboard, the airplane made out of printer paper will fly the furthest.")); }
public void BuildSimple(List <Hold> hold, string outputPath, string year) { var doc = new Document(); PdfWriter.GetInstance(doc, new FileStream(outputPath, FileMode.Create)); doc.Open(); AddHeader(year, doc, true); int cnt = 0; iTextSharp.text.Font subHeader2 = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.Black); Paragraph p = new Paragraph(); p.Alignment = Element.ALIGN_LEFT; Chunk subChunk2 = new Chunk("Børnefritidsforeningen i sydhavnen udbyder fritidsaktiviteter for alle børn i og omkring sydhavnen. Sæsonen løber typisk fra medio september til ultimo april og tilmelding til foreningens aktiviteter åbnes start september\n\nI år udbydes følgende aktiviteter:\n\n", subHeader2); p.Add(subChunk2); iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); list.IndentationLeft = 20f; foreach (var team in hold.Where(h => h.Udskudt == false)) { list.Add(team.Name + " for " + team.Age + " - " + team.WeekDay + " " + team.Time + " i " + team.Place); } p.Add(list); doc.Add(p); iTextSharp.text.Image img = null; string imgPath = Path.Combine(Directory.GetCurrentDirectory(), "images/" + "banner.png"); if (File.Exists(imgPath)) { img = iTextSharp.text.Image.GetInstance(imgPath); img.ScaleToFit(500, 180f); img.Alignment = iTextSharp.text.Image.TEXTWRAP | iTextSharp.text.Image.ALIGN_CENTER; img.IndentationLeft = 1f; img.SpacingAfter = 1f; img.BorderWidthTop = 1f; img.BorderColorTop = iTextSharp.text.BaseColor.White; doc.Add(img); } else { Console.WriteLine("Kan ikke finde billedet banner.png"); } addFooter(doc); doc.Close(); }
public static void PageList(Document pdf) { var list1 = new iTextSharp.text.List(); list1.IndentationLeft = 12; list1.SetListSymbol("\u2022"); //Unicode Character 'BULLET' list1.Numbered = true; list1.Add(new ListItem("Value 1")); list1.Add(new ListItem("Value 2")); list1.Add(new ListItem("Value 3")); pdf.Add(list1); pdf.Add(new Chunk("\n")); //Tip to add Break Line list1 = new iTextSharp.text.List(); list1.IndentationLeft = 20; list1.SetListSymbol("\u2022"); //Unicode Character 'BULLET' list1.Numbered = false; list1.Add(new ListItem("Value 1")); list1.Add(new ListItem("Value 2")); list1.Add(new ListItem("Value 3")); pdf.Add(list1); pdf.Add(new Chunk("\n")); //Tip to add Break Line Font _font = FontFactory.GetFont(BaseFont.ZAPFDINGBATS, 8f, Font.BOLD, BaseColor.Magenta); list1 = new iTextSharp.text.List(); //list1.IndentationLeft = 18; list1.ListSymbol = new Chunk("H ", _font); list1.Numbered = false; list1.Add(new ListItem("Value 1")); list1.Add(new ListItem("Value 2")); list1.Add(new ListItem("Value 3")); pdf.Add(list1); }
public virtual void StartElement(String tag, Hashtable h) { if (!tagsSupported.ContainsKey(tag)) return; style.ApplyStyle(tag, h); String follow = (String)FactoryProperties.followTags[tag]; if (follow != null) { Hashtable prop = new Hashtable(); prop[follow] = null; cprops.AddToChain(follow, prop); return; } FactoryProperties.InsertStyle(h, cprops); if (tag.Equals(HtmlTags.ANCHOR)) { cprops.AddToChain(tag, h); if (currentParagraph == null) currentParagraph = new Paragraph(); stack.Push(currentParagraph); currentParagraph = new Paragraph(); return; } if (tag.Equals(HtmlTags.NEWLINE)) { if (currentParagraph == null) currentParagraph = new Paragraph(); currentParagraph.Add(factoryProperties.CreateChunk("\n", cprops)); return; } if (tag.Equals(HtmlTags.HORIZONTALRULE)) { // Attempting to duplicate the behavior seen on Firefox with // http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_hr_test // where an initial break is only inserted when the preceding element doesn't // end with a break, but a trailing break is always inserted. bool addLeadingBreak = true; if (currentParagraph == null) { currentParagraph = new Paragraph(); addLeadingBreak = false; } if (addLeadingBreak) { // Not a new paragraph int numChunks = currentParagraph.Chunks.Count; if (numChunks == 0 || ((Chunk)currentParagraph.Chunks[numChunks - 1]).Content.EndsWith("\n")) addLeadingBreak = false; } String align = (String)h["align"]; int hrAlign = Element.ALIGN_CENTER; if (align != null) { if (Util.EqualsIgnoreCase(align, "left")) hrAlign = Element.ALIGN_LEFT; if (Util.EqualsIgnoreCase(align, "right")) hrAlign = Element.ALIGN_RIGHT; } String width = (String)h["width"]; float hrWidth = 1; if (width != null) { float tmpWidth = Markup.ParseLength(width, Markup.DEFAULT_FONT_SIZE); if (tmpWidth > 0) hrWidth = tmpWidth; if (!width.EndsWith("%")) hrWidth = 100; // Treat a pixel width as 100% for now. } String size = (String)h["size"]; float hrSize = 1; if (size != null) { float tmpSize = Markup.ParseLength(size, Markup.DEFAULT_FONT_SIZE); if (tmpSize > 0) hrSize = tmpSize; } if (addLeadingBreak) currentParagraph.Add(Chunk.NEWLINE); currentParagraph.Add(new LineSeparator(hrSize, hrWidth, null, hrAlign, currentParagraph.Leading/2)); currentParagraph.Add(Chunk.NEWLINE); return; } if (tag.Equals(HtmlTags.CHUNK) || tag.Equals(HtmlTags.SPAN)) { cprops.AddToChain(tag, h); return; } if (tag.Equals(HtmlTags.IMAGE)) { String src = (String)h[ElementTags.SRC]; if (src == null) return; cprops.AddToChain(tag, h); Image img = null; if (interfaceProps != null) { IImageProvider ip = (IImageProvider)interfaceProps["img_provider"]; if (ip != null) img = ip.GetImage(src, h, cprops, document); if (img == null) { Hashtable images = (Hashtable)interfaceProps["img_static"]; if (images != null) { Image tim = (Image)images[src]; if (tim != null) img = Image.GetInstance(tim); } else { if (!src.StartsWith("http")) { // relative src references only String baseurl = (String)interfaceProps["img_baseurl"]; if (baseurl != null) { src = baseurl + src; img = Image.GetInstance(src); } } } } } if (img == null) { if (!src.StartsWith("http")) { String path = cprops["image_path"]; if (path == null) path = ""; src = Path.Combine(path, src); } img = Image.GetInstance(src); } String align = (String)h["align"]; String width = (String)h["width"]; String height = (String)h["height"]; String before = cprops["before"]; String after = cprops["after"]; if (before != null) img.SpacingBefore = float.Parse(before, System.Globalization.NumberFormatInfo.InvariantInfo); if (after != null) img.SpacingAfter = float.Parse(after, System.Globalization.NumberFormatInfo.InvariantInfo); float actualFontSize = Markup.ParseLength(cprops[ElementTags.SIZE], Markup.DEFAULT_FONT_SIZE); if (actualFontSize <= 0f) actualFontSize = Markup.DEFAULT_FONT_SIZE; float widthInPoints = Markup.ParseLength(width, actualFontSize); float heightInPoints = Markup.ParseLength(height, actualFontSize); if (widthInPoints > 0 && heightInPoints > 0) { img.ScaleAbsolute(widthInPoints, heightInPoints); } else if (widthInPoints > 0) { heightInPoints = img.Height * widthInPoints / img.Width; img.ScaleAbsolute(widthInPoints, heightInPoints); } else if (heightInPoints > 0) { widthInPoints = img.Width * heightInPoints / img.Height; img.ScaleAbsolute(widthInPoints, heightInPoints); } img.WidthPercentage = 0; if (align != null) { EndElement("p"); int ralign = Image.MIDDLE_ALIGN; if (Util.EqualsIgnoreCase(align, "left")) ralign = Image.LEFT_ALIGN; else if (Util.EqualsIgnoreCase(align, "right")) ralign = Image.RIGHT_ALIGN; img.Alignment = ralign; IImg i = null; bool skip = false; if (interfaceProps != null) { i = (IImg)interfaceProps["img_interface"]; if (i != null) skip = i.Process(img, h, cprops, document); } if (!skip) document.Add(img); cprops.RemoveChain(tag); } else { cprops.RemoveChain(tag); if (currentParagraph == null) currentParagraph = FactoryProperties.CreateParagraph(cprops); currentParagraph.Add(new Chunk(img, 0, 0)); } return; } EndElement("p"); if (tag.Equals("h1") || tag.Equals("h2") || tag.Equals("h3") || tag.Equals("h4") || tag.Equals("h5") || tag.Equals("h6")) { if (!h.ContainsKey(ElementTags.SIZE)) { int v = 7 - int.Parse(tag.Substring(1)); h[ElementTags.SIZE] = v.ToString(); } cprops.AddToChain(tag, h); return; } if (tag.Equals(HtmlTags.UNORDEREDLIST)) { if (pendingLI) EndElement(HtmlTags.LISTITEM); skipText = true; cprops.AddToChain(tag, h); List list = new List(false); try{ list.IndentationLeft = float.Parse(cprops["indent"], System.Globalization.NumberFormatInfo.InvariantInfo); }catch { list.Autoindent = true; } list.SetListSymbol("\u2022"); stack.Push(list); return; } if (tag.Equals(HtmlTags.ORDEREDLIST)) { if (pendingLI) EndElement(HtmlTags.LISTITEM); skipText = true; cprops.AddToChain(tag, h); List list = new List(true); try{ list.IndentationLeft = float.Parse(cprops["indent"], System.Globalization.NumberFormatInfo.InvariantInfo); }catch { list.Autoindent = true; } stack.Push(list); return; } if (tag.Equals(HtmlTags.LISTITEM)) { if (pendingLI) EndElement(HtmlTags.LISTITEM); skipText = false; pendingLI = true; cprops.AddToChain(tag, h); stack.Push(FactoryProperties.CreateListItem(cprops)); return; } if (tag.Equals(HtmlTags.DIV) || tag.Equals(HtmlTags.BODY) || tag.Equals("p")) { cprops.AddToChain(tag, h); return; } if (tag.Equals(HtmlTags.PRE)) { if (!h.ContainsKey(ElementTags.FACE)) { h[ElementTags.FACE] = "Courier"; } cprops.AddToChain(tag, h); isPRE = true; return; } if (tag.Equals("tr")) { if (pendingTR) EndElement("tr"); skipText = true; pendingTR = true; cprops.AddToChain("tr", h); return; } if (tag.Equals("td") || tag.Equals("th")) { if (pendingTD) EndElement(tag); skipText = false; pendingTD = true; cprops.AddToChain("td", h); stack.Push(new IncCell(tag, cprops)); return; } if (tag.Equals("table")) { cprops.AddToChain("table", h); IncTable table = new IncTable(h); stack.Push(table); tableState.Push(new bool[]{pendingTR, pendingTD}); pendingTR = pendingTD = false; skipText = true; return; } }
private List BuildListObject(List<HtmlElement> elements, string listType, PdfPCell pCell = null) { var docObjs = new Stack<HtmlElement>(); var lt = listType == "ol" ? List.ORDERED : List.UNORDERED; // remove the ol objects elements.RemoveAll(a => a.Value == listType); var list = new iTextSharp.text.List(lt); if (lt == List.UNORDERED) list.SetListSymbol("\u2022"); list.IndentationLeft = 10f; foreach (var a in elements) { var obj = HandleTag(docObjs, a, pCell); if (obj != null) { list.Add(new ListItem(obj)); } } if (pCell != null) pCell.AddElement(list); return list; }
private void AddAssumptions(Project project, Document document) { if (project.Assumptions == null || project.Assumptions.Count < 1) return; var element = new Paragraph("Assumptions", TitleFont()); element.SpacingBefore = 5f; document.Add(element); var list = new List(false, false, 10); list.SetListSymbol("\u2022"); list.IndentationLeft = 10f; foreach (var assumption in project.Assumptions) { var item = new ListItem(assumption); list.Add(item); } document.Add(list); }
private void btn_createPDF_Click(object sender, EventArgs e) { Document doc = new Document(iTextSharp.text.PageSize.A4); string pdfFileName = "Test.pdf"; PdfWriter wri = PdfWriter.GetInstance(doc,new FileStream(pdfFileName,FileMode.Create)); doc.Open(); // Open Document to write // add image iTextSharp.text.Image JPEG = iTextSharp.text.Image.GetInstance("Basic_logo.jpg"); // resize image //JPEG.ScalePercent(50f); JPEG.ScaleToFit(150f,150f); JPEG.Border = iTextSharp.text.Rectangle.BOX; JPEG.BorderColor = iTextSharp.text.BaseColor.YELLOW; JPEG.BorderWidth = 1f; // location of image JPEG.SetAbsolutePosition(doc.PageSize.Width -32f-212f,doc.PageSize.Height - 36f); doc.Add(JPEG); // write some content Paragraph paragraph = new Paragraph("This is my first line using Paragraph.\n Hello world!"); // add the above created text using different class object to our PDF document doc.Add(paragraph); //--- 編號為羅馬編號 RomanList romanlist = new RomanList(true,15); // true:lowwer case, 50: 編號後的間距 romanlist.IndentationLeft = 30f; romanlist.Add("One"); romanlist.Add("two"); romanlist.Add("Three"); romanlist.Add("Four"); romanlist.Add("Five"); //doc.Add(romanlist); List list = new List(List.ORDERED,40f); list.SetListSymbol("\u2022"); list.IndentationLeft = 40f; // 向左移 list.Add("One"); list.Add("Two"); list.Add("Three"); list.Add("Roman List"); list.Add(romanlist); // list in list list.Add("Four"); list.Add("Five"); doc.Add(list); paragraph = new Paragraph("\n"); doc.Add(paragraph); //PdfPTable table = new PdfPTable(3); ////--- table header //PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns", new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL,20f,iTextSharp.text.Font.NORMAL,iTextSharp.text.BaseColor.YELLOW))); //cell.BackgroundColor = new iTextSharp.text.BaseColor(0,150,0); // RGB //cell.Colspan = 3; // header size expand to 3 column //cell.HorizontalAlignment = 1; // 0 = Left, 1 = Centre, 2 = Right //table.AddCell(cell); ////--- //table.AddCell("Col 1 Row 1"); //table.AddCell("Col 2 Row 1"); //table.AddCell("Col 3 Row 1"); //table.AddCell("Col 1 Row 2"); //table.AddCell("Col 2 Row 2"); //table.AddCell("Col 3 Row 2"); //doc.Add(table); PdfPTable table = new PdfPTable(dataGridView1.Columns.Count); // add the headers from the DGV to the table for (int i = 0; i < dataGridView1.Columns.Count; i++) { table.AddCell(new Phrase(dataGridView1.Columns[i].HeaderText)); } // Flag the firt row as a header table.HeaderRows = 1; // add the actual rows from the DGV to the table for (int j = 0; j < dataGridView1.Rows.Count; j++) { for (int k = 0; k < dataGridView1.Columns.Count; k++) { if (dataGridView1[k,j].Value != null) { table.AddCell(new Phrase(dataGridView1[k,j].Value.ToString())); } } } doc.Add(table); // add chart to pdf var chartimage = new MemoryStream(); chart1.SaveImage(chartimage, ChartImageFormat.Tiff); iTextSharp.text.Image Chart_image = iTextSharp.text.Image.GetInstance(chartimage.GetBuffer()); Chart_image.ScalePercent(150f); doc.Add(Chart_image); doc.Close(); //--- After finish create pdf, open the file's path string path = System.AppDomain.CurrentDomain.BaseDirectory; try { #region 開路徑 //if (!Directory.Exists(path)) //{ // DirectoryInfo di = Directory.CreateDirectory(path); //} //System.Diagnostics.Process.Start(path); #endregion #region 開路徑後選擇執行exe //OpenFileDialog openFileDialog = new OpenFileDialog(); //openFileDialog.InitialDirectory = path; //openFileDialog.Filter = "PDF (*.pdf)|*.pdf"; //if (openFileDialog.ShowDialog() == DialogResult.OK) //{ // string FilePath = openFileDialog.FileName; // Process.Start(FilePath); //} #endregion #region automatically open PDF teached by youtube System.Diagnostics.Process.Start(path+@"\"+pdfFileName); #endregion } catch (Exception ee) { MessageBox.Show(ee.Message); } }
private void CreatePDFFile() { // CREATE A DOCUMENT OBJECT var document = new Document(PageSize.A4, 10, 10, 10, 0); // CREATE A NEW PDFWRITE OBJECT, WRITING THE OUTPUT TO A MEMORYSTREAM var output = new MemoryStream(); var writer = PdfWriter.GetInstance(document, output); // OPEN THE DOCUMENT FOR WRITING PDFHeaderFooter PageEventHandler = new PDFHeaderFooter(); writer.PageEvent = PageEventHandler; document.Open(); // FIRST, CREATE OUR FONTS... (FOR MORE ON WORKING W/FONTS IN ITEXTSHARP, see: http://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD); titleFont.SetColor(201, 38, 0); var subTitleFont = FontFactory.GetFont("Arial", 12, Font.BOLD); subTitleFont.SetColor(1, 107, 165); var SubbodyFont = FontFactory.GetFont("Arial", 10, Font.NORMAL); var SubbodyFont1 = FontFactory.GetFont("Arial", 10, Font.BOLD); var boldTableFont = FontFactory.GetFont("Arial", 12, Font.BOLD); var endingMessageFont = FontFactory.GetFont("Arial", 10, Font.ITALIC); var bodyFont = FontFactory.GetFont("Arial", 12, Font.NORMAL); var anchorFont = FontFactory.GetFont("Arial", 10, Font.NORMAL); //--------------------------------------------------------------------- strSql = @"SELECT INSTA_MST_SOLUTION.SOLUTION_ID, INSTA_MST_SOLUTION.SOLUTION_NAME, INSTA_MST_SIZE.SIZE_DESCRIPTION, INSTA_MST_EXHIBIT_TYPE_DETAIL.EXHIBIT_DETAILS, INSTA_PAR_MOD_PORT_TYPE.MOD_PORT_DESCRIPTION, INSTA_MST_PRODUCT.PRODUCT_ID, INSTA_MST_PRODUCT.PRODUCT_NAME, INSTA_MST_PRODUCT.PRODUCT_DESCRIPTION, INSTA_MST_CURRENCY.CURRENCY_DESCRIPTION, INSTA_MST_CURRENCY.CURRENCY_SYMBOL, INSTA_MST_DISCOUNT.DISCOUNT_RATE, CAST(INSTA_MST_SOLUTION.PRICE AS VARCHAR(10)) AS PRICE, INSTA_MST_SOLUTION.SHIPPING_DAYS, INSTA_MST_SOLUTION.SHIPPING_DISPLAY_FLAG, INSTA_MST_SOLUTION.SOLUTION_TEXT, INSTA_MST_SOLUTION.PRODUCT_FEATURE_DISPLAY_FLAG FROM INSTA_MST_SOLUTION LEFT JOIN INSTA_MST_SIZE ON INSTA_MST_SOLUTION.SIZE_ID = INSTA_MST_SIZE.SIZE_ID LEFT JOIN INSTA_MST_EXHIBIT_TYPE_DETAIL ON INSTA_MST_SOLUTION.EXHIBIT_DETAIL_ID = INSTA_MST_EXHIBIT_TYPE_DETAIL.EXHIBIT_DETAIL_ID LEFT JOIN INSTA_PAR_MOD_PORT_TYPE ON INSTA_MST_SOLUTION.MOD_PORT_ID = INSTA_PAR_MOD_PORT_TYPE.MOD_PORT_ID LEFT JOIN INSTA_MST_PRODUCT ON INSTA_MST_SOLUTION.PRODUCT_ID = INSTA_MST_PRODUCT.PRODUCT_ID LEFT JOIN INSTA_MST_DISCOUNT ON INSTA_MST_SOLUTION.DISCOUNT_ID = INSTA_MST_DISCOUNT.DISCOUNT_ID LEFT JOIN INSTA_MST_CURRENCY ON INSTA_MST_SOLUTION.CURRENCY_ID = INSTA_MST_CURRENCY.CURRENCY_ID WHERE INSTA_MST_SOLUTION.SOLUTION_ID =" + Convert.ToString(ViewState["SOLUTION_ID"]); //------------------------------------------------------------------------------------------------------------ PdfContentByte cb = writer.DirectContent; var para = new Paragraph(); ColumnText ct = new iTextSharp.text.pdf.ColumnText(cb); float intYaxis = 150f; int intProductId = 0; int intProdFeatureFlag = 0; int intXaxis = 310; //------------------------------------------------------------------------------------------------------------ IDataReader reader = objHelper.gExecuteReader(CommandType.Text, strSql); while (reader.Read()) { intProductId = Convert.ToInt32(string.IsNullOrWhiteSpace(Convert.ToString(reader["PRODUCT_ID"])) ? "0" : Convert.ToString(reader["PRODUCT_ID"])); intProdFeatureFlag = Convert.ToInt32(string.IsNullOrWhiteSpace(Convert.ToString(reader["PRODUCT_FEATURE_DISPLAY_FLAG"])) ? "0" : Convert.ToString(reader["PRODUCT_FEATURE_DISPLAY_FLAG"])); //lblProductNamePdf.Text = lblProductName.Text = Convert.ToString(reader["PRODUCT_NAME"]); //lblHeadSolutionNamePdf.Text = lblHeadSolutionName.Text = ": " + Convert.ToString(reader["SOLUTION_NAME"]); //------------------------------------------------------------------------------------------------------------------------------------------------------- var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/edsLogo.JPG")); logo.SetAbsolutePosition(440, document.Top - 100f); logo.ScalePercent(65f, 65f); document.Add(logo); //------------------------------------------------------------------------------------------------------------------------------------------------------- PlaceText(writer.DirectContent, Convert.ToString(reader["PRODUCT_NAME"]) + ":" + Convert.ToString(reader["SOLUTION_NAME"]), titleFont, 316, 750, 40, 790, 14, Element.ALIGN_LEFT); PlaceText(writer.DirectContent, "Size:" + Convert.ToString(reader["SIZE_DESCRIPTION"]) + " (" + Convert.ToString(reader["EXHIBIT_DETAILS"]) + ")", bodyFont, 316, 750, 40, 775, 14, Element.ALIGN_LEFT); //------------------------------------------------------------------------------------------------------------------------------------------------------- var bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.BeginText(); cb.SetFontAndSize(bf, 9); cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "www.expodisplayservice.ae", document.PageSize.GetRight(157), document.PageSize.GetTop(123), 0); cb.EndText(); //------------------------------------------------------------------------------------------------------------------------------------------------------- cb.MoveTo(40, document.Top - 120f); cb.LineTo(550, document.Top - 120f); cb.Stroke(); //---------------------------------------------------- cb.MoveTo(40f, document.Top - 130f); cb.LineTo(550f, document.Top - 130f); cb.LineTo(550f, 100f); cb.LineTo(40f, 100f); cb.ClosePath(); cb.Stroke(); //-------------------------------------------------- logo = iTextSharp.text.Image.GetInstance(Server.MapPath(imgPreviewLarge.ImageUrl)); // logo.ScalePercent(20f, 20f); logo.ScaleAbsolute(240f, 245f); logo.SetAbsolutePosition(55, document.Top - 387f); logo.Border = Rectangle.BOX; // logo.BorderColor = System.Drawing.Color.Black; logo.BorderWidth = 1f; document.Add(logo); //----------------------------------------------------- //PRINT SOLUTION TEXT IF ANY if (!string.IsNullOrWhiteSpace(Convert.ToString(reader["SOLUTION_TEXT"]))) { ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); para.Add(new Chunk(Convert.ToString(reader["SOLUTION_TEXT"]), SubbodyFont)); ct.AddText(para); ct.Go(); intYaxis += 10; } //----------------------------------------------------- if (Convert.ToInt32(Convert.ToString(reader["SHIPPING_DISPLAY_FLAG"])) > 0) { para = new Paragraph(); ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); para.Add(new Chunk("Shipping :", subTitleFont)); ct.AddText(para); ct.Go(); //------------------------- intYaxis += 20; para = new Paragraph(); ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); para.Add(new Chunk("Minimum days required to ship : " + Convert.ToString(reader["SHIPPING_DAYS"]), SubbodyFont)); ct.AddText(para); ct.Go(); intYaxis += 10; } if (Convert.ToDouble(Convert.ToString(reader["PRICE"])) > 0) { para = new Paragraph(); ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); para.Add(new Chunk("Price : ", subTitleFont)); para.Add(new Chunk(Convert.ToString(reader["CURRENCY_SYMBOL"]) + " " + Convert.ToString(reader["PRICE"]), subTitleFont)); ct.AddText(para); ct.Go(); intYaxis += 10; } } reader.Close(); //----------------------------------------------------------------------- intYaxis += 5; para = new Paragraph(); ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 10); string strParaName = ""; strSql = @"SELECT INSTA_MST_PARAGRAPH_HEADER.PARAGRAPH_DESCRIPTION, INSTA_MST_SETUP.SOLUTION_DETAILS_BULLET_FLAG, INSTA_MST_SOLUTION_DETAILS.SOLUTION_DETAILS FROM INSTA_MST_SOLUTION_DETAILS, INSTA_MST_PARAGRAPH_HEADER, INSTA_MST_SETUP WHERE INSTA_MST_SOLUTION_DETAILS.PARAGRAPH_HEADER_ID = INSTA_MST_PARAGRAPH_HEADER.PARAGRAPH_HEADER_ID AND INSTA_MST_SOLUTION_DETAILS.SOLUTION_ID=" + Convert.ToString(ViewState["SOLUTION_ID"]) + @" ORDER BY INSTA_MST_PARAGRAPH_HEADER.SORT_ORDER, INSTA_MST_SOLUTION_DETAILS.SOLUTION_DETAIL_ID"; reader = objHelper.gExecuteReader(CommandType.Text, strSql); while (reader.Read()) { if (strParaName != Convert.ToString(reader["PARAGRAPH_DESCRIPTION"])) { // intYaxis += 5; para = new Paragraph(); // ct.SetSimpleColumn(285, document.Top - intYaxis, 500, 100); para.Add(new Chunk(Convert.ToString(reader["PARAGRAPH_DESCRIPTION"]) + ": ", SubbodyFont1)); ct.AddText(para); ct.Go(); // intYaxis += 10; } //if (Convert.ToInt32(Convert.ToString(reader["SOLUTION_DETAILS_BULLET_FLAG"])) > 0) //{ // strDynamicHTML += "<li style=\"font-family: Arial, Helvetica, sans-serif;font-size: 12px;\" >" + Convert.ToString(reader["SOLUTION_DETAILS"]) + "</li> "; //} //else //{ para = new Paragraph(); // ct.SetSimpleColumn(285, document.Top - intYaxis, 550, 600); para.Add(new Chunk(Convert.ToString(reader["SOLUTION_DETAILS"]), SubbodyFont)); ct.AddText(para); ct.Go(); // intYaxis += 10; //} strParaName = Convert.ToString(reader["PARAGRAPH_DESCRIPTION"]); } reader.Close(); //ADD PRODUCT FEATURES //------------------------------------------------------------------------ if (intProdFeatureFlag > 0) { divProductFeature.Visible = true; //--------------------------------------------------------- //intYaxis = 100; para = new Paragraph(); ct.SetSimpleColumn(55, 440, 500, 100); para.Add(new Chunk(lblProductName.Text + " Features", subTitleFont)); ct.AddText(para); ct.Go(); //--------------------------------------------------------- iTextSharp.text.List listFeature = new iTextSharp.text.List(List.UNORDERED, 10f); listFeature.SetListSymbol("\u2022"); listFeature.IndentationLeft = 1f; //--------------------------------------------------------- strSql = @"SELECT FEATURE_DESCRIPTION, SORT_ORDER, PRODUCT_FEATURE_BULLET_FLAG FROM INSTA_MST_PRODUCT_FEATURE, INSTA_MST_SETUP WHERE PRODUCT_ID = " + intProductId + @" ORDER BY SORT_ORDER "; reader = objHelper.gExecuteReader(CommandType.Text, strSql); while (reader.Read()) { if (Convert.ToInt32(Convert.ToString(reader["PRODUCT_FEATURE_BULLET_FLAG"])) > 0) { listFeature.Add(new iTextSharp.text.ListItem(Convert.ToString(reader["FEATURE_DESCRIPTION"]), SubbodyFont)); } else { listFeature.Add(new iTextSharp.text.ListItem(Convert.ToString(reader["FEATURE_DESCRIPTION"]), SubbodyFont)); } } reader.Close(); //------------------------------------ para = new Paragraph(); ct.SetSimpleColumn(55, 420, 260, 100); para.Add(listFeature); ct.AddElement(para); ct.Go(); int i = listFeature.Size; } //------------------------------------------------------------------------- document.Close(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", string.Format("attachment;filename=Receipt-{0}.pdf", lblProductName.Text + lblHeadSolutionName.Text)); Response.BinaryWrite(output.ToArray()); Response.End(); }
public void StartElement(String tag, Hashtable h) { if (!tagsSupported.ContainsKey(tag)) return; style.ApplyStyle(tag, h); String follow = (String)FactoryProperties.followTags[tag]; if (follow != null) { Hashtable prop = new Hashtable(); prop[follow] = null; cprops.AddToChain(follow, prop); return; } FactoryProperties.InsertStyle(h); if (tag.Equals("a")) { cprops.AddToChain(tag, h); if (currentParagraph == null) currentParagraph = new Paragraph(); stack.Push(currentParagraph); currentParagraph = new Paragraph(); return; } if (tag.Equals("br")) { if (currentParagraph == null) currentParagraph = new Paragraph(); currentParagraph.Add(factoryProperties.CreateChunk("\n", cprops)); return; } if (tag.Equals("font") || tag.Equals("span")) { cprops.AddToChain(tag, h); return; } if (tag.Equals("img")) { String src = (String)h["src"]; if (src == null) return; cprops.AddToChain(tag, h); Image img = null; if (interfaceProps != null) { Hashtable images = (Hashtable)interfaceProps["img_static"]; if (images != null) { Image tim = (Image)images[src]; if (tim != null) img = Image.GetInstance(tim); } else { if (!src.StartsWith("http")) { // relative src references only String baseurl = (String)interfaceProps["img_baseurl"]; if (baseurl != null) { src = baseurl + src; img = Image.GetInstance(src); } } } } if (img == null) { if (!src.StartsWith("http")) { String path = cprops["image_path"]; if (path == null) path = ""; src = Path.Combine(path, src); } img = Image.GetInstance(src); } String align = (String)h["align"]; String width = (String)h["width"]; String height = (String)h["height"]; String before = cprops["before"]; String after = cprops["after"]; if (before != null) img.SpacingBefore = float.Parse(before, System.Globalization.NumberFormatInfo.InvariantInfo); if (after != null) img.SpacingAfter = float.Parse(after, System.Globalization.NumberFormatInfo.InvariantInfo); float wp = LengthParse(width, (int)img.Width); float lp = LengthParse(height, (int)img.Height); if (wp > 0 && lp > 0) img.ScalePercent(wp > lp ? lp : wp); else if (wp > 0) img.ScalePercent(wp); else if (lp > 0) img.ScalePercent(lp); img.WidthPercentage = 0; if (align != null) { EndElement("p"); int ralign = Image.MIDDLE_ALIGN; if (Util.EqualsIgnoreCase(align, "left")) ralign = Image.LEFT_ALIGN; else if (Util.EqualsIgnoreCase(align, "right")) ralign = Image.RIGHT_ALIGN; img.Alignment = ralign; IImg i = null; bool skip = false; if (interfaceProps != null) { i = (IImg)interfaceProps["img_interface"]; if (i != null) skip = i.Process(img, h, cprops, document); } if (!skip) document.Add(img); cprops.RemoveChain(tag); } else { cprops.RemoveChain(tag); if (currentParagraph == null) currentParagraph = FactoryProperties.CreateParagraph(cprops); currentParagraph.Add(new Chunk(img, 0, 0)); } return; } EndElement("p"); if (tag.Equals("h1") || tag.Equals("h2") || tag.Equals("h3") || tag.Equals("h4") || tag.Equals("h5") || tag.Equals("h6")) { if (!h.ContainsKey("size")) { int v = 7 - int.Parse(tag.Substring(1)); h["size"] = v.ToString(); } cprops.AddToChain(tag, h); return; } if (tag.Equals("ul")) { if (pendingLI) EndElement("li"); skipText = true; cprops.AddToChain(tag, h); List list = new List(false, 10); list.SetListSymbol("\u2022"); stack.Push(list); return; } if (tag.Equals("ol")) { if (pendingLI) EndElement("li"); skipText = true; cprops.AddToChain(tag, h); List list = new List(true, 10); stack.Push(list); return; } if (tag.Equals("li")) { if (pendingLI) EndElement("li"); skipText = false; pendingLI = true; cprops.AddToChain(tag, h); stack.Push(FactoryProperties.CreateListItem(cprops)); return; } if (tag.Equals("div") || tag.Equals("body")) { cprops.AddToChain(tag, h); return; } if (tag.Equals("pre")) { if (!h.ContainsKey("face")) { h["face"] = "Courier"; } cprops.AddToChain(tag, h); isPRE = true; return; } if (tag.Equals("p")) { cprops.AddToChain(tag, h); currentParagraph = FactoryProperties.CreateParagraph(h); return; } if (tag.Equals("tr")) { if (pendingTR) EndElement("tr"); skipText = true; pendingTR = true; cprops.AddToChain("tr", h); return; } if (tag.Equals("td") || tag.Equals("th")) { if (pendingTD) EndElement(tag); skipText = false; pendingTD = true; cprops.AddToChain("td", h); stack.Push(new IncCell(tag, cprops)); return; } if (tag.Equals("table")) { cprops.AddToChain("table", h); IncTable table = new IncTable(h); stack.Push(table); tableState.Push(new bool[]{pendingTR, pendingTD}); pendingTR = pendingTD = false; skipText = true; return; } }
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); } }
public static void CreatePDF(string strSolID) { string strSql = ""; string strProductName = ""; string strSolName = ""; string strImageURL = ""; DBHelper objHelper = new DBHelper(); using (MemoryStream output = new MemoryStream()) { using (Document document = new Document(PageSize.A4, 10, 10, 10, 0)) { using (PdfWriter writer = PdfWriter.GetInstance(document, output)) { // CREATE A DOCUMENT OBJECT // var document = new Document(PageSize.A4, 10, 10, 10, 0); // CREATE A NEW PDFWRITE OBJECT, WRITING THE OUTPUT TO A MEMORYSTREAM // var output = new MemoryStream(); // var writer = PdfWriter.GetInstance(document, output); // OPEN THE DOCUMENT FOR WRITING //THIS CLASS IS FOR FOOTER PDFHeaderFooter PageEventHandler = new PDFHeaderFooter(); writer.PageEvent = PageEventHandler; document.Open(); // FIRST, CREATE OUR FONTS... (FOR MORE ON WORKING W/FONTS IN ITEXTSHARP, see: http://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD); titleFont.SetColor(202, 5, 17); var subTitleFont = FontFactory.GetFont("Arial", 12, Font.BOLD); //subTitleFont.SetColor(161, 38, 101); subTitleFont.SetColor(202, 5, 17); var SubbodyFont = FontFactory.GetFont("Arial", 10, Font.NORMAL); var SubbodyFont1 = FontFactory.GetFont("Arial", 10, Font.BOLD); var boldTableFont = FontFactory.GetFont("Arial", 12, Font.BOLD); var endingMessageFont = FontFactory.GetFont("Arial", 10, Font.ITALIC); var bodyFont = FontFactory.GetFont("Arial", 12, Font.NORMAL); var anchorFont = FontFactory.GetFont("Arial", 10, Font.NORMAL); //--------------------------------------------------------------------- strSql = @"SELECT INSTA_MST_SOLUTION.SOLUTION_ID, INSTA_MST_SOLUTION.SOLUTION_NAME, INSTA_MST_SIZE.SIZE_DESCRIPTION, INSTA_MST_EXHIBIT_TYPE_DETAIL.EXHIBIT_DETAILS, INSTA_PAR_MOD_PORT_TYPE.MOD_PORT_DESCRIPTION, INSTA_MST_PRODUCT.PRODUCT_ID, INSTA_MST_PRODUCT.PRODUCT_NAME, INSTA_MST_PRODUCT.PRODUCT_DESCRIPTION, INSTA_MST_CURRENCY.CURRENCY_DESCRIPTION, INSTA_MST_CURRENCY.CURRENCY_SYMBOL, INSTA_MST_DISCOUNT.DISCOUNT_RATE, CAST(INSTA_MST_SOLUTION.PRICE AS VARCHAR(10)) AS PRICE, INSTA_MST_SOLUTION.SHIPPING_DAYS, INSTA_MST_SOLUTION.SHIPPING_DISPLAY_FLAG, INSTA_MST_SOLUTION.SOLUTION_TEXT, INSTA_MST_SOLUTION.PRODUCT_FEATURE_DISPLAY_FLAG, (SELECT TOP 1 '~/upload/Images/' + PICTURE_NAME FROM INSTA_MST_PICTURE_LINK WHERE SOLUTION_ID =INSTA_MST_SOLUTION.SOLUTION_ID AND INACTIVE_FLAG = 0 ORDER BY SORT_ORDER ) AS PICS_URL FROM INSTA_MST_SOLUTION LEFT JOIN INSTA_MST_SIZE ON INSTA_MST_SOLUTION.SIZE_ID = INSTA_MST_SIZE.SIZE_ID LEFT JOIN INSTA_MST_EXHIBIT_TYPE_DETAIL ON INSTA_MST_SOLUTION.EXHIBIT_DETAIL_ID = INSTA_MST_EXHIBIT_TYPE_DETAIL.EXHIBIT_DETAIL_ID LEFT JOIN INSTA_PAR_MOD_PORT_TYPE ON INSTA_MST_SOLUTION.MOD_PORT_ID = INSTA_PAR_MOD_PORT_TYPE.MOD_PORT_ID LEFT JOIN INSTA_MST_PRODUCT ON INSTA_MST_SOLUTION.PRODUCT_ID = INSTA_MST_PRODUCT.PRODUCT_ID LEFT JOIN INSTA_MST_DISCOUNT ON INSTA_MST_SOLUTION.DISCOUNT_ID = INSTA_MST_DISCOUNT.DISCOUNT_ID LEFT JOIN INSTA_MST_CURRENCY ON INSTA_MST_SOLUTION.CURRENCY_ID = INSTA_MST_CURRENCY.CURRENCY_ID WHERE INSTA_MST_SOLUTION.SOLUTION_ID =" + strSolID; //------------------------------------------------------------------------------------------------------------ PdfContentByte cb = writer.DirectContent; var para = new Paragraph(); ColumnText ct = new iTextSharp.text.pdf.ColumnText(cb); float intYaxis = 150f; int intProductId = 0; int intProdFeatureFlag = 0; int intXaxis = 310; int intShippingDisflg = 0; int intShippingDays = 0; string strCurrSymbol = ""; string strPrice = ""; //------------------------------------------------------------------------------------------------------------ IDataReader reader = objHelper.gExecuteReader(CommandType.Text, strSql); while (reader.Read()) { strProductName = Convert.ToString(reader["PRODUCT_NAME"]); strSolName = Convert.ToString(reader["SOLUTION_NAME"]); strImageURL = Convert.ToString(reader["PICS_URL"]); intProductId = Convert.ToInt32(string.IsNullOrWhiteSpace(Convert.ToString(reader["PRODUCT_ID"])) ? "0" : Convert.ToString(reader["PRODUCT_ID"])); intProdFeatureFlag = Convert.ToInt32(string.IsNullOrWhiteSpace(Convert.ToString(reader["PRODUCT_FEATURE_DISPLAY_FLAG"])) ? "0" : Convert.ToString(reader["PRODUCT_FEATURE_DISPLAY_FLAG"])); //lblProductNamePdf.Text = lblProductName.Text = Convert.ToString(reader["PRODUCT_NAME"]); //lblHeadSolutionNamePdf.Text = lblHeadSolutionName.Text = ": " + Convert.ToString(reader["SOLUTION_NAME"]); //------------------------------------------------------------------------------------------------------------------------------------------------------- //DISPLAY LOGO var logo = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/images/Insta-logo.png")); logo.SetAbsolutePosition(445, document.Top - 100f); logo.ScalePercent(65f, 65f); document.Add(logo); //------------------------------------------------------------------------------------------------------------------------------------------------------- PlaceText(writer.DirectContent, Convert.ToString(reader["PRODUCT_NAME"]) + ": " + Convert.ToString(reader["SOLUTION_NAME"]), titleFont, 40, 790, 580, 317, 15, Element.ALIGN_LEFT); PlaceText(writer.DirectContent, "Size: " + Convert.ToString(reader["SIZE_DESCRIPTION"]) + " (" + Convert.ToString(reader["EXHIBIT_DETAILS"]).Replace("<sup>","").Replace("</sup>","") + ")", bodyFont, 40, 775, 580, 317, 15, Element.ALIGN_LEFT); //PlaceText(writer.DirectContent, Convert.ToString(reader["PRODUCT_NAME"]) + ":" + Convert.ToString(reader["SOLUTION_NAME"]), titleFont, 316, 750, 40, 790, 14, Element.ALIGN_LEFT); //PlaceText(writer.DirectContent, "Size:" + Convert.ToString(reader["SIZE_DESCRIPTION"]) + " (" + Convert.ToString(reader["EXHIBIT_DETAILS"]) + ")", bodyFont, 316, 750, 40, 775, 14, Element.ALIGN_LEFT); //------------------------------------------------------------------------------------------------------------------------------------------------------- var bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.BeginText(); cb.SetFontAndSize(FontFactory.GetFont(FontFactory.HELVETICA_BOLD).BaseFont, 8); cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "www.insta-group.com", document.PageSize.GetRight(152), document.PageSize.GetTop(123), 0); cb.EndText(); //------------------------------------------------------------------------------------------------------------------------------------------------------- //FOR HORIZONTAL LINE BELOW PRODUCT & LOGO IMAGE cb.MoveTo(40, document.Top - 120f); cb.LineTo(550, document.Top - 120f); cb.Stroke(); ////---------------------------------------------------- //FOR RECTANGLE BORDER OVER CONTENT //cb.MoveTo(40f, document.Top - 130f); //cb.LineTo(550f, document.Top - 130f); //cb.LineTo(550f, 100f); //cb.LineTo(40f, 100f); //cb.ClosePath(); //cb.Stroke(); //-------------------------------------------------- logo = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(strImageURL)); // logo.ScalePercent(20f, 20f); logo.ScaleAbsolute(240f, 245f); logo.SetAbsolutePosition(40, document.Top - 387f); logo.Border = Rectangle.BOX; // logo.BorderColor = System.Drawing.Color.Black; logo.BorderWidth = 1f; document.Add(logo); //----------------------------------------------------- //PRINT SOLUTION TEXT IF ANY if (!string.IsNullOrWhiteSpace(Convert.ToString(reader["SOLUTION_TEXT"]))) { ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); para.Add(new Chunk(Convert.ToString(reader["SOLUTION_TEXT"]), SubbodyFont)); ct.AddText(para); ct.Go(); intYaxis += 10; } //----------------------------------------------------- intShippingDisflg = Convert.ToInt32(Convert.ToString(reader["SHIPPING_DISPLAY_FLAG"])); intShippingDays = Convert.ToInt32(Convert.ToString(reader["SHIPPING_DAYS"])); strCurrSymbol = Convert.ToString(reader["CURRENCY_SYMBOL"]); strPrice = Convert.ToString(reader["PRICE"]); //if (Convert.ToInt32(Convert.ToString(reader["SHIPPING_DISPLAY_FLAG"])) > 0) //{ // para = new Paragraph(); // ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); // para.Add(new Chunk("Shipping :", subTitleFont)); // ct.AddText(para); // ct.Go(); // //------------------------- // intYaxis += 20; // para = new Paragraph(); // ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); // para.Add(new Chunk("Minimum days required to ship : " + Convert.ToString(reader["SHIPPING_DAYS"]), SubbodyFont)); // ct.AddText(para); // ct.Go(); // intYaxis += 10; //} //if (Convert.ToDouble(Convert.ToString(reader["PRICE"])) > 0) //{ // para = new Paragraph(); // ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); // para.Add(new Chunk("Price : ", subTitleFont)); // para.Add(new Chunk(Convert.ToString(reader["CURRENCY_SYMBOL"]) + " " + Convert.ToString(reader["PRICE"]), subTitleFont)); // ct.AddText(para); // ct.Go(); // intYaxis += 10; //} } reader.Close(); //----------------------------------------------------------------------- //intYaxis += 15; string strParaName = ""; strSql = @"SELECT INSTA_MST_PARAGRAPH_HEADER.PARAGRAPH_DESCRIPTION, INSTA_MST_SETUP.SOLUTION_DETAILS_BULLET_FLAG, INSTA_MST_SOLUTION_DETAILS.SOLUTION_DETAILS FROM INSTA_MST_SOLUTION_DETAILS, INSTA_MST_PARAGRAPH_HEADER, INSTA_MST_SETUP WHERE INSTA_MST_SOLUTION_DETAILS.PARAGRAPH_HEADER_ID = INSTA_MST_PARAGRAPH_HEADER.PARAGRAPH_HEADER_ID AND INSTA_MST_SOLUTION_DETAILS.SOLUTION_ID=" + strSolID + @" ORDER BY INSTA_MST_PARAGRAPH_HEADER.SORT_ORDER, INSTA_MST_SOLUTION_DETAILS.SOLUTION_DETAIL_ID"; bool bnlSpecifican = false; reader = objHelper.gExecuteReader(CommandType.Text, strSql); while (reader.Read()) { if (!bnlSpecifican) { para = new Paragraph(); ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); para.Add(new Chunk("Specification : ", subTitleFont)); ct.AddText(para); ct.Go(); intYaxis += 20; bnlSpecifican = true; } if (strParaName != Convert.ToString(reader["PARAGRAPH_DESCRIPTION"])) { para = new Paragraph(); para.Add(new Chunk(Convert.ToString(reader["PARAGRAPH_DESCRIPTION"]) + ": ", SubbodyFont1)); para.Leading = 0; para.MultipliedLeading = 2f; ct.AddElement(new Phrase(para)); //ct.AddElement(Chunk.NEWLINE); // ct.AddElement(para); ct.Go(); } para = new Paragraph(); para.Add(new Chunk(Convert.ToString(reader["SOLUTION_DETAILS"]), SubbodyFont)); // para.SpacingAfter = 0; para.Leading = 0; para.MultipliedLeading = 1f; // ct.AddElement(new Phrase(para)); ct.AddElement(new Phrase(para)); //ct.AddText(Chunk.NEWLINE); //ct.AddText(para); ct.Go(); strParaName = Convert.ToString(reader["PARAGRAPH_DESCRIPTION"]); } reader.Close(); //----------------------------------------------------------------------------- //-- ADDING SHIPPING DETAILS //----------------------------------------------------------------------------- intYaxis = ct.YLine -30 ; if (intShippingDisflg > 0 && intShippingDays > 0) { para = new Paragraph(); ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); //ct.SetSimpleColumn(intXaxis, ct.YLine+50, 550, 100); para.Add(new Chunk("Shipping :", subTitleFont)); ct.AddElement(para); ct.Go(); //------------------------- intYaxis += 10; para = new Paragraph(); ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); para.Add(new Chunk("Minimum days required to ship : " + intShippingDays, SubbodyFont)); ct.AddElement(para); ct.Go(); } if (Convert.ToDouble(strPrice) > 0) { intYaxis += 15; para = new Paragraph(); ct.SetSimpleColumn(intXaxis, document.Top - intYaxis, 550, 100); para.Add(new Chunk("Price : ", SubbodyFont)); para.Add(new Chunk(strCurrSymbol + " " + strPrice, SubbodyFont)); ct.AddElement(para); ct.Go(); } //ADD PRODUCT FEATURES //------------------------------------------------------------------------ if (intProdFeatureFlag > 0) { //--------------------------------------------------------- //intYaxis = 100; para = new Paragraph(); ct.SetSimpleColumn(40, 440, 500, 100); para.Add(new Chunk(strProductName + " Features", subTitleFont)); ct.AddElement(para); ct.Go(); //--------------------------------------------------------- iTextSharp.text.List listFeature = new iTextSharp.text.List(List.UNORDERED, 10f); listFeature.SetListSymbol("\u2022"); listFeature.IndentationLeft = 1f; //--------------------------------------------------------- strSql = @"SELECT FEATURE_DESCRIPTION, SORT_ORDER, PRODUCT_FEATURE_BULLET_FLAG FROM INSTA_MST_PRODUCT_FEATURE, INSTA_MST_SETUP WHERE PRODUCT_ID = " + intProductId + @" ORDER BY SORT_ORDER "; reader = objHelper.gExecuteReader(CommandType.Text, strSql); while (reader.Read()) { if (Convert.ToInt32(Convert.ToString(reader["PRODUCT_FEATURE_BULLET_FLAG"])) > 0) { listFeature.Add(new iTextSharp.text.ListItem(Convert.ToString(reader["FEATURE_DESCRIPTION"]), SubbodyFont)); } else { listFeature.Add(new iTextSharp.text.ListItem(Convert.ToString(reader["FEATURE_DESCRIPTION"]), SubbodyFont)); } } reader.Close(); //------------------------------------ para = new Paragraph(); ct.SetSimpleColumn(40, 420, 260, 100); para.Add(listFeature); ct.AddElement(para); ct.Go(); int i = listFeature.Size; } //------------------------------------------------------------------------- document.Close(); HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf", strProductName + strSolName)); HttpContext.Current.Response.BinaryWrite(output.ToArray()); HttpContext.Current.Response.End(); } } } }
public bool Convert(Inspection inspection) { var sf = new SaveFileDialog { FileName = $"{inspection.Checklist.Name}.pdf", Filter = @"Pdf Files|*.pdf" }; if (sf.ShowDialog() != DialogResult.OK) { return(false); } // Now here's our save folder var savePath = sf.FileName + (sf.FileName.EndsWith(".pdf") ? "" : ".pdf"); const it.Font.FontFamily font = it.Font.FontFamily.TIMES_ROMAN; var header = new it.Font(font, 22f); var title = new it.Font(font, 18f); var subtitle = new it.Font(font, 13f); var paragraph = new it.Font(font, 9f); var templateDocument = new it.Document(it.PageSize.A4); var pdfWriter = PdfWriter.GetInstance(templateDocument, new FileStream(savePath, FileMode.Create)); pdfWriter.PageEvent = new ITextEvents(); templateDocument.Open(); var name = new it.Paragraph(inspection.Checklist.Name, header); var datetime = new it.Paragraph(inspection.Checklist.DateTimeCreated.ToString(), subtitle); name.Alignment = it.Element.ALIGN_CENTER; datetime.Alignment = it.Element.ALIGN_CENTER; templateDocument.Add(name); templateDocument.Add(datetime); templateDocument.Add(new it.Paragraph("Vragen:", header)); //Load questions and convert to pdf format. //Assemble list of questions var questions = inspection.Answers.Select(a => a.Question).Distinct(); foreach (var question in questions) { templateDocument.Add(new it.Paragraph(question.Text, title)); templateDocument.Add(new it.Paragraph(question.QuestionType.Name, subtitle)); if (!IncludeAnswers) { continue; } //Place answers in an unordered(bullet point) list below the question title var list = new it.List(it.List.UNORDERED); list.SetListSymbol("\u2022"); //Set type to bulletpoint list.IndentationLeft = 30f; //Neatly indent it foreach (var answer in question.Answers) { list.Add(new it.ListItem($"{question.AnswerPrefix} {answer.Text} {question.AnswerSuffix}", paragraph)); } templateDocument.Add(list); } templateDocument.Close(); return(true); }
/// <summary> /// Add a page that includes a bullet list. /// </summary> /// <param name="doc"></param> private void AddPageWithBulletList(iTextSharp.text.Document doc) { // Add a new page to the document doc.NewPage(); // The header at the top of the page is an anchor linked to by the table of contents. iTextSharp.text.Anchor contentsAnchor = new iTextSharp.text.Anchor("RESEARCH\n\n", _largeFont); contentsAnchor.Name = "research"; // Add the header anchor to the page this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, contentsAnchor); // Create an unordered bullet list. The 10f argument separates the bullet from the text by 10 points iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); // Set the bullet symbol (without this a hypen starts each list item) list.IndentationLeft = 20f; // Indent the list 20 points list.Add(new ListItem("Lift, thrust, drag, and gravity are the four forces that act on a plane.", _standardFont)); list.Add(new ListItem("A plane should be light to help fight against gravity's pull to the ground.", _standardFont)); list.Add(new ListItem("Gravity will have less effect on a plane built from the lightest materials available.", _standardFont)); list.Add(new ListItem("In order to fly well, airplanes must be stable.", _standardFont)); list.Add(new ListItem("A plane that is unstable will either pitch up into a stall, or nose-dive.", _standardFont)); doc.Add(list); // Add the list to the page // Add some white space and another heading this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("\n\n\n")); this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("HYPOTHESIS\n\n")); // Add some final text to the page this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_LEFT, _standardFont, new Chunk("Given five paper airplanes made out of newspaper, printer paper, construction paper, paper towel, and posterboard, the airplane made out of printer paper will fly the furthest.")); }
private void AddTeam(Hold team, Document doc, bool addSeparator) { iTextSharp.text.Font header = FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.Black); iTextSharp.text.Font boldText = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.BOLD, BaseColor.Black); iTextSharp.text.Font descFont = FontFactory.GetFont("Verdana", 12, iTextSharp.text.Font.NORMAL, BaseColor.DarkGray); iTextSharp.text.Font text = FontFactory.GetFont("Verdana", 12, iTextSharp.text.Font.NORMAL, BaseColor.Black); iTextSharp.text.Font textRed = FontFactory.GetFont("Verdana", 12, iTextSharp.text.Font.NORMAL, BaseColor.Red); iTextSharp.text.Image img = null; if (string.IsNullOrEmpty(team.Image) == false) { string imgPath = Path.Combine(Directory.GetCurrentDirectory(), "images/" + team.Image); if (File.Exists(imgPath)) { img = iTextSharp.text.Image.GetInstance(imgPath); img.ScaleToFit(120f, 120f); img.Alignment = iTextSharp.text.Image.TEXTWRAP | iTextSharp.text.Image.ALIGN_RIGHT; img.IndentationLeft = 9f; img.SpacingAfter = 9f; img.BorderWidthTop = 16f; img.BorderColorTop = iTextSharp.text.BaseColor.White; } else { Console.WriteLine("Kan ikke finde billedet " + team.Image); } } Paragraph p = new Paragraph(); p.Alignment = Element.ALIGN_JUSTIFIED; Chunk nameChunk = new Chunk(team.Name, header); Chunk ageChunk = new Chunk(" for " + team.Age + "\n\n", text); p.Add(nameChunk); p.Add(ageChunk); iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f); list.SetListSymbol("\u2022"); list.IndentationLeft = 20f; list.Add("Ansvarlig Instruktør : " + team.Responsible); list.Add("Yderligere Instruktør(er) : " + team.Assistent); list.Add("Tidspunkt : " + team.WeekDay + " " + team.Time + " i " + team.Place); list.Add("Deltagere : " + team.Min + " til " + team.Max); list.Add("Pris : " + team.Price); list.Add("Opstart : " + team.StartDate); p.Add(list); Paragraph p1 = new Paragraph(); Chunk descChunk = new Chunk("\n\n" + team.Description + "\n\n", descFont); p1.Add(descChunk); if (string.IsNullOrWhiteSpace(team.HalfSeason) == false && team.HalfSeason.ToLowerInvariant() != "nej") { Chunk halfSeason = new Chunk("Bemærk", boldText); Chunk halfSeason2 = new Chunk(" Dette hold udbydes kun for en halv sæson og løber indtil " + team.HalfSeason, text); p1.Add(halfSeason); p1.Add(halfSeason2); } if (string.IsNullOrWhiteSpace(team.Status)) { Chunk extraInfo = new Chunk("\nHoldet er endnu ikke helt på plads ( " + team.Comments + ")", textRed); p1.Add(extraInfo); } if (addSeparator) { p1.Add(new Chunk("\n\n")); DottedLineSeparator dottedline = new DottedLineSeparator(); dottedline.Offset = 0; dottedline.Gap = 2f; p1.Add(dottedline); p1.Add(new Chunk("\n\n")); } if (img != null) { doc.Add(img); } doc.Add(p); doc.Add(p1); }
public List CreateList(String tag, ChainedProperties chain) { List list; if (Util.EqualsIgnoreCase(HtmlTags.UL, tag)) { list = new List(List.UNORDERED); list.SetListSymbol("\u2022 "); } else { list = new List(List.ORDERED); } try{ list.IndentationLeft = float.Parse(chain[HtmlTags.INDENT], CultureInfo.InvariantCulture); }catch { list.Autoindent = true; } return list; }
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 float AddList(string[] chunks, iTextSharp.text.Font font, float spacingBefore, float spacingAfter, float spacingBetween, int alignment, float lineHightMultiplier, string listSymbol, int padding, float indentationLeft, Document doc) { int count = 0; float height = 0; float fontSize = font.Size * lineHightMultiplier; var paragraph = new Paragraph(); var list = new List(List.UNORDERED); list.SetListSymbol(listSymbol.PadRight(padding)); list.IndentationLeft = indentationLeft; foreach (string item in chunks) { if (string.IsNullOrEmpty(item)) continue; var chunk = new Chunk(item, font); chunk.setLineHeight(font.Size * lineHightMultiplier); var listItem = new ListItem(chunk); listItem.SpacingBefore = spacingBetween; list.Add(listItem); count++; } if (count > 0) { paragraph.Add(list); paragraph.Alignment = alignment; paragraph.SpacingBefore = spacingBefore; paragraph.SpacingAfter = spacingAfter; doc.Add(paragraph); height = spacingBefore + spacingAfter + count * (fontSize + spacingBetween) - spacingBetween; } return height; }
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); } }