我已经试着解决这个问题有一段时间了,但没有结果。我在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();
发布于 2012-05-25 14:51:49
在iTextSharp中处理文本的主要方法有两种,一种是通过抽象(如Paragraph
和Phrase
),另一种是通过使用PdfContentByte
手动执行命令。抽象将处理诸如边距、换行和间距等事情,而手动路径则由您决定。你不能把这两者混为一谈,这就是你要做的。我强烈建议使用抽象而不是手动路由,除非您有特定的粒度控制需求。下面是一个显示两者的样本。
但是要明确地回答您的问题,原始PDF命令(您正在使用)从左到右在特定的x,y
坐标上绘制文本,它们不支持“返回”或“换行”的概念。为此,需要手动将当前文本光标移动到新行。有关这方面的示例,请参阅以下代码。
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document doc = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//This creates two lines of text using the iTextSharp abstractions
doc.Add(new Paragraph("This is Paragraph 1"));
doc.Add(new Paragraph("This is Paragraph 2"));
//This does the same as above but line spacing needs to be calculated manually
PdfContentByte cb = writer.DirectContent;
cb.SaveState();
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12f);
cb.BeginText();
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is cb1", 20, 311, 0);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is cb2", 20, 291, 0);//Just guessing that line two should be 20px down, will actually depend on the font
cb.EndText();
cb.RestoreState();
doc.Close();
}
}
}
发布于 2013-12-17 07:29:25
试着做这样的事情:
document.Add(new Chunk("\n"));
发布于 2013-02-08 21:42:37
document.Add(new Paragraph(" "));
对我来说很好。记住,Paragraph
语句会自动添加一个行提要。你所要做的就是给它一些渲染的东西。在这种情况下,一个空间就行了。
https://stackoverflow.com/questions/10756171
复制相似问题