itext keep paragraphs together - c#

I have 2 paragraph objects that take up about 2/3 of the page. When I view it in the pdf the start of the 2nd paragraph starts on the 2nd page. Is there a way to start it on 1st page following the 1st paragraph?
PdfPTable rs1 = new PdfPTable(1);
PdfPCell c = new PdfPCell();
c.MinimumHeight = 36f;
Paragraph p = new Paragraph(
"some text to align\n" +
"..." +
"some text to align\n"
);
c.AddElement(p);
rs1.AddCell(c);
PdfPCell c2 = new PdfPCell();
c.MinimumHeight = 36f;
Paragraph p2 = new Paragraph(
"some text to align\n" +
"..." +
"some text to align\n" +
"some text to align\n"
);
p2.KeepTogether = false;
c2.AddElement(p2);
c2.VerticalAlignment = Element.ALIGN_TOP;
rs1.AddCell(c2);
return rs1;

I used PdfPTable.SplitLate = false

The issue isn't with your paragraphs but with your table. iTextSharp tries to not break content across table cells and your current layout appears to do that. Do you need to have a table? Regular paragraphs will just break when a line goes off the viewable area. If you need tables then you'll have to adjust the table's width if you can (rs1.WidthPercentage = 100;) and possibly any padding that you've set up.

Related

How to sum the values of all rows in a column that has been dynamically generated?

Good Morning Fellow Coders,
I am trying to Sum all values in each row of a specific Column called SubTotal
SubTotal needs to sum from the LineTotal of every row, but the rows are generated dynamically and on a button click event. I will link my code down below and a screen shot and maybe one of you can help me:
EDIT: - I have tried all the "Solutions" below but each time i try to add that to my page it makes it that my PDF has no pages, i have tried doing my own research too, to no avail as those also make it that my pdf has no pages
tried these:-
(1)int sum = Convert.ToInt32(dt.Compute("SUM(Salary)", string.Empty));
(2)DataTable dt= dataSet.Tables["YourTableName"];
object lineTotalInputSum ;
lineTotalInputSum = dt.Compute("Sum("YourColumnName")", string.Empty);
(3)var subTotal= rows.Sum(row => row.Field<double>("LineTotal"));
i am not saying any of you are wrong, i am saying that i am not sure how to implement these suggestions into my code and keep the rest working, if you could give me a simple explanation i will do my best to make it work, feel free to ask for any additional code that i have not provided
------END EDIT--------
foreach (DataRow dataRow in dt.Rows)
{
//Adding dt.Rows to Strings for Use in iTextSharp
string lineNumberInput = dataRow[1].ToString();
string itemCodeInput = dataRow[12].ToString();
string itemNameInput = dataRow[13].ToString();
string QtyInput = dataRow[14].ToString();
string UnitPriceInput = dataRow[15].ToString();
//string Discount = dt.Rows[0][""].ToString();
string lineTotalInput = dataRow[16].ToString();
string wasReturnedInput = dataRow[17].ToString();
//Implementing strings in iTextSharp
var Cell_LineNumberList = new PdfPCell(new
Phrase(lineNumberInput, tablefont));
var Cell_ItemCodelist = new PdfPCell(new
Phrase(itemCodeInput, tablefont));
var Cell_ItemNamelist = new PdfPCell(new
Phrase(itemNameInput, tablefont));
var Cell_Qtylist = new PdfPCell(new Phrase(QtyInput,
tablefont));
var Cell_UnitPricelist = new PdfPCell(new
Phrase(UnitPriceInput, tablefont));
var Cell_Discountlist = new PdfPCell(new
Phrase("None", tablefont));
var Cell_LineTotallist = new PdfPCell(new
Phrase(lineTotalInput, tablefont));
var Cell_WasReturnedlist = new PdfPCell(new
Phrase(wasReturnedInput, tablefont));
//Aligning all the cells
Cell_LineNumberList.HorizontalAlignment =
Element.ALIGN_CENTER;
Cell_ItemCodelist.HorizontalAlignment =
Element.ALIGN_CENTER;
Cell_ItemNamelist.HorizontalAlignment =
Element.ALIGN_CENTER;
Cell_Qtylist.HorizontalAlignment =
Element.ALIGN_CENTER;
Cell_UnitPricelist.HorizontalAlignment =
Element.ALIGN_CENTER;
Cell_Discountlist.HorizontalAlignment =
Element.ALIGN_CENTER;
Cell_LineTotallist.HorizontalAlignment =
Element.ALIGN_CENTER;
Cell_WasReturnedlist.HorizontalAlignment =
Element.ALIGN_CENTER;
//adding the cells to the table
t.AddCell(Cell_LineNumberList);
t.AddCell(Cell_ItemCodelist);
t.AddCell(Cell_ItemNamelist);
t.AddCell(Cell_Qtylist);
t.AddCell(Cell_UnitPricelist);
t.AddCell(Cell_Discountlist);
t.AddCell(Cell_LineTotallist);
t.AddCell(Cell_WasReturnedlist);
}
Below you will find a screen shot of the column I want to sum in red and the value(from where the values come) column in green * these are all dynamically generated*
If you can just show me how to store it in a var then i can take it from there
and thank you in advance.
I am not quite sure how your DataTable looks like but you use the Compute method to get the sum of the column that you want:
DataTable dt= dataSet.Tables["YourTableName"];
object lineTotalInputSum ;
lineTotalInputSum = dt.Compute("Sum("YourColumnName")", string.Empty);
Try using the LINQ on the "Line Total" Column outside your foreach loop as follows
var subTotal= rows.Sum(row => row.Field<double>("LineTotal"));
or as you are processing row by row, use a old school method of having a variable and adding the row values to it.
Hope this helps!

