如何使用 iTextSharp 限制 PDF 页面上段落的宽度?
How can I restrict the width of a paragraph on a PDF page using iTextSharp?
我想使用 iTextSharp 在 PDF 文档中插入一个段落(将自动换行多行),但我想将段落的宽度限制在页面的左半部分。我看不到 "width" 属性 段落 class,但肯定有办法做到这一点,stimmt?
更新
假设的答案对我不起作用,因为它使用了 iText (Java) 在 iTextSharp (C#) 中显然不可用的东西。具体来说(首先,可能还有更多):
ct.setSimpleColumn(myText, 60, 750, document.getPageSize().getWidth()
虽然*Sharp(大写首字母's')有"SetSimpleColumn",但没有"GetPageSize"。
更新 2
我开始认为我真正需要做的可能是按照建议创建一个 'borderless table',如 "BestiTextQuestionsOnWhosebugFull.pdf"
中所述
这是一种实现方式 - 无边框单行 table,WidthPercentage 设置为 50,Horizontal Alignment 发送到 Left:
using (var ms = new MemoryStream())
{
using (var doc = new Document(PageSize.A4, 50, 50, 25, 25)) {
//Create a writer that's bound to our PDF abstraction and our stream
using (var writer = PdfWriter.GetInstance(doc, ms))
{
//Open the document for writing
doc.Open();
var courier9RedFont = FontFactory.GetFont("Courier", 9, BaseColor.RED);
var importantNotice = new Paragraph("Sit on a potato pan Otis - if you don't agree that that's the best palindrome ever, I will sic Paladin on you, or at least say, 'All down but nine - set 'em up on the other alley, pard'", courier9RedFont);
importantNotice.Leading = 0;
importantNotice.MultipliedLeading = 0.9F; // reduce the width between lines in the paragraph with these two settings
// Add a single-cell, borderless, left-aligned, half-page, table
PdfPTable table = new PdfPTable(1);
PdfPCell cellImportantNote = new PdfPCell(importantNotice);
cellImportantNote.BorderWidth = PdfPCell.NO_BORDER;
table.WidthPercentage = 50;
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(cellImportantNote);
doc.Add(table);
doc.Close();
}
var bytes = ms.ToArray();
String PDFTestOutputFileName = String.Format("iTextSharp_{0}.pdf", DateTime.Now.ToShortTimeString());
PDFTestOutputFileName = PDFTestOutputFileName.Replace(":", "_");
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), PDFTestOutputFileName);
File.WriteAllBytes(testFile, bytes);
MessageBox.Show(String.Format("{0} written", PDFTestOutputFileName));
}
}
我想使用 iTextSharp 在 PDF 文档中插入一个段落(将自动换行多行),但我想将段落的宽度限制在页面的左半部分。我看不到 "width" 属性 段落 class,但肯定有办法做到这一点,stimmt?
更新
假设的答案对我不起作用,因为它使用了 iText (Java) 在 iTextSharp (C#) 中显然不可用的东西。具体来说(首先,可能还有更多):
ct.setSimpleColumn(myText, 60, 750, document.getPageSize().getWidth()
虽然*Sharp(大写首字母's')有"SetSimpleColumn",但没有"GetPageSize"。
更新 2
我开始认为我真正需要做的可能是按照建议创建一个 'borderless table',如 "BestiTextQuestionsOnWhosebugFull.pdf"
中所述这是一种实现方式 - 无边框单行 table,WidthPercentage 设置为 50,Horizontal Alignment 发送到 Left:
using (var ms = new MemoryStream())
{
using (var doc = new Document(PageSize.A4, 50, 50, 25, 25)) {
//Create a writer that's bound to our PDF abstraction and our stream
using (var writer = PdfWriter.GetInstance(doc, ms))
{
//Open the document for writing
doc.Open();
var courier9RedFont = FontFactory.GetFont("Courier", 9, BaseColor.RED);
var importantNotice = new Paragraph("Sit on a potato pan Otis - if you don't agree that that's the best palindrome ever, I will sic Paladin on you, or at least say, 'All down but nine - set 'em up on the other alley, pard'", courier9RedFont);
importantNotice.Leading = 0;
importantNotice.MultipliedLeading = 0.9F; // reduce the width between lines in the paragraph with these two settings
// Add a single-cell, borderless, left-aligned, half-page, table
PdfPTable table = new PdfPTable(1);
PdfPCell cellImportantNote = new PdfPCell(importantNotice);
cellImportantNote.BorderWidth = PdfPCell.NO_BORDER;
table.WidthPercentage = 50;
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(cellImportantNote);
doc.Add(table);
doc.Close();
}
var bytes = ms.ToArray();
String PDFTestOutputFileName = String.Format("iTextSharp_{0}.pdf", DateTime.Now.ToShortTimeString());
PDFTestOutputFileName = PDFTestOutputFileName.Replace(":", "_");
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), PDFTestOutputFileName);
File.WriteAllBytes(testFile, bytes);
MessageBox.Show(String.Format("{0} written", PDFTestOutputFileName));
}
}