public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Instantiate Document instance by calling empty constructor Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(); // Create a page in the pdf document Aspose.Pdf.Page page = pdfDocument.Pages.Add(); // Create a Header Section of the PDF file Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter(); // Set the Odd Header for the PDF file page.Header = header; // Set the top margin for the header section header.Margin.Top = 20; // Instantiate a table object Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table(); // Add the table in paragraphs collection of the desired section header.Paragraphs.Add(tab1); // Set default cell border using BorderInfo object tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); // Set with column widths of the table tab1.ColumnWidths = "60 300"; Aspose.Pdf.Image img = new Aspose.Pdf.Image(); img.File = dataDir + "aspose-logo.jpg"; // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = tab1.Rows.Add(); row1.Cells.Add("Table in Header Section"); row1.BackgroundColor = Color.Gray; // Set the row span value for first row as 2 tab1.Rows[0].Cells[0].ColSpan = 2; tab1.Rows[0].Cells[0].DefaultCellTextState.ForegroundColor = Color.Cyan; tab1.Rows[0].Cells[0].DefaultCellTextState.Font = FontRepository.FindFont("Helvetica"); // Create rows in the table and then cells in the rows Aspose.Pdf.Row row2 = tab1.Rows.Add(); // Set the background color for Row2 row2.BackgroundColor = Color.White; // Add the cell which holds the image Aspose.Pdf.Cell cell2 = row2.Cells.Add(); // Set the image width to 60 img.FixWidth = 60; // Add the image to the table cell cell2.Paragraphs.Add(img); row2.Cells.Add("Logo is looking fine !"); row2.Cells[1].DefaultCellTextState.Font = FontRepository.FindFont("Helvetica"); // Set the vertical allignment of the text as center alligned row2.Cells[1].VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center; row2.Cells[1].Alignment = Aspose.Pdf.HorizontalAlignment.Center; // Save the Pdf file pdfDocument.Save(dataDir + "TableInHeaderFooterSection_out.pdf"); // ExEnd:1 }
public static void Run() { // ExStart:AddImageinATableCell // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); // Instantiate a Document object Document pdfDocument = new Document(); // Create a page in the pdf document Page sec1 = pdfDocument.Pages.Add(); // Instantiate a table object Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table(); // Add the table in paragraphs collection of the desired page sec1.Paragraphs.Add(tab1); // Set default cell border using BorderInfo object tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); // Set with column widths of the table tab1.ColumnWidths = "100 100 120"; Aspose.Pdf.Image img = new Aspose.Pdf.Image(); img.File = dataDir + "aspose.jpg"; // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = tab1.Rows.Add(); row1.Cells.Add("Sample text in cell"); // Add the cell which holds the image Aspose.Pdf.Cell cell2 = row1.Cells.Add(); // Add the image to the table cell cell2.Paragraphs.Add(img); row1.Cells.Add("Previous cell with image"); row1.Cells[2].VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center; // Save the Document pdfDocument.Save(dataDir + "AddImageInTableCell_out.pdf"); // ExEnd:AddImageinATableCell }
public static void Run() { //ExStart: TextAlignmentForTableRowContent var dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); // Create PDF document Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Initializes a new instance of the Table Aspose.Pdf.Table table = new Aspose.Pdf.Table(); // Set the table border color as LightGray table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); // set the border for table cells table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); // create a loop to add 10 rows for (int row_count = 0; row_count < 10; row_count++) { // add row to table Aspose.Pdf.Row row = table.Rows.Add(); row.VerticalAlignment = VerticalAlignment.Center; row.Cells.Add("Column (" + row_count + ", 1)" + DateTime.Now.Ticks); row.Cells.Add("Column (" + row_count + ", 2)"); row.Cells.Add("Column (" + row_count + ", 3)"); } Page tocPage = doc.Pages.Add(); // Add table object to first page of input document tocPage.Paragraphs.Add(table); // Save updated document containing table object doc.Save(dataDir + "43620_ByWords_out.pdf"); //ExEnd: TextAlignmentForTableRowContent }
public static void ImportGroupedData <TKey, TValue>(this Aspose.Pdf.Table table, IEnumerable <GroupViewModel <TKey, TValue> > groupedData) { var headRow = table.Rows.Add(); var props = typeof(TValue).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in props) { headRow.Cells.Add(prop.GetCustomAttribute(typeof(DisplayAttribute)) is DisplayAttribute dd ? dd.Name : prop.Name); } foreach (var group in groupedData) { // Add group row to table var row = table.Rows.Add(); var cell = row.Cells.Add(group.Key.ToString()); cell.ColSpan = props.Length; cell.BackgroundColor = Aspose.Pdf.Color.DarkGray; cell.DefaultCellTextState.ForegroundColor = Aspose.Pdf.Color.White; foreach (var item in group.Values) { // Add data row to table var dataRow = table.Rows.Add(); // Add cells foreach (var t in props) { var dataItem = t.GetValue(item, null); if (t.GetCustomAttribute(typeof(DataTypeAttribute)) is DataTypeAttribute dataType) { switch (dataType.DataType) { case DataType.Currency: dataRow.Cells.Add(string.Format("{0:C}", dataItem)); break; case DataType.Date: var dateTime = (DateTime)dataItem; if (t.GetCustomAttribute(typeof(DisplayFormatAttribute)) is DisplayFormatAttribute df) { dataRow.Cells.Add(string.IsNullOrEmpty(df.DataFormatString) ? dateTime.ToShortDateString() : string.Format(df.DataFormatString, dateTime)); } break; default: dataRow.Cells.Add(dataItem.ToString()); break; } } else { dataRow.Cells.Add(dataItem.ToString()); } } } } }
public static void ImportEntityList <TSource>(this Aspose.Pdf.Table table, IList <TSource> data) { var headRow = table.Rows.Add(); var props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in props) { headRow.Cells.Add(prop.GetCustomAttribute(typeof(DisplayAttribute)) is DisplayAttribute dd ? dd.Name : prop.Name); } foreach (var item in data) { // Add row to table var row = table.Rows.Add(); // Add table cells foreach (var t in props) { var dataItem = t.GetValue(item, null); if (t.GetCustomAttribute(typeof(DataTypeAttribute)) is DataTypeAttribute dataType) { switch (dataType.DataType) { case DataType.Currency: row.Cells.Add(string.Format("{0:C}", dataItem)); break; case DataType.Date: var dateTime = (DateTime)dataItem; if (t.GetCustomAttribute(typeof(DisplayFormatAttribute)) is DisplayFormatAttribute df) { row.Cells.Add(string.IsNullOrEmpty(df.DataFormatString) ? dateTime.ToShortDateString() : string.Format(df.DataFormatString, dateTime)); } break; default: row.Cells.Add(dataItem.ToString()); break; } } else { row.Cells.Add(dataItem.ToString()); } } } }
private void CreateTableOnPdfFile_4() { //create filename of pdf_file_tosave var pdf_file_tosave = @"d:\1.pdf"; //new pdf doc var pdfDoc = new Aspose.Pdf.Document(); //editing pdfDoc.SetTitle("hello!"); var pdfPage = pdfDoc.Pages.Add(); var table = new Aspose.Pdf.Table(); // Set the table border color as LightGray table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.LightGray); // Set the border for table cells table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.LightGray); // Create a loop to add 10 rows for (int row_count = 1; row_count <= 10; row_count++) { // Add row to table Aspose.Pdf.Row row = table.Rows.Add(); // Add table cells row.Cells.Add("Column (" + row_count + ", 1)"); row.Cells.Add("Column (" + row_count + ", 2)"); } // Add table object to first page of input document pdfPage.Paragraphs.Add(table); //Aspose.Pdf.Layer layer1 = new Aspose.Pdf.Layer("layer1","layer1"); //pdfPage.Layers.Add(layer1); //why is pdfPage.layers null? //save try { pdfDoc.Save(pdf_file_tosave, Aspose.Pdf.SaveFormat.Pdf); MessageBox.Show("Save Successfully!"); pdfDoc.Dispose(); Process.Start(pdf_file_tosave); } catch { MessageBox.Show("Faild to Save!"); } }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); DataTable dt = new DataTable("Employee"); dt.Columns.Add("data", System.Type.GetType("System.String")); DataRow dr = dt.NewRow(); dr[0] = "<li>Department of Emergency Medicine: 3400 Spruce Street Ground Silverstein Bldg Philadelphia PA 19104-4206</li>"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "<li>Penn Observation Medicine Service: 3400 Spruce Street Ground Floor Donner Philadelphia PA 19104-4206</li>"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "<li>UPHS/Presbyterian - Dept. of Emergency Medicine: 51 N. 39th Street . Philadelphia PA 19104-2640</li>"; dt.Rows.Add(dr); Document doc = new Document(); doc.Pages.Add(); // Initializes a new instance of the Table Aspose.Pdf.Table tableProvider = new Aspose.Pdf.Table(); //Set column widths of the table tableProvider.ColumnWidths = "400 50 "; // Set the table border color as LightGray tableProvider.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); // Set the border for table cells tableProvider.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo(); margin.Top = 2.5F; margin.Left = 2.5F; margin.Bottom = 1.0F; tableProvider.DefaultCellPadding = margin; tableProvider.ImportDataTable(dt, false, 0, 0, 3, 1, true); doc.Pages[1].Paragraphs.Add(tableProvider); doc.Save(dataDir + "HTMLInsideTableCell_out.pdf"); // ExEnd:1 }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); string outFile = dataDir + "AddRepeatingColumn_out.pdf"; // Create a new document Document doc = new Document(); Aspose.Pdf.Page page = doc.Pages.Add(); // Instantiate an outer table that takes up the entire page Aspose.Pdf.Table outerTable = new Aspose.Pdf.Table(); outerTable.ColumnWidths = "100%"; outerTable.HorizontalAlignment = HorizontalAlignment.Left; // Instantiate a table object that will be nested inside outerTable that will break inside the same page Aspose.Pdf.Table mytable = new Aspose.Pdf.Table(); mytable.Broken = TableBroken.VerticalInSamePage; mytable.ColumnAdjustment = ColumnAdjustment.AutoFitToContent; // Add the outerTable to the page paragraphs // Add mytable to outerTable page.Paragraphs.Add(outerTable); var bodyRow = outerTable.Rows.Add(); var bodyCell = bodyRow.Cells.Add(); bodyCell.Paragraphs.Add(mytable); mytable.RepeatingColumnsCount = 5; page.Paragraphs.Add(mytable); // Add header Row Aspose.Pdf.Row row = mytable.Rows.Add(); row.Cells.Add("header 1"); row.Cells.Add("header 2"); row.Cells.Add("header 3"); row.Cells.Add("header 4"); row.Cells.Add("header 5"); row.Cells.Add("header 6"); row.Cells.Add("header 7"); row.Cells.Add("header 11"); row.Cells.Add("header 12"); row.Cells.Add("header 13"); row.Cells.Add("header 14"); row.Cells.Add("header 15"); row.Cells.Add("header 16"); row.Cells.Add("header 17"); for (int RowCounter = 0; RowCounter <= 5; RowCounter++) { // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = mytable.Rows.Add(); row1.Cells.Add("col " + RowCounter.ToString() + ", 1"); row1.Cells.Add("col " + RowCounter.ToString() + ", 2"); row1.Cells.Add("col " + RowCounter.ToString() + ", 3"); row1.Cells.Add("col " + RowCounter.ToString() + ", 4"); row1.Cells.Add("col " + RowCounter.ToString() + ", 5"); row1.Cells.Add("col " + RowCounter.ToString() + ", 6"); row1.Cells.Add("col " + RowCounter.ToString() + ", 7"); row1.Cells.Add("col " + RowCounter.ToString() + ", 11"); row1.Cells.Add("col " + RowCounter.ToString() + ", 12"); row1.Cells.Add("col " + RowCounter.ToString() + ", 13"); row1.Cells.Add("col " + RowCounter.ToString() + ", 14"); row1.Cells.Add("col " + RowCounter.ToString() + ", 15"); row1.Cells.Add("col " + RowCounter.ToString() + ", 16"); row1.Cells.Add("col " + RowCounter.ToString() + ", 17"); } doc.Save(outFile); // ExEnd:1 }
public static void Run() { // ExStart:ExportExcelWorksheetDataToTable // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(new FileStream(dataDir + "newBook1.xlsx", FileMode.Open)); // Accessing the first worksheet in the Excel file Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0]; // Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable DataTable dataTable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, true); // Instantiate a Document instanc Aspose.Pdf.Document pdf1 = new Aspose.Pdf.Document(); // Create a page in the document instance Aspose.Pdf.Page sec1 = pdf1.Pages.Add(); // Create a Table object Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table(); // Add the Table object in the paragraphs collection of the section sec1.Paragraphs.Add(tab1); // Set column widths of the table. We need to specify the ColumnCount manually. // As the curent excel worksheet has three columsn, so we are specifying the same count tab1.ColumnWidths = "40 100 100"; // Set default cell border of the table using BorderInfo object tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); // Import data into the Table object from the DataTable created above tab1.ImportDataTable(dataTable, true, 0, 0, dataTable.Rows.Count + 1, dataTable.Columns.Count); // Get 1st row from the table Aspose.Pdf.Row row1 = tab1.Rows[0]; // Iterate through all cells in the 1st row and set their background color to blue foreach (Aspose.Pdf.Cell curCell in row1.Cells) { // Set the background of all the cells in 1st row of the table. curCell.BackgroundColor = Color.Blue; // Set the font face for the cells of 1st row in the table. curCell.DefaultCellTextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Helvetica-Oblique"); // Set the font Color of all the cells in 1st row of the table. curCell.DefaultCellTextState.ForegroundColor = Color.Yellow; // Set the text allignment for the cells of 1st row as Center. curCell.DefaultCellTextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center; } for (int All_Rows = 1; All_Rows <= dataTable.Rows.Count; All_Rows++) { // Iterate through all cells in the 1st row and set their background color to blue foreach (Aspose.Pdf.Cell curCell in tab1.Rows[All_Rows].Cells) { // Set the background color of all the cells except of the 1st row. curCell.BackgroundColor = Color.Gray; // Set the Text color of all the cells except the 1st row. curCell.DefaultCellTextState.ForegroundColor = Color.White; } } // Save the Pdf pdf1.Save(dataDir + @"Exceldata_toPdf_table.pdf"); // ExEnd:ExportExcelWorksheetDataToTable }
public static void Run() { // ExStart:PlacingTextAroundImage // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Instantiate document object Aspose.Pdf.Document doc = new Aspose.Pdf.Document(); // Create a page in the Pdf Aspose.Pdf.Page page = doc.Pages.Add(); // Instantiate a table object Aspose.Pdf.Table table1 = new Aspose.Pdf.Table(); // Add the table in paragraphs collection of the desired section page.Paragraphs.Add(table1); // Set with column widths of the table table1.ColumnWidths = "120 270"; // Create MarginInfo object and set its left, bottom, right and top margins Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo(); margin.Top = 5f; margin.Left = 5f; margin.Right = 5f; margin.Bottom = 5f; // Set the default cell padding to the MarginInfo object table1.DefaultCellPadding = margin; // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = table1.Rows.Add(); // Create an image object Aspose.Pdf.Image logo = new Aspose.Pdf.Image(); // Specify the image file path logo.File = dataDir + "aspose-logo.jpg"; // Specify the image Fixed Height logo.FixHeight = 120; // Specify the image Fixed Width logo.FixWidth = 110; row1.Cells.Add(); // Add the image to paragraphs collection of the table cell row1.Cells[0].Paragraphs.Add(logo); // Create string variables with text containing html tags string TitleString = "<font face=\"Arial\" size=6 color=\"#101090\"><b> Aspose.Pdf for .NET</b></font>"; string BodyString1 = "<font face=\"Arial\" size=2><br/>Aspose.Pdf for .NET is a non-graphical PDF� document reporting component that enables .NET applications to <b> create PDF documents from scratch </b> without utilizing Adobe Acrobat�. Aspose.Pdf for .NET is very affordably priced and offers a wealth of strong features including: compression, tables, graphs, images, hyperlinks, security and custom fonts. </font>"; // Create a text object to be added to the right of image Aspose.Pdf.HtmlFragment TitleText = new Aspose.Pdf.HtmlFragment(TitleString + BodyString1); row1.Cells.Add(); // Add the text paragraphs containing HTML text to the table cell row1.Cells[1].Paragraphs.Add(TitleText); // Set the vertical alignment of the row contents as Top row1.Cells[1].VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top; // Create rows in the table and then cells in the rows Aspose.Pdf.Row SecondRow = table1.Rows.Add(); SecondRow.Cells.Add(); // Set the row span value for Second row as 2 SecondRow.Cells[0].ColSpan = 2; // Set the vertical alignment of the second row as Top SecondRow.Cells[0].VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top; string SecondRowString = "<font face=\"Arial\" size=2>Aspose.Pdf for .NET supports the creation of PDF files through API and XML or XSL-FO templates. Aspose.Pdf for .NET is very easy to use and is provided with 14 fully featured demos written in both C# and Visual Basic.</font>"; Aspose.Pdf.HtmlFragment SecondRowText = new Aspose.Pdf.HtmlFragment(SecondRowString); // Add the text paragraphs containing HTML text to the table cell SecondRow.Cells[0].Paragraphs.Add(SecondRowText); // Save the Pdf file doc.Save(dataDir + "PlacingTextAroundImage_out.pdf"); // ExEnd:PlacingTextAroundImage }