C# 如何使用iTextSharp限制PDF页面上段落的宽度?
我想使用iTextSharp在PDF文档中插入一个段落(将word环绕几行),但我想将段落的宽度限制在页面的左半部分。我没有看到段落类的“宽度”属性,但肯定有办法做到这一点,stimmt 使现代化 假设的答案对我来说不起作用,因为它使用了iText(Java)中显然无法使用的东西。具体而言(首先,可能还有更多): 虽然*Sharp(大写字母“s”)有一个“SetSimpleColumn”,但没有“GetPageSize” 更新2C# 如何使用iTextSharp限制PDF页面上段落的宽度?,c#,pdf,pdf-generation,itextsharp,C#,Pdf,Pdf Generation,Itextsharp,我想使用iTextSharp在PDF文档中插入一个段落(将word环绕几行),但我想将段落的宽度限制在页面的左半部分。我没有看到段落类的“宽度”属性,但肯定有办法做到这一点,stimmt 使现代化 假设的答案对我来说不起作用,因为它使用了iText(Java)中显然无法使用的东西。具体而言(首先,可能还有更多): 虽然*Sharp(大写字母“s”)有一个“SetSimpleColumn”,但没有“GetPageSize” 更新2 我开始认为,我真正需要做的可能是按照建议创建一个“无边界表格”,如
我开始认为,我真正需要做的可能是按照建议创建一个“无边界表格”,如“BestitextQuestionStackOverflowFull.pdf”中所述,这是一种方法-一个无边界、单行表格,宽度百分比设置为50,水平对齐发送到左侧:
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));
}
}
最简单的方法可能是使用无边框的表。您需要调整单元格填充和其他一些事情,但这将允许您“Document.Add()”它。如果这对你不起作用,那么正如@OneFineDay所说的那样,你需要创建自己的矩形来控制它,并使用请阅读,例如,你会发现你最近发布的许多问题都是重复的。例如:这个问题可能是它的重复,也可能是定义自定义左边距那么简单;我下载了“StackOverflow上的最佳iText问题”示例;完整版要大多少?关于您对问题的更新:iText中的getter/setter主要是iTextSharp中的属性
document.getPageSize().getWidth()
变为document.PageSize.Width
。
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));
}
}