我一直在尝试解决这个问题一段时间,但无济于事。我在 iTextSharp 中有一些文本,我正在尝试换行。我已经尝试使用\n
转义字符Environment.NewLine
, 并且document.Add(new Phrase(Environment.NewLine))
没有任何成功。那么有没有办法做到这一点?这是我试图在其中执行的代码片段(注意用 注释的行//Doesn't work
):
//Open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
//Configure the content
PdfContentByte cb = writer.DirectContent;
// select the font properties
BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(bf, 10);
//Write the text here
cb.BeginText();
text = "F\n";//Doesn’t work
document.Add(new Phrase(Environment.NewLine));//Doesn’t work
text += "o\n";
text += Environment.NewLine;//Doesn’t work
text += "o\n";
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 85, 311, 0);
cb.EndText();
//Create the new page
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
//Close all streams
document.Close();
fs.Close();
writer.Close();
reader.Close();
有什么建议么?
编辑一:
仍然无法使用document.Add(new Paragraph("\n"));
. 我做错了吗?
cb.BeginText();
text = "F";
document.Add(new Paragraph("\n"));
text += "o";
document.Add(new Paragraph("\n"));
text += "o";
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 85, 311, 0);
cb.EndText();