ฉันต้องการตกแต่งย่อหน้าต่างๆ ในไฟล์ PDF โดยทำให้ข้อความเป็นตัวหนา ใช้แบบอักษรและสีที่แตกต่างกัน ฉันคิดว่าฉันพบโค้ดสำหรับวิธีการทำเช่นนั้นแล้ว (ด้านล่าง) แต่มันใช้งานไม่ได้ - ข้อความทั้งหมดมีแบบอักษร สี (สีดำ) และขนาดเหมือนกัน เหตุใดความพยายามเสมือนจริงของฉันในการปรับแต่งไฟล์ PDF ให้ดูไร้ประโยชน์? นี่คือรหัสของฉัน:
using (var ms = new MemoryStream())
{
using (var doc = new Document(PageSize.A4, 50, 50, 25, 25))
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
doc.Open();
// Mimic the appearance of Duckbill_Platypus.pdf
var docTitle = new Paragraph("Duckbilled Platypi - they're not what's for dinner");
var titleFont = FontFactory.GetFont("Courier", 18, BaseColor.BLACK);
docTitle.Font = titleFont;
doc.Add(docTitle);
var subTitle = new Paragraph("Baby, infant, toddler, and perhaps 'Terrible 2s' Platypi are called Platypups");
var subtitleFont = FontFactory.GetFont("Times Roman", 13, BaseColor.BLACK);
subTitle.Font = subtitleFont;
doc.Add(subTitle);
var importantNotice = new Paragraph("Teenage platypi are sometimes called Platydude[tte]s");
var importantNoticeFont = FontFactory.GetFont("Courier", 13, BaseColor.RED);
importantNotice.Font = importantNoticeFont;
doc.Add(importantNotice);
ListColumns lc;
for (int i = 0; i < listOfListItems.Count; i++)
{
lc = listOfListItems[i];
sb.AppendLine(String.Format(@"<p>Request date is {0}; Payee Name is {1}; Remit Address or Mail Stop is {2}; Last 4 of SSN or ITIN is {3}; 204 Submitted or on file is {4}; Requester Name is {5}; Dept or Div Name is {6}; Phone is {7}; Email is {8}</p>",
lc.li_requestDate, lc.li_payeeName, lc.li_remitAddressOrMailStop, lc.li_last4SSNDigitsOrITIN, lc.li_204SubmittedOrOnFile, lc.li_requesterName, lc.li_deptDivName, lc.li_phone, lc.li_email));
}
String htmlToRenderAsPDF = sb.ToString();
//XMLWorker also reads from a TextReader and not directly from a string
using (var srHtml = new StringReader(htmlToRenderAsPDF))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
doc.Close();
}
}
try
{
var bytes = ms.ToArray();
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "iTextSharpTest.pdf");
File.WriteAllBytes(testFile, bytes);
}
catch (Exception ex)
{
String exMsg = ex.Message;
; // what is "ex" here?
}
ฉันกำลังตั้งค่าแบบอักษรสองขนาด (18 และ 13) สองสี (สีดำและสีแดง) สองแบบอักษร ("Courier" และ "Times Roman") แต่การเดินทางหลายชั่วโมงของฉันที่นี่เพื่อการแปลงไฟล์ PDF ดูเหมือนจะไร้ประโยชน์ (ขออภัย ถึงวาเชล ลินด์ซีย์ และอับราฮัม ลินคอล์น)