我用Visual Studio 2005 C#2.0 Net编写代码。我在PDF文件中读取了Textfile和Textfile的内容。现在我想用Text line替换Textfile中的Text(“---”)。
public void PDFCreate()
{
iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4, 25, 10, 40, 40);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
///########
BaseFont bf = BaseFont.CreateFont(@"C:\WINNT\Fonts\COUR.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.BOLD);
BaseFont bs = BaseFont.CreateFont(@"C:\WINNT\Fonts\COUR.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font s = new iTextSharp.text.Font(bs, 10.2f, iTextSharp.text.Font.NORMAL);
doc.Open();
PdfContentByte cb = writer.DirectContent;
string myfiles = MySetIniPath + feld[0] + "_rus." + feld[1];
string str;
StreamReader myfi = new StreamReader(myfiles);
{
while ((str = myfi.ReadLine()) != null)
{
if (str.Contains("MIMO"))
doc.NewPage();
if (str != "")
{
if (str.StartsWith("результат теста"))
{
doc.Add(new Phrase(str, s));
doc.Add(new Phrase("\n\n", s));
continue;
}
if (str.StartsWith("Nummer"))
{
doc.Add(new Phrase(str, f));
doc.Add(new Phrase("\n\n", f));
continue;
}
if (str.StartsWith("MIMO") ||
str.StartsWith("Serial") ||
str.StartsWith("Numbers") ||
str.StartsWith("order"))
{
doc.Add(new Phrase(str + "\n", f));
continue;
}
if (str.StartsWith("---"))
{
//Draw a line
continue;
}
doc.Add(new Phrase(str + "\n", s));
}
}
}
doc.Close();
myfi.Close();
}
我怎样写(REPLACE)if(str.StartsWith("----"))
使用itextsharp或e.Graphics.MeasureString(" ", boldFont)
答案 0 :(得分:0)
您可以使用string.Replace和DrawString
var text = "----- other text";
var yourValue =e.Graphics.DrawString();//Adjust your drawstring
var result = text.Replace("-----", yourValue);
答案 1 :(得分:0)
这就是解决方案:
if (str.IndexOf("|") > -1)
{
char vertLine = '\u2502';
str = str.Replace("|", vertLine.ToString());
doc.Add(new Phrase(str + "\n", s));
continue;
}
if (str.StartsWith("---"))
{
char vert = '\u2500';
str = str.Replace("--", vert.ToString());
doc.Add(new Phrase(str + str +"\n", s));
continue;
}