سؤال

أقوم بإنشاء ملف PDF برمجيًا باستخدام iTextSharp وإنشاء ملف PdfPTable.

في بعض الخلايا أحتاج إلى وجود ارتباط تشعبي.

بدون ارتباط تشعبي، يمكنني محاذاة النص في الخلية أفقيًا وعموديًا، ولكن عندما أقوم بإضافة القطعة التي تحتوي على الارتباط التشعبي الخاص بي، تفقد الخلية خاصية المحاذاة الخاصة بها.

هنا هو الكود الخاص بي ...

var webAddress = new Chunk(row["product_code"].ToString(), linkFont);
webAddress.SetAnchor(row["url"].ToString());

PdfPCell ProductCodeCell = new PdfPCell();
ProductCodeCell.AddElement(webAddress);
ProductCodeCell.VerticalAlignment = Element.ALIGN_MIDDLE;
ProductCodeCell.HorizontalAlignment = Element.ALIGN_CENTER;
ProductCodeCell.Padding = 3f;
table.AddCell(ProductCodeCell);

هل يمكن لأحد أن يساعدني في إعادة تنظيم الرابط؟

تشكرات.

هل كانت مفيدة؟

المحلول

يرى هذه الإجابة للمناقشة حول وضع النص عكس الوضع المركب.في الأساس، إذا كنت تستخدم AddElement() تحتاج إلى ضبط المحاذاة على الكائن الخاص بك بدلاً من الخلية نفسها. Chunk ومع ذلك، لا يحتوي على محاذاة، لذلك تحتاج إلى لف القطعة الخاصة بك بشيء يقوم بذلك وهو Paragraph.

var table = new PdfPTable(1);

//Create our chunk with anchor
var webAddress = new Chunk("hello");
webAddress.SetAnchor("http://www.example.net/");

//Create a paragraph holding the chunk and set the alignment on _it_
var para = new Paragraph(webAddress);
para.Alignment = Element.ALIGN_CENTER;

//Create our cell
PdfPCell ProductCodeCell = new PdfPCell();

//Add the paragrph to it
ProductCodeCell.AddElement(para);

//Padding on the cell still works
ProductCodeCell.Padding = 30f;

//Add cell to table
table.AddCell(ProductCodeCell);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top