继续引用spire的dll。
1.代码如下:
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("wen.pdf");
PdfPageBase page = doc.Pages[doc.Pages.Count-1];
System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 9, FontStyle.Regular);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font, true);
page.Canvas.DrawString("请在此输入建议:", trueTypeFont, new PdfSolidBrush(Color.Black), 80, 180);
doc.SaveToFile("MyFirstPDF.pdf");
//page.Canvas.DrawString("请在此输入建议:",
//new PdfFont(PdfFontFamily.Helvetica, 9f),
//new PdfSolidBrush(Color.Black), 80, 180);
//doc.SaveToFile("MyFirstPDF.pdf");//如果用此中文乱码
doc.Close();
2.进阶:
问题情境:要修改的地方原先就有东西,需要删除再覆盖:
解决办法:先添加白色brush矩形覆盖,再进行添加内容
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("orignal.pdf");
PdfPageBase page = doc.Pages[doc.Pages.Count - 1];
RectangleF rectangle = new RectangleF(80,180,500,500);
PdfBrush brush = PdfBrushes.White;
page.Canvas.DrawRectangle(brush, rectangle);
System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 12, FontStyle.Regular, GraphicsUnit.World);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font, true);
page.Canvas.DrawString(editStr, trueTypeFont, new PdfSolidBrush(Color.Black), 80, 180);
doc.SaveToFile("now.pdf");
doc.Close();
3.后加textBox获取换行符
private string getEditString(string strOrig)
{
int numL = 40;
string editStr = strOrig;
string[] strs = editStr.Split(new string[1] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
editStr = "";
for (int i = 0; i < strs.Length; i++)
{
float numC = 0F;
int addT = 0;
for (int k = 0; k < strs[i].Length; k++)
{
try
{
if (!Regex.IsMatch(strs[i].Substring(k, 1), @"[a-zA-Z0-9]"))
{
numC += 1;
}
else
{
numC += 0.5f;
addT++;
}
if (numC > numL)
{
break;
}
}
catch(Exception eeee)
{
throw eeee;
}
}
if (numC > numL)
{
string str = strs[i].Insert(numL - 1 + addT / 2, "\r\n");
editStr += getEditString(str);
continue;
}
else
{
editStr += strs[i] + "\r\n";
}
}
return editStr;
}
附加:
1.行间距设置:PdfStringFormat stringFormat = new PdfStringFormat();
stringFormat.LineSpacing = 16f;