2012-05-25 41 views
11

我一直试图解决这个问题一段时间无济于事。我在iTextSharp中有一些文本我正试图换上一个换行符。我试过使用\n转义字符,Environment.NewLinedocument.Add(new Phrase(Environment.NewLine)),但没有取得任何成功。那么有没有办法做到这一点?这里是一块我的代码,我试图做到这一点的(注意与//Doesn't work注释行):在iTextSharp中添加新行

//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(); 
+0

你知道如何使用Itextsharp在pdf上创建单选按钮列表 –

回答

22

有两种主要的方法与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(); 
       } 
      } 
     } 
+0

谢谢你的澄清! –

+0

很好的解释Chris,谢谢。 – Kashif

+0

感谢您的好回答 –

3
document.Add(new Paragraph("\n")); 

编辑:

cb.BeginText(); 
string text = ""; 
text = "F\n";   
text += "o\n";    
text += "o"; 
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); 

Paragraph p = new Paragraph(text); 
document.Add(p); 

//Close all streams 
document.Close(); 
fs.Close(); 
writer.Close(); 
reader.Close(); 
+0

仍然显示一行,请参阅编辑...也许我做错了吗? –

+1

安德鲁现在尝试。 – Kashif

+0

尽管如此,如果您检查Chris的答案,这可能是iText软件的技术限制。尽管如此,我感谢你的帮助! –

4

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

+0

这个对我来说也很好。谢谢 – SajuPK

9

尝试这样:

document.Add(new Chunk("\n")); 
+1

小心解释你的解决方案是做什么的?这是一个质量很低的帖子。 – thomaux

+2

@Anzeno它在看起来并添加一个新的线夹字符()与文件。没有太多解释。 – JSON

0

我只是想这个工具,并添加新行我只是说“\ r \ n”和它的工作。 像下面这样。

String content01 = "Nulla condimentum dui lobortis risus viverra, eu pellentesque sem blandit.\r\nUt congue turpis quis sapien mollis, vitae rutrum mi consectetur. Integer maximus nisl sed tellus pharetra pharetra."; 
Phrase contentPhrase = new Phrase(content01); 
Paragraph contentPr = new Paragraph(); 
contentPr.Add(contentPhrase); 

然后将段落contentPtr添加到我的Document实例。

0

我知道这有点旧,但还有另一种方法。以下是我用过的一份报告。

  var contents = new Paragraph(); 
      contents.Alignment = Element.ALIGN_CENTER; 

      contents.Add(new Chunk(string.Format("{0} {1}\n", emp.FirstName, emp.LastName), new Font(baseFont, 11f, Font.BOLD))); 
      contents.Add(new Chunk(string.Format("Dept: {0}\n", emp.Departments.Title), new Font(baseFont, 9f))); 
      contents.Add(new Chunk(string.Format("Wk Ending: {0}\n", _date), new Font(baseFont, 9f))); 

正如你所看到的,我做的是创建块。这使您可以使用“\ n”来预制换行符。

0

随着间距attriubtes的添加,您可以精确设置中断的高度...

var spacerParagraph = new Paragraph(); 
spacerParagraph.SpacingBefore = 4f; 
spacerParagraph.SpacingAfter = 0f; 
document.Add(spacerParagraph);