I am trying to create inner cells in pdf with using aspose words. But I have issues about its style. I need to center and fit my text in cell. But when I set center property for cell it gives some padding automatically.
I made a sample with MS word as below image:
This one is that I have created programmatically with using aspose words.
As you can see it doesn't fit when I set center property true for columns.
My inner cell import code is below:
Cell cell = table.Rows[j].Cells[i];
cell.CellFormat.RightPadding = 0;
foreach (Paragraph pf in cell.Paragraphs)
{
pf.ParagraphFormat.LeftIndent = 0;
}
cell.CellFormat.WrapText = false;
builder.MoveTo(cell.FirstParagraph);
builder.InsertCell();
builder.Writeln(tr.Rows[j][i].Value);
builder.EndTable();
Which property should I set to fit it in cell, thanks.
Please use following code example to get the required output. You may modify the table/cell width according to your requirement. I suggest you please read following link to work with tables. Hope this helps you.
Working with Tables
If you still face problem, share your input and expected output documents. I will share the code according to your expected document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Calibri";
builder.CellFormat.LeftPadding = 0;
builder.CellFormat.RightPadding = 0;
builder.CellFormat.TopPadding = 0;
builder.CellFormat.BottomPadding = 0;
Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(33);
//Insert inner table
Table table1 = builder.StartTable();
builder.InsertCell();
table1.Alignment = TableAlignment.Left;
table1.PreferredWidth = PreferredWidth.FromPercent(50);
builder.Write("This is cell 1");
builder.EndRow();
builder.EndTable();
// Insert a cell
builder.InsertCell();
//Insert inner table
Table table2 = builder.StartTable();
builder.InsertCell();
table2.Alignment = TableAlignment.Center;
table2.PreferredWidth = PreferredWidth.FromPercent(50);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("This is cell 2");
builder.EndRow();
builder.EndTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(33);
//Insert inner table
Table table3 = builder.StartTable();
builder.InsertCell();
table3.PreferredWidth = PreferredWidth.FromPercent(50);
table3.Alignment = TableAlignment.Right;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("This is cell 3");
builder.EndRow();
builder.EndTable();
builder.EndRow();
builder.EndTable();
doc.Save(MyDir + "Out.docx");
I work with Aspose as Developer evangelist.
Related
I want to export my Sqlite to a PDF. I am able to do it but instead of putting the result in just one row, it puts the the result in the other column i have. What I want is to fill the row in the same column(incremente the rows)with the database results. This is what I have, Can you help me with this?
db = new Database(FirstActivity.this);
Cursor c = bd.getGroupsNamesMachines();
PdfPTable table = new PdfPTable(2);
cell = new PdfPCell(new Phrase("groups"));
cell2 = new PdfPCell(new Phrase("machines"));
if (c.moveToFirst()) {
do {
String groups = c.getString(c.getColumnIndex("groupname"));
cell3 = new PdfPCell(new Phrase(groups));
table.addCell(cell3);
} while (c.moveToNext());
}
cell.setColspan(1);
cell2.setColspan(2);
cell3.setColspan(1);
table.addCell(cell);
table.addCell(cell2);
table.addCell(cell3);
Please read the documentation, or watch this video (before the first 10 minutes are over, you'll know how to create a table that shows all the states of the US). The code for that example can be found here: set format of header row using iText
In your case, you are doing something really strange. You create a table with 2 columns. That's OK if each record has two fields. However, you add the body cells before you add the header cells. I think you're trying to create a header row with 3 columns (which is illogical), and you probably don't complete the last row (and iText only adds complete rows by default).
You need something like this:
// Table
PdfPTable table = new PdfPTable(2);
// Header
PdfPCell cell1 = new PdfPCell(new Phrase("groups"));
PdfPCell cell2 = new PdfPCell(new Phrase("machines"));
table.addCell(cell1);
table.addCell(cell2);
// Data
db = new Database(FirstActivity.this);
Cursor c = db.getGroupsNamesMachines();
if (c.moveToFirst()) {
do {
String group = c.getString(c.getColumnIndex("groupname"));
cell1 = new PdfPCell(new Phrase(group));
table.addCell(cell1);
String machine = c.getString(c.getColumnIndex("machinename"));
cell2 = new PdfPCell(new Phrase(machine));
table.addCell(cell2);
} while (c.moveToNext());
}
document.add(table);
I don't know if the database code is OK; it's your responsibility to know how to get the correct data from the database, but please compare your very asymmetric code with the logical code I changed it into.
First define the header, and add it to the table,
Then loop over the data, and add the data row by row,
Never add more cells to a row than there are columns!
I have a treeTable with editable cells within the expanded rows. The editable cells get a dirty flag after editing (in the example the background color is set to red).
The problem i'm running into is that i found no certain way to update the dirty flag on expand/collapse (edited cells get the css class 'edited-cell').
At the moment the code looks like that:
// each editable textfield gets a Listener
textField.attachLiveChange(
var source = oEvent.oSource;
...
jQuery('#' + source.getId()).addClass(EDITED_CELL_CLASS, false)
// list with cell ids, e.g. "__field1-col1-row1"
DIRTY_MODELS.push(model.getId()) //*** add also binding context of row
)
// the table rows are updated on toggleOpenState
new sap.ui.table.TreeTable({
toggleOpenState: function(oEvent) {
...
this.updateRows() // see function below
}
})
// update Rows function is also delegated
oTable.addDelegate({ onAfterRendering : jQuery.proxy(this.updateRows, oTable)});
//http://stackoverflow.com/questions/23683627/access-row-for-styling-in-sap-ui5-template-handler-using-jquery
// this method is called on each expand/collapse: here i can make sure that the whole row has it's correct styling...
// but how to make sure that special cells are dirty?
function updateRows(oEvent) {
if (oEvent.type !== 'AfterRendering'){
this.onvscroll(oEvent);
}
var rows = this.getVisibleRowCount();
var rowStart = this.getFirstVisibleRow();
var actualRow;
for (var i = 0; i < rows; i++){
actualRow = this.getContextByIndex(rowStart + i); //content
var row = this.getRows()[i]
var obj = actualRow.getObject()
var rowId = row.getId()
updateStyleOfRows(obj, rowId, actualRow)
updateDirtyCells(rowId) //*** how to get the binding context in this function???
}
};
// update Dirty Cells in function updateRows():
function updateDirtyCells(rowId){
for (var i = 0; i < DIRTY_MODELS.length; i++){
var dirtyCellId = DIRTY_MODELS[i]
//*** make sure that only the correct expanded/collapsed rows will be updated -> depends on the bindingContext of the row
jQuery('#' + rowId).find('#' + dirtyCellId + '.editable-cell').addClass(EDITED_CELL_CLASS, false)
}
}
This doesn't work correctly, because the ids of the cells change on each layout render (e.g. collapse/expand rows). Please see attached image.
Let me know if i should provide more information.
In my Requirement i want to repeat a particular paragraph which is in section in a word document.
here word document divided into sections, in sections we have paragraphs like below
#Section Start
1) TO RECEIVE AND ADOPT FINANCIAL STATEMENTS FOR THE YEAR ENDED [FYE]
a.That the Financial Statements of the Company for the financial year ended [FYE] together with the Director(s)' Report and Statement thereon be hereby received and adopted.
b. Second paragraph.
c. Third paragraph.
#Section End
i want to repeat "a" point into 3 times
i tried the below code
// Copy all content including headers and footers from the specified
//pages into the destination document.
ArrayList pageSections = finder.RetrieveAllNodesOnPages(1, doc.Sections.Count, NodeType.Section);
System.Data.DataTable dt = GetDataTable(); //Sample DataTable which is having Keys and Values
int sectionCount = 0;
foreach (Section section in pageSections)
{
NodeCollection paragraphs = section.GetChildNodes(NodeType.Paragraph, true);
for (int i = 0; i < paragraphs.Count; i++)
{
string text = paragraphs[i].Range.Text;
}
}
Please help me how to repeat a paragraph.
I am working as Social Media Developer at Aspose. Please use the following sample code to repeat a paragraph using Aspose.Words for .NET.
Document doc = new Document("document.docx");
PageNumberFinder finder = new PageNumberFinder(doc);
// Split nodes which are found across pages.
finder.SplitNodesAcrossPages(true);
// Copy all content including headers and footers from the specified pages into the
//destination document.
ArrayList pageSections = finder.RetrieveAllNodesOnPages(1, doc.Sections.Count, NodeType.Section);
//Sample DataTable which is having Keys and Values
System.Data.DataTable dt = GetDataTable();
int sectionCount = 0;
foreach (Section section in pageSections)
{
NodeCollection paragraphs = section.GetChildNodes(NodeType.Paragraph, true);
for (int i = 0; i < paragraphs.Count; i++)
{
//Paragraph you want to copy
if (i == 10)
{
//Use Document Builder to Navigate to the paragraph
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(paragraphs[i]);
//Insert a Paragraph break
builder.InsertParagraph();
//Insert the Paragraph to repeat it
builder.Writeln(paragraphs[i].ToString(SaveFormat.Text));
}
}
}
doc.Save("test.docx");
I am working on generating pdf using itextsharp ,In my table i have multiple columns such as sr.ni. name .],quantity ,mrp ,price tax etc.
Name column has round about 40% of total width all values in other column comes right in middle of row but name's value is somehow close to bottom line ,Everything is same for all columns ie. style,font etc.
code
var cell=new PdfPcell();
cell = new PdfPCell(new Phrase(price, font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticleAlignMent= Element.ALIGN_CENTER;
producttable.AddCell(cell);
same code for all values
any solutions.
also tried
cell.AddElement(new Chunk(name, font));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
productsTable.AddCell(cell);
didnt work
You can assign vertical alignment to your particular cell as shown below:
pdfCell2.VerticalAlignment = Element.ALIGN_BOTTOM;
pdfCell3.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell4.VerticalAlignment = Element.ALIGN_TOP;
I have made a table with cells and interested in having an image in one of the cell.
Below is my code:
doc.Open();
PdfPTable table = new PdfPTable(2);
table.TotalWidth = 570f;
table.LockedWidth = true;
table.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
PdfPCell points = new PdfPCell(new Phrase("and is therefore entitled to 2 points", arialCertify));
points.Colspan = 2;
points.Border = 0;
points.PaddingTop = 40f;
points.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
table.AddCell(points);
// add a image
doc.Add(table);
Image jpg = Image.GetInstance(imagepath + "/logo.jpg");
doc.Add(jpg);
With the above code, the image shows in my pdf but I want it to be inside a cell so that I can add more cells to the right of the image.
On the very basic level you can simply add the image to a PdfPCell and add this cell to your table.
So using your code...
PdfPCell points = new PdfPCell(new Phrase("and is therefore entitled to 2 points", arialCertify));
points.Colspan = 2;
points.Border = 0;
points.PaddingTop = 40f;
points.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
// add a image
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imagepath + "/logo.jpg");
PdfPCell imageCell = new PdfPCell(jpg);
imageCell.Colspan = 2; // either 1 if you need to insert one cell
imageCell.Border = 0;
imageCell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.AddCell(points);
// add a image
table.AddCell(imageCell);
doc.Add(table);
Update
Check your imagepath. This should be an absolute path to the image, and not relative like in a website page. Also, change your `/logo.jpg' to '\logo.jpg'
this is assuming imagepath is actually to the directory, and not the actual image...
I.E
Server.MapPath(imagepath) + "\\logo.jpg"