Aspose pdf table - c#

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.

Related

How to add multiple texts on Visio connector shapes using the c#/vba code?

I want to add multiple texts on the connector line as per the image below. I am using c# code to automate the process. Below is my code which I have used. It is not giving the exact output as I had expected. Any help in this regard would be highly appreciated.
Visio.Shape vsoLastShape = visioPage.Shapes.get_ItemFromID(lastshapeID);
vsoLastShape.ConvertToGroup();
Visio.Selection vsoSelections = app.ActiveWindow.Selection;
vsoSelections.Select(vsoLastShape, (short)VisSelectArgs.visSelect);
Visio.Shape vsoGroupShape = vsoSelections.Group();
vsoGroupShape.Text = "Testing 12";
vsoGroupShape.TextStyle.PadLeft(10);
Whatever method (manual, C#, VBA or whatevber) you use, one shape can only contain one text. If you want to add more than one text then you need to convert the shape into a grouped shape. Then you can add a shape to the group and set that sub-shape's text to what you want.
Shape .Characters object used in conjunction with .Text allows for some flexibility.
private string nl = Environment.NewLine;
public void MultiText() {
try {
// using = System.Windows.Forms;
// using Vis = Microsoft.Office.Interop.Visio;
Vis.Application app = Globals.ThisAddIn.Application; // or launch Visio
Vis.Document vDoc = app.Documents.Add(""); // new blank document
Vis.Shape c1 = app.ActivePage.DrawOval(1, 1, 1.5, 1.5);
Vis.Shape s1 = app.ActivePage.DrawLine(1.5, 1.25, 4, 1.25);
s1.Text = $"Shape1{nl}Line2";
Vis.Shape c2 = app.ActivePage.DrawOval(4, 1, 4.5, 1.5);
Vis.Shape c3 = app.ActivePage.DrawOval(1, 3, 1.5, 3.5);
Vis.Shape s2 = app.ActivePage.DrawLine(1.5, 3.25, 4, 3.25);
s2.Text = $"Shape2";
Vis.Shape c4 = app.ActivePage.DrawOval(4, 3, 4.5, 3.5);
app.ActiveWindow.CenterViewOnShape(c4, Vis.VisCenterViewFlags.visCenterViewDefault);
app.ActiveWindow.Zoom = 1.2;
app.ActiveWindow.Selection.DeselectAll();
app.DoCmd((short)VisUICmds.visCmdDeselectAll);
System.Windows.Forms.MessageBox.Show($"2 Shapes with text.", "Continue...");
// reset the Text on Shape #2 and define 2 separate ranges
s2.Text = "";
// alocate a range
Characters range1 = s2.Characters;
range1.Begin = 0;
range1.End = 3;
range1.Text = "Name";
// alocate another
Characters range2 = s2.Characters;
range2.Begin = 4;
//range2.End = 7;
range2.Text = $"{Environment.NewLine}Type";
MessageBox.Show($"Now change Font Size", "Continue...");
// change font size or any of numerous properties
range1.CharProps[(short)Vis.VisCellIndices.visCharacterSize] = 16;
range2.CharProps[(short)Vis.VisCellIndices.visCharacterSize] = 8;
//range2.CharProps[(short)Vis.VisCellIndices.visCharacterStrikethru] = 1; // 1-true 0-false
MessageBox.Show($"Big Name!{nl}little Type.", "OK to continue");
} catch (Exception ex) {
ta.LogIt($"Err {ex.Message} Trace {ex.StackTrace}");
}
}
You may want to check this post and used the uploaded stencil. (Requires registration to see and download the attachment):
http://visguy.com/vgforum/index.php?topic=6318.msg25957#msg25957

The maximum value is not taken into account - MicroCharts C# .NET

I am using the QuestPDF library and MicroCharts to generate a pdf with charts.
It is happening to me that when generating the first bar graph, it does it correctly. However, if I keep generating more reports, the graph starts to look different.
Does anyone know why this could happen?
I hope you can help me.
I leave you some pictures so you understand what is happening to me:
First Report
Second report
third report
I leave the part of my code where I generate the Bar Chart:
List<ChartEntry> totalTransactionsByMonthsEntries = new List<ChartEntry> { };
List<string> randomColors = new List<string> { "#5AA454", "#A10A28", "#C7B42C", "#AAAAAA", "#80DFFF", "#FF80DF", "#008000", "#4D4DFF", "#333333", "#B84DFF", "#FFAD33", "#FF1A8C" };
var countBarColors = 0;
foreach (var item in paymentEvolutionExportDetail.TotalTransactionsByMonths)
{
totalTransactionsByMonthsEntries.Add(new ChartEntry(item.Transactions)
{
Label = item.Month,
ValueLabel = item.Transactions.ToString(),
Color = SKColor.Parse(randomColors[countBarColors]),
ValueLabelColor = SKColors.Black
});
countBarColors++;
}
var totalTransactionsByMonthsChart = new BarChart
{
Entries = totalTransactionsByMonthsEntries,
LabelOrientation = Orientation.Horizontal,
ValueLabelOrientation = Orientation.Horizontal,
IsAnimated = false,
LabelTextSize = 8,
Margin = 10
};
return totalTransactionsByMonthsChart;
And this is the part of the PDF, where I add the chart to my document:
grid.Item(2)
.PaddingLeft(-10)
.PaddingTop(-150)
.Width(270)
.Border(0.5f)
.BorderTop(0)
.BorderColor("#A5A5A5")
.Background("#FFFFFF")
.Height(200)
.Canvas((canvas, size) =>
{
barChart.DrawContent(canvas, 270, 200);
});
I tried giving the maximum value to the chart, but this gives me the same result as shown in the images.
var maxTransactionValue = paymentEvolutionExportDetail.TotalTransactionsByMonths.Select(x => x.Transactions).Max();
totalTransactionsByMonthsChart.MaxValue = maxTransactionValue;
Finally, I solved this problem by adding the bar chart generation to the PDF report generation:
grid.Item(2)
.PaddingLeft(-10)
.PaddingTop(-150)
.Width(270)
.Border(0.5f)
.BorderTop(0)
.BorderColor("#A5A5A5")
.Background("#FFFFFF")
.Height(200)
.Canvas((canvas, size) =>
{
var barChart = new BarChart
{
Entries = barChartEntries,
LabelOrientation = Orientation.Horizontal,
ValueLabelOrientation = Orientation.Horizontal,
IsAnimated = false,
LabelTextSize = 8,
Margin = 10
};
barChart.DrawContent(canvas, 270, 200);
});

iTextSharp - How can I iterate tables side by side in C# winforms?

I have a book library and I want to print library labels for them. When you select books from GridControl and click Print Labels button, the program creates labels on a pdf file like this:
private void btnQRPrintLabels_Click(object sender, EventArgs e)
{
// These are books' ids. Normally, these are retrieved from GridControl element.
var books_ids = new List<int>() {1, 2, 5, 6, 7, 12};
// I don't use PanelControl. But in the future, maybe I can use it later.
CreateLabels(books_ids, panelControl1);
}
And here is the contents of CreateLabels method:
public void CreateLabels(List<int> books_ids, PanelControl p)
{
var doc = new Document(PageSize.A4, 10, 10, 10, 10);
var m = new SaveFileDialog
{
Filter = #"PDF File Format|*.pdf",
FileName = "Labels.pdf"
};
if (m.ShowDialog() != DialogResult.OK) return;
var file = new FileStream(m.FileName, FileMode.Create);
wr = PdfWriter.GetInstance(doc, file);
doc.Open();
cb = wr.DirectContent;
wr.PageEvent = this;
// Labels are created from this method.
Labels(doc, books_ids);
doc.Close();
}
Finally, the contents of Labels method:
protected void Labels(Document doc, List<int> books_ids)
{
var i = 1;
foreach (var book_id in books_ids)
{
var alignment = (i % 2 != 0) ? Element.ALIGN_LEFT : Element.ALIGN_RIGHT;
// Book's informations are retrieved from Linq Connect Model.
var _connection = new LinqtoSQLiteDataContext();
var book = _connection.books.SingleOrDefault(b_no => b_no.id == book_id);
// Outer table to get rounded corner...
var table = new PdfPTable(1) { WidthPercentage = 48, HorizontalAlignment = alignment };
// Inner table: First cell for QR code and second cell for book's informations.
var inner_table = new PdfPTable(2) { WidthPercentage = 100 };
inner_table.DefaultCell.Border = Rectangle.NO_BORDER;
var inner_measurements = new[] { 40f, 60f };
inner_table.SetWidths(inner_measurements);
// Generate QR code in the `Tools` class, `GenerateQR` method.
System.Drawing.Image image = Tools.GenerateQR(100, 100, book?.isbn);
var pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
var qr = iTextSharp.text.Image.GetInstance(pdfImage);
var a = new PdfPCell(qr) { Border = Rectangle.NO_BORDER };
var b = new PdfPCell(new Phrase(book?.name, font_normal)) { Border = Rectangle.NO_BORDER };
inner_table.AddCell(a);
inner_table.AddCell(b);
var s = new PdfPCell(inner_table)
{
CellEvent = new RoundRectangle(),
Border = Rectangle.NO_BORDER,
Padding = 2,
HorizontalAlignment = Element.ALIGN_LEFT
};
table.AddCell(s);
doc.Add(table);
i++;
}
}
These codes bring me labels like this:
But I want to get labels like this:
Because if I manage getting labels side by side, I print it to computer labels easily. Here is the computer label that I want to print to on it:
How can I iterate tables side by side?
Use one outer Table with
new PdfPTable(2) { WidthPercentage = 100 }
Then add all your inner tables to this one and at last add the outer table to the document. This way there is NO need to use that toggeling left/right alignment.
:edit:
var table = new PdfPTable(2) { WidthPercentage = 100 };
foreach (var book_id in books_ids)
{
// Book's informations are retrieved from Linq Connect Model.
var _connection = new LinqtoSQLiteDataContext();
var book = _connection.books.SingleOrDefault(b_no => b_no.id == book_id);
// Outer table to get rounded corner...
// Inner table: First cell for QR code and second cell for book's informations.
var inner_table = new PdfPTable(2) { WidthPercentage = 100 };
inner_table.DefaultCell.Border = Rectangle.NO_BORDER;
var inner_measurements = new[] { 40f, 60f };
inner_table.SetWidths(inner_measurements);
// Generate QR code in the `Tools` class, `GenerateQR` method.
System.Drawing.Image image = Tools.GenerateQR(100, 100, book?.isbn);
var pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
var qr = iTextSharp.text.Image.GetInstance(pdfImage);
var a = new PdfPCell(qr) { Border = Rectangle.NO_BORDER };
var b = new PdfPCell(new Phrase(book?.name, font_normal)) { Border = Rectangle.NO_BORDER };
inner_table.AddCell(a);
inner_table.AddCell(b);
PdfPCell labelCell = new PdfPCell(inner_table)
{
CellEvent = new RoundRectangle(),
Border = Rectangle.NO_BORDER,
Padding = 2,
HorizontalAlignment = Element.ALIGN_LEFT
};
table.AddCell(labelCell);
}
doc.Add(table);
Just add page-level table (2 columns, 6 rows) and place each label into individual cell in this page-level table.

Fill PDFP Table Cell with Dots

I have a PDFP Table and I want to have it laid out like so:
Item1 ............ $10.00
Item1123123 ...... $50.00
Item3 ............ $75.00
This is what I have so far:
var tableFont = FontFactory.GetFont(FontFactory.HELVETICA, 7);
var items = from p in ctx.quote_server_totals
where p.item_id == id
&& p.name != "total"
&& p.type != "totals"
select p;
foreach (var innerItem in items)
{
detailsTable.AddCell(new Phrase(innerItem.type == "discount" ? "ADJUSTMENT -" + innerItem.name : innerItem.name, tableFont));
detailsTable.AddCell(new Phrase(".......................................................", tableFont));
detailsTable.AddCell(new Phrase(Convert.ToDecimal(innerItem.value).ToString("c"), tableFont));
}
document.Add(detailsTable);
As can see, the only way I've been able to get the dots to extend is by manually entering them; however, this obviously won't work because the the first column's width will be different every time I run this code. Is there a way I can accomplish this? Thanks.
Please download chapter 2 of my book and search for DottedLineSeparator. This separator class will draw a dotted line between two parts of a Paragraph (as shown in the figures in the book). You can find the C# version of the Java book samples here.
If you can use a fixed-width font such as FontFactory.COURIER your task will be a lot easier.
//Our main font
var tableFont = FontFactory.GetFont(FontFactory.COURIER, 20);
//Will hold the shortname from the database
string itemShortName;
//Will hold the long name which includes the periods
string itemNameFull;
//Maximum number of characters that will fit into the cell
int maxLineLength = 23;
//Our table
var t = new PdfPTable(new float[] { 75, 25 });
for (var i = 1; i < 10000; i+=100) {
//Get our item name from "the database"
itemShortName = "Item " + i.ToString();
//Add dots based on the length
itemNameFull = itemShortName + ' ' + new String('.', maxLineLength - itemShortName.Length + 1);
//Add the two cells
t.AddCell(new PdfPCell(new Phrase(itemNameFull, tableFont)) { Border = PdfPCell.NO_BORDER });
t.AddCell(new PdfPCell(new Phrase(25.ToString("c"), tableFont)) { Border = PdfPCell.NO_BORDER });
}

itext keep paragraphs together

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.

Categories