Aspose pdf table

i have added a table with two rows and two columns and set border to all cells. but it shows the border only for first column
Sample code is shown below
var tableestdet = new Table
{
ColumnWidths = "120,120",
Margin = new MarginInfo { Top = 40, Left = 10 },
DefaultCellBorder = new BorderInfo((int)BorderSide.All, 1F),
};
tableestdet.DefaultCellTextInfo = new TextInfo { Alignment = AlignmentType.Center };
var estdet1 = tableestdet.Rows.Add();
estdet1.DefaultRowCellPadding = new MarginInfo { Top = 5, Bottom = 5 };
var req=estdet1.Cells.Add("Requested By:");
var estde=estdet1.Cells.Add("Entered By:");
var estdet2 = tableestdet.Rows.Add();
estdet2.DefaultCellTextInfo = new TextInfo
{
FontSize = 8,
Alignment = AlignmentType.Center
};
estdet2.DefaultRowCellPadding = new MarginInfo { Top = 5, Bottom = 5 };
estdet2.Cells.Add(Requestedby);
estdet2.Cells.Add(CustomerName);
sec1.Paragraphs.Add(tableestdet);
We have noticed that you are working with an outdated version of the API so please upgrade to Aspose.PDF for .NET 19.2 which is latest available version of the API as it includes more features and bug fixes. You may use below code snippet to add a table on page of PDF document, while setting borders for all cells.
// Load source PDF document
Aspose.Pdf.Document document = new Aspose.Pdf.Document();
// Add a page to the document
Page page = document.Pages.Add();
// 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 = 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
page.Paragraphs.Add(table);
// Save updated document containing table object
document.Save(dataDir + "Table_19.2.pdf");
Generated PDF document has been attached for your kind reference Table19.2.pdf. Please feel free to let us know if you need any further assistance.
PS: I work with Aspose as Developer Evangelist.

Sort Microsoft.Office.Interop.Word in C#

I use Microsoft.Office.Interop.Word to get words from Word file and then I populate it into a table layout panel. Unfortunately, the words displayed at the table layout panel are not following exact sequence as in the Word file.
How to fix this?
// Open a doc file.
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document d ocument = application.Documents.Open(txtUploadedPathToken.Text);
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// Write the word.
string text = document.Words[i].Text;
//Console.WriteLine("Word {0} = {1}", i, text);
tableLayoutPanel2.Controls.Add(new Label() { Text = text, Anchor = AnchorStyles.Left, AutoSize = true}, 0, 0);
}
Your word document reading code seems OK. but you may need to change how you add items to panel. Since you add new items to same position (0,0) it may give incorrect order.
foreach (Microsoft.Office.Interop.Word.Range range in document.Words)
{
string text = range.Text;
tableLayoutPanel2.Controls.Add(new Label() { Text = text, Anchor = AnchorStyles.Left, AutoSize = true});
}

Insert Word doc page number field in text box

I have a text box within a footer in a Word doc. It has some template text that is left-aligned, and then already has the "Page x of n" right-aligned. When I try and replace the template text, all of the text is replaced.
Using C# or VB (either way) I need to replace the text inside of the text box (all of this text should be left-aligned) and then add "Page x of n" (right-aligned).
Here is what I have so far as a test:
string footer;
foreach (Shape shape in oMyDoc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes)
{
footer = shape.TextFrame.ContainingRange.Text;
footer = footer.Replace("\r", "");
footer = footer.Replace("[Quote Type]", "Big Quote");
footer = footer.Replace("(", "\u2022");
int start = footer.IndexOf("Pager ");
footer = footer.Remove(start + 5);
var numPages = oMyDoc.ComputeStatistics(WdStatistic.wdStatisticPages);
footer = footer + " of " + numPages.ToString();
shape.TextFrame.ContainingRange.Text = footer;
}

Spacing/Leading PdfPCell's elements

Is it possible to add space between the elements of a cell (rows) in C#?
I'm creating a pdf in visual studio 2012 and wanted to set some space between the rows. I have something like this:
PdfPTable cellTable = new PdfPTable(1);
PdfPCell cell= new PdfPCell();
for(i=0; i < 5; i++)
{
var titleChunk = new Chunk(tittle[i], body);
var descriptionChunk = new Chunk(" " description[i], body2);
var phrase = new Phrase(titleChunk);
phrase.Add(descriptionChunk);
cell.AddElement(phrase);
}
cellTable.AddCell(cell);
OK, I've made you an example named LeadingInCell:
PdfPCell cell = new PdfPCell();
Paragraph p;
p = new Paragraph(16, "paragraph 1: leading 16");
cell.addElement(p);
p = new Paragraph(32, "paragraph 2: leading 32");
cell.addElement(p);
p = new Paragraph(10, "paragraph 3: leading 10");
cell.addElement(p);
p = new Paragraph(18, "paragraph 4: leading 18");
cell.addElement(p);
p = new Paragraph(40, "paragraph 5: leading 40");
cell.addElement(p);
As you can see in leading_in_cell.pdf, you define the space between the lines using the first parameter of the Paragraph constructor. I've used different values to demonstrate how it works. The third paragraph sticks to the second one, because the leading of the third paragraph is only 10 pt. There's plenty of space between the fourth and the fifth paragraph, because the leading of the fifth paragraph is 40 pt.

Categories