在添加一个iTextSharp的新线 [英] Adding a New Line in iTextSharp

查看:291
本文介绍了在添加一个iTextSharp的新线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想现在就解决这个问题了一会儿无济于事。我有一些文字iTextSharp的我试图把一个换行符。我已经使用 \\\
转义字符试过, Environment.NewLine 文件。新增(新乐句(Environment.NewLine))没有任何成功。那么,有没有办法做到这一点?这里是我想要做的(注意与 //不工作注释行)一块我的代码:

  //打开读者
PdfReader读卡器=新PdfReader(oldfile目录);
矩形大小= reader.GetPageSizeWithRotation(1);
文档的文档=新的文件(大小);
//打开作家
的FileStream FS =新的FileStream(NEWFILE,FileMode.Create,FileAccess.Write);
PdfWriter作家= PdfWriter.GetInstance(文件,FS);
document.Open();

//配置内容
PdfContentByte CB = writer.DirectContent;
//选择字体属性
BASEFONT BF = BaseFont.CreateFont(C:\\windows\\fonts\\calibri.ttf,BaseFont.CP1252,BaseFont.NOT_EMBEDDED );
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(BF,10);

//这里$ B $写入化文本B cb.BeginText();
文本=F\\\
; //不行的
document.Add(新乐句(Environment.NewLine)); //不行的
文字+ =上;
文字+ = Environment.NewLine; //不行的
文字+ =o\\\
;
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT,文本,85,311,0);
cb.EndText();

//创建新页面
PdfImportedPage页= writer.GetImportedPage(读卡器,1);
cb.AddTemplate(页面,0,0);

//关闭所有流
document.Close();
fs.Close();
writer.Close();
reader.Close();

有什么建议?



编辑一个:



还没与工作document.Add(新段(\\\
));
。 ?难道我这样做不对

  cb.BeginText(); 
文本=F;
document.Add(新段(\\\
));
文字+ =O;
document.Add(新段(\\\
));
文字+ =O;
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT,文本,85,311,0);
cb.EndText();


解决方案

有两个主要的iTextSharp的文本工作的方式,无论是通过像段落和抽象短语或通过手动执行命令的 PdfContentByte 。而手动路线是一切都取决于你的抽象会处理之类的东西利润率,换行符和间距。你真的不能将两者搅和这是你在做什么。我强烈建议使用抽象而不是手动路线,除非你有精细的控制特定需要。以下是同时显示了一个样本。



但具体回答你的问题,生PDF命令(您正在使用),在特定的绘制文本点¯x ,Y 从左至右协调和他们不支持收益或换行的概念。要做到这一点,你需要将当前文本光标手动移动到新行。请参见下面的代码的一个样本。

 字符串OUTPUTFILE = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop ),检验.pdf); 
使用(的FileStream FS =新的FileStream(OUTPUTFILE,FileMode.Create,FileAccess.Write,FileShare.None)){
使用(DOC文档=新的文件(PageSize.LETTER)){
使用(PdfWriter作家= PdfWriter.GetInstance(DOC,FS)){
doc.Open();

//这将创建使用iTextSharp的两行文字的抽象
doc.Add(新段(这是第1款));
doc.Add(新段(这是第2款));

//这确实与上述相同,但行距需要计算手动
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,这是CB1,20,311,0);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT,这是CB2,20,291,0); //只是猜测该行两个技术应该为20px下来,实际上将取决于字体
cb.EndText ();
cb.RestoreState();
doc.Close();
}
}
}


I’ve been trying to solve this problem for a while now to no avail. I have some text in iTextSharp I’m trying to put on a newline. I’ve tried using the \n escape character, Environment.NewLine, and document.Add(new Phrase(Environment.NewLine)) without any success. So is there a way to do this? Here is the piece of my code I’m trying to do it in (note the lines commented with //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();

Any suggestions?

Edit One:

Still not working with document.Add(new Paragraph("\n"));. Did I do it wrong?

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();

解决方案

There's two main ways to work with text in iTextSharp, either through the abstractions like Paragraph and Phrase or by manually executing commands using a PdfContentByte. The abstractions will handle things like margins, line breaks and spacing while the manual route is all up to you. You can't really mix the two which is what you are doing. I'd highly recommend using the abstractions instead of the manual route unless you have a specific need for granular control. Below is a sample showing both off.

But to answer your question specifically, raw PDF commands (which you are using) draw text at certain x,y coordinates from left to right and they do not support the concept of "returns" or "line breaks". To do this you need to manually move the current text cursor to a new line. See the code below for a sample of that.

        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();
                }
            }
        }

这篇关于在添加一个iTextSharp的新线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