public virtual void CreatePdf(String dest) { PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); Document document = new Document(pdf); SolidLine line = new SolidLine(1f); line.SetColor(ColorConstants.RED); LineSeparator ls = new LineSeparator(line); ls.SetWidth(UnitValue.CreatePercentValue(50)); ls.SetMarginTop(5); IList <IList <String> > resultSet = CsvTo2DList.Convert(SRC, "|"); resultSet.RemoveAt(0); foreach (IList <String> record in resultSet) { String url = String.Format("http://www.imdb.com/title/tt{0}", record[0]); Link movie = new Link(record[2], PdfAction.CreateURI(url)); Div div = new Div().SetKeepTogether(true).SetBorderLeft(new SolidBorder(2)).SetPaddingLeft(3).SetMarginBottom (10).Add(new Paragraph(movie.SetFontSize(14f))).Add(new Paragraph(String.Format("Directed by {0} ({1}, {2})" , record[3], record[4], record[1]))); FileInfo file = new FileInfo(String.Format("../../../resources/img/{0}.jpg", record[0])); if (file.Exists) { iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(file.FullName)); img.ScaleToFit(10000, 120); div.Add(img); } div.Add(ls); document.Add(div); } document.Close(); }
public virtual void LineSeparatorWidthPercentageTest01() { String outFileName = destinationFolder + "lineSeparatorWidthPercentageTest01.pdf"; String cmpFileName = sourceFolder + "cmp_lineSeparatorWidthPercentageTest01.pdf"; PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName)); Document document = new Document(pdf); ILineDrawer line1 = new SolidLine(); line1.SetColor(ColorConstants.RED); ILineDrawer line2 = new SolidLine(); document.Add(new LineSeparator(line1).SetWidth(50).SetMarginBottom(10)); document.Add(new LineSeparator(line2).SetWidth(UnitValue.CreatePercentValue(50))); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
private void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document doc = new Document(pdfDoc, new PageSize(595, 842)); doc.SetMargins(55, 15, 35, 15); ILineDrawer line = new SolidLine(2); line.SetColor(ColorConstants.LIGHT_GRAY); LineSeparator tableEndSeparator = new LineSeparator(line); tableEndSeparator.SetMarginTop(10); String[] header = { "Header1", "Header2", "Header3", "Header4", "Header5" }; String[] content = { "column 1", "column 2", "some Text in column 3", "Test data ", "column 5" }; Table table = new Table( UnitValue.CreatePercentArray(new float[] { 3, 2, 4, 3, 2 })).UseAllAvailableWidth(); foreach (String columnHeader in header) { Paragraph headerParagraph = new Paragraph(columnHeader) .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD)) .SetFontSize(10); Cell headerCell = new Cell() .Add(headerParagraph) .SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1)) .SetPadding(8); table.AddHeaderCell(headerCell); } for (int i = 0; i < 15; i++) { int j = 0; foreach (String text in content) { Paragraph paragraph = new Paragraph((i == 13 && j == 3) ? "Test data \n Test data \n Test data" : text) .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA)) .SetFontSize(10); Cell cell = new Cell() .Add(paragraph) .SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1)) .SetPaddingLeft(5) .SetPaddingTop(5) .SetPaddingRight(5) .SetPaddingBottom(5); table.AddCell(cell); j++; } } doc.Add(table); doc.Add(tableEndSeparator); for (int k = 0; k < 5; k++) { Paragraph info = new Paragraph("Some title") .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA)) .SetFontSize(10) .SetMarginTop(12); doc.Add(info); table = new Table( UnitValue.CreatePercentArray(new float[] { 3, 2, 4, 3, 2 })).UseAllAvailableWidth(); table.SetMarginTop(15); foreach (String columnHeader in header) { Paragraph paragraph = new Paragraph(columnHeader) .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD)) .SetFontSize(10); Cell headerCell = new Cell() .Add(paragraph) .SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1)) .SetPaddingLeft(8) .SetPaddingTop(8) .SetPaddingRight(8) .SetPaddingBottom(8); table.AddHeaderCell(headerCell); } foreach (String text in content) { Paragraph paragraph = new Paragraph(text) .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA)) .SetFontSize(10); Cell cell = new Cell() .Add(paragraph) .SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1)) .SetPaddingLeft(5) .SetPaddingTop(5) .SetPaddingRight(5) .SetPaddingBottom(5); table.AddCell(cell); } doc.Add(table); doc.Add(tableEndSeparator); } doc.Close(); }