ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,158 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello friends,
In my Blazor Server application, I am generating a PDF like this. But requirements changed, I need to insert a table and logo like this sample. How can I manage it?
InkedScreenshot 2023-01-31 at 19-47-25 Purchase Order - P21112317104.pdf_LI.jpg
InkedScreenshot 2023-01-31 at 20-15-41.jpg
private async Task ReportPDF(VendorDto vendorVM)
{
double sum = 0;
using var memoryStream = new MemoryStream();
await using var writer = new PdfWriter(memoryStream);
writer.SetCloseStream(false);
using PdfDocument pdfDocument = new PdfDocument(writer);
Document doc = new Document(pdfDocument, PageSize.A4, false);
Paragraph header = new Paragraph("ORDER REPORT")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(20);
doc.Add(new Paragraph());
doc.Add(header);
// Line separator
LineSeparator line1 = new LineSeparator(new SolidLine());
doc.Add(line1);
doc.Add(new Paragraph());
Paragraph orderDate = new Paragraph("Order Date: " + Order.OrderDateTime)
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(orderDate);
Paragraph company = new Paragraph("Company: Vorlance")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(company);
Paragraph responsible = new Paragraph("Responsible: " + Order.DoneBy)
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(responsible);
Paragraph vendor = new Paragraph("Vendor: " + orderDetails.Where(x => x.VendorId == vendorVM.Id).Select(y => y.Vendor.Name).FirstOrDefault())
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(vendor);
switch (vendorVM.Warehouse)
{
case "China Warehouse":
FontProgram fontProgram =
FontProgramFactory.CreateFont(@"Resources/NotoSansTC-Regular.otf");
PdfFont chinese = PdfFontFactory.CreateFont(fontProgram, PdfEncodings.IDENTITY_H);
Paragraph delivery = new Paragraph($"Delivery Information: Co Limited")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14)
.SetFont(chinese);
doc.Add(delivery);
Paragraph address = new Paragraph($"Address: Test Address")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14)
.SetFont(chinese);
doc.Add(address);
Paragraph contact = new Paragraph($"Warehouse Contact: ABC")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14)
.SetFont(chinese);
doc.Add(contact);
Paragraph phone = new Paragraph("Phone: +88-1111111111")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(phone);
Paragraph contact3 = new Paragraph($"Merchandise Contact: Veveveveve")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14)
.SetFont(chinese);
doc.Add(contact3);
Paragraph phone3 = new Paragraph("Phone: +88-181111110")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(phone3);
Paragraph email = new Paragraph("Email: test@test.com")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(email);
break;
case "Shenzen Warehouse":
FontProgram fontProgram2 =
FontProgramFactory.CreateFont(@"Resources/NotoSansTC-Regular.otf");
PdfFont chinese2 = PdfFontFactory.CreateFont(fontProgram2, PdfEncodings.IDENTITY_H);
Paragraph delivery5 = new Paragraph($"Delivery Information: Co.,Ltd")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14)
.SetFont(chinese2);
doc.Add(delivery5);
Paragraph address5 = new Paragraph($"Address: Building, District, Shenzhen ZIP Code:1111111")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14)
.SetFont(chinese2);
doc.Add(address5);
Paragraph contact6 = new Paragraph($"Warehouse Contact: Test Test")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14)
.SetFont(chinese2);
doc.Add(contact6);
Paragraph phone6 = new Paragraph("Phone: +81 111111111")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(phone6);
Paragraph email5 = new Paragraph("Email: test1@c.com")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(email5);
break;
case "USA Warehouse":
Paragraph delivery1 = new Paragraph("Delivery Information: TRADING LLC")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(delivery1);
Paragraph address1 = new Paragraph("Address: Test, FL, ZIP Code: 11111")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(address1);
Paragraph contact1 = new Paragraph("Contact: Contact1")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(contact1);
Paragraph email1 = new Paragraph("Email: test@firm.com")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(email1);
Paragraph phone1 = new Paragraph("Phone: +1(999)1111111")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(phone1);
break;
case "Private Shipping":
Paragraph delivery2 = new Paragraph("Delivery Information: Company info")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(delivery2);
Paragraph address2 = new Paragraph("Address: Test Adress")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(address2);
Paragraph contact2 = new Paragraph("Contact: Contact Name")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(contact2);
Paragraph email2 = new Paragraph("Email: stest@stest.com")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(email2);
Paragraph phone2 = new Paragraph("Phone: +99 11111111")
.SetTextAlignment(TextAlignment.LEFT)
.SetFontSize(14);
doc.Add(phone2);
break;
}
LineSeparator line2 = new LineSeparator(new SolidLine());
doc.Add(line2);
doc.Add(new Paragraph());
doc.Add(new Paragraph());
Table table = new Table(7).UseAllAvailableWidth();
table.AddHeaderCell(new Cell(1, 7).Add(new Paragraph("Order Details")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold())
.SetTextAlignment(TextAlignment.CENTER);
for (int i = 0; i < 1; i++) {
table.AddHeaderCell(new Cell().Add(new Paragraph("ORDER ID")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
table.AddHeaderCell(new Cell().Add(new Paragraph("Product ID")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
table.AddHeaderCell(new Cell().Add(new Paragraph("Product Code")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
table.AddHeaderCell(new Cell().Add(new Paragraph("Currency")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
table.AddHeaderCell(new Cell().Add(new Paragraph("Quantity")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
table.AddHeaderCell(new Cell().Add(new Paragraph("Unit Price")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
table.AddHeaderCell(new Cell().Add(new Paragraph("Total Price")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
}
foreach (var details in orderDetails.Where(x => x.VendorId == vendorVM.Id))
{
table.AddCell(new Cell().Add(new Paragraph(details.OrderId.ToString())));
table.AddCell(new Cell().Add(new Paragraph(details.Id.ToString())));
table.AddCell(new Cell().Add(new Paragraph(details.ProductCode)));
table.AddCell(new Cell().Add(new Paragraph(details.Currency)));
table.AddCell(new Cell().Add(new Paragraph(details.Quantity.ToString())));
table.AddCell(new Cell().Add(new Paragraph(details.BuyUnitPrice.ToString())));
table.AddCell(new Cell().Add(new Paragraph(details.TotalBuyPrice.ToString())));
sum = sum + details.TotalBuyPrice;
}
//table.AddCell(new Cell().Add(new Paragraph()));
//table.AddCell(new Cell().Add(new Paragraph()));
//table.AddCell(new Cell().Add(new Paragraph()));
//table.AddCell(new Cell().Add(new Paragraph()));
//table.AddCell(new Cell().Add(new Paragraph()));
//table.AddCell(new Cell().Add(new Paragraph("Total: " + sum.ToString())));
doc.Add(table);
// Page numbers
int n = pdfDocument.GetNumberOfPages();
for (int i = 1; i <= n; i++)
{
doc.ShowTextAligned(new Paragraph(String
.Format(i + " of " + n)),
559, 806, i, TextAlignment.RIGHT,
VerticalAlignment.BOTTOM, 0);
}
doc.Close();
memoryStream.Position = 0;
var pdfname = $"Report-{DateTime.Now.ToString("ddMMyyyyHHmm")}.pdf";
using var streamRef = new DotNetStreamReference(stream: memoryStream);
await JsRuntime.InvokeVoidAsync("downloadFileFromStream", pdfname, streamRef);
}