And*_*est 16 c# newline itextsharp
我一直试图解决这个问题一段时间后无济于事.我在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();
Run Code Online (Sandbox Code Playgroud)
有什么建议?
编辑一:
仍然没有合作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();
Run Code Online (Sandbox Code Playgroud)
Chr*_*aas 23
有两种主要的方法与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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Pie*_*ter 14
尝试这样的事情:
document.Add(new Chunk("\n"));
Run Code Online (Sandbox Code Playgroud)
document.Add(new Paragraph(" "));适合我.请记住,该Paragraph语句会自动添加换行符.你所要做的就是给它渲染一些东西.在这种情况下,空间会很好.