首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在iTextSharp中添加一条新行

在iTextSharp中添加一条新行
EN

Stack Overflow用户
提问于 2012-05-25 14:25:45
回答 10查看 85.6K关注 0票数 18

我已经试着解决这个问题有一段时间了,但没有结果。我在iTextSharp上有一些短信,我正试着写一条换行符。我尝试过使用\n转义字符、Environment.NewLinedocument.Add(new Phrase(Environment.NewLine)),但没有成功。有办法吗?下面是我要做的代码的一部分(请注意用//Doesn't work注释的行):

代码语言:javascript
复制
//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"));合作。我做错了吗?

代码语言:javascript
复制
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();
EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2012-05-25 14:51:49

在iTextSharp中处理文本的主要方法有两种,一种是通过抽象(如ParagraphPhrase ),另一种是通过使用PdfContentByte手动执行命令。抽象将处理诸如边距、换行和间距等事情,而手动路径则由您决定。你不能把这两者混为一谈,这就是你要做的。我强烈建议使用抽象而不是手动路由,除非您有特定的粒度控制需求。下面是一个显示两者的样本。

但是要明确地回答您的问题,原始PDF命令(您正在使用)从左到右在特定的x,y坐标上绘制文本,它们不支持“返回”或“换行”的概念。为此,需要手动将当前文本光标移动到新行。有关这方面的示例,请参阅以下代码。

代码语言:javascript
复制
        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();
                }
            }
        }
票数 26
EN

Stack Overflow用户

发布于 2013-12-17 07:29:25

试着做这样的事情:

代码语言:javascript
复制
document.Add(new Chunk("\n"));
票数 16
EN

Stack Overflow用户

发布于 2013-02-08 21:42:37

document.Add(new Paragraph(" "));对我来说很好。记住,Paragraph语句会自动添加一个行提要。你所要做的就是给它一些渲染的东西。在这种情况下,一个空间就行了。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10756171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档