我已经尝试解决这个问题已有一段时间了,但无济于事。我要在iTextSharp中输入一些文本,我想换行。我尝试使用\n转义字符,Environment.NewLinedocument.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();

最佳答案

在iTextSharp中使用文本的主要方法有两种,要么通过像ParagraphPhrase这样的抽象方法,要么通过使用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();
                }
            }
        }

关于c# - 在iTextSharp中添加新行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10756171/

10-12 05:05