0% found this document useful (0 votes)
11 views2 pages

Working Member Name and Address

Uploaded by

kirankumarsexy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Working Member Name and Address

Uploaded by

kirankumarsexy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

try

{
// Retrieve the existing document
var document = await _documentsService.GetDocument(id);
if (document == null)
{
_logger.LogWarning("Document with ID {Id} not found", id);
return NotFound();
}

byte[] pdfBytes = document.Document;

// Retrieve member info


var memberInfo = await _memberListService.GetMemberInfo(memberid);

// Process member name


string memberName = string.Join(" ", new[]
{
memberInfo.LastName?.Trim(),
memberInfo.FirstName?.Trim(),
memberInfo.MiddleName?.Trim()
}.Where(part => !string.IsNullOrWhiteSpace(part)));

// Process member address


string memberAddress = string.Join(", ",
new[] { memberInfo.Address }
.Where(part => !string.IsNullOrWhiteSpace(part))
);

using (var newPdfDocument = new PdfDocument())


{
// Add a new page to the PDF
PdfPage newPage = newPdfDocument.AddPage();
using (XGraphics gfx = XGraphics.FromPdfPage(newPage))
{
// Define fonts
XFont fontName = new XFont("Verdana", 14, XFontStyleEx.Bold);
XFont fontAddress = new XFont("Verdana", 12, XFontStyleEx.Regular);
XFont fontRectangleText = new XFont("Verdana", 10,
XFontStyleEx.Regular);

// Define page dimensions and margins


double margin = 50;
double rectangleWidth = 200; // Width of the content area
double rectangleHeight = 100; // Height of the content area
double nameHeight = 30;
double addressTop = nameHeight + 35; // Gap between name and
address

// Define and draw name


XRect nameRect = new XRect(margin, margin, rectangleWidth,
nameHeight);
gfx.DrawString(memberName, fontName, XBrushes.Black, nameRect,
XStringFormats.TopLeft);

// Define and draw address


XRect addressRect = new XRect(margin, addressTop, rectangleWidth,
newPage.Height - addressTop - margin);
gfx.DrawString(memberAddress, fontAddress, XBrushes.Black,
addressRect, XStringFormats.TopLeft);

// Define and draw a filled rectangle (analogous to a 'div' with


width and height)
double rectX = margin;
double rectY = margin + nameHeight + 50; // Positioned below the
name
XRect rect = new XRect(rectX, rectY, rectangleWidth,
rectangleHeight);

// Draw rectangle border and fill


gfx.DrawRectangle(XBrushes.LightGray, rect); // Fill with light
gray color
gfx.DrawRectangle(XPens.Black, rect.X, rect.Y, rect.Width,
rect.Height); // Draw border

// Draw text inside the rectangle


string rectangleText = $"{memberName}\n{memberAddress}";
XRect textRect = new XRect(rect.X + 5, rect.Y + 5, rect.Width - 10,
rect.Height - 10); // Padding inside the rectangle

// Define text formatting with word wrapping


XStringFormat textFormat = new XStringFormat
{
Alignment = XStringAlignment.Near,
LineAlignment = XLineAlignment.Near

};

gfx.DrawString(rectangleText, fontRectangleText, XBrushes.Black,


textRect, textFormat);
}

// Load the existing PDF and copy pages to the new PDF document
using (var memoryStream = new MemoryStream(pdfBytes))
{
PdfDocument existingPdfDocument = PdfReader.Open(memoryStream,
PdfDocumentOpenMode.Import);
foreach (PdfPage page in existingPdfDocument.Pages)
{
newPdfDocument.AddPage(page);
}
}

// Save the new PDF document with the added page


using (var outputStream = new MemoryStream())
{
newPdfDocument.Save(outputStream, false);
byte[] updatedPdfBytes = outputStream.ToArray();
return Ok(File(updatedPdfBytes, "application/pdf",
document.Filename));
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while processing the request.");
return StatusCode(500, "Internal server error");
}

You might also like