现使用iTextSharp更改原有pdf内容,主要就是在指定位置加几个字符,之前是生成一个图片贴到原有pdf指定位置,可以实现,但现在经理要一定添加为文字。 我看了之前使用的添加图片的类里好像没有添加文字的有知道的朋友请帮下忙,急啊~~~~ 附上之前的代码。 (注意:是修改原有的PDF,不是生成新的!!!)
private void ConvertPDFToPDF(string filePath, string toPath)
{
PdfReader reader = new PdfReader(filePath);
int n = reader.NumberOfPages;
PdfStamper stamp = new PdfStamper(reader, new FileStream(toPath, FileMode.Create));
int i = 0;
PdfContentByte under = null; string text = hfEventCode.Value;
int width = text.Length * 16 > 40 ? text.Length * 16 : 40; System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, 18);
SetBgColor(image, Color.White);
Graphics g = Graphics.FromImage(image); System.Drawing.Font font = new System.Drawing.Font("Arial", 9, (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Black, 1.2F, true);
g.DrawString(text, font, brush, 2, 2); MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] img = ms.ToArray();
iTextSharp.text.Image mm = iTextSharp.text.Image.GetInstance(img);
mm.SetAbsolutePosition(155, 478);
mm.ScaleAbsolute(width, 18);
while (i < n)
{
i++;
under = stamp.GetOverContent(i);
under.AddImage(mm);
}
stamp.Close();
reader.Close(); ms.Close();
ms.Dispose();
}
private void ConvertPDFToPDF(string filePath, string toPath)
{
PdfReader reader = new PdfReader(filePath);
int n = reader.NumberOfPages;
PdfStamper stamp = new PdfStamper(reader, new FileStream(toPath, FileMode.Create));
int i = 0;
PdfContentByte under = null; string text = hfEventCode.Value;
int width = text.Length * 16 > 40 ? text.Length * 16 : 40; System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, 18);
SetBgColor(image, Color.White);
Graphics g = Graphics.FromImage(image); System.Drawing.Font font = new System.Drawing.Font("Arial", 9, (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Black, 1.2F, true);
g.DrawString(text, font, brush, 2, 2); MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] img = ms.ToArray();
iTextSharp.text.Image mm = iTextSharp.text.Image.GetInstance(img);
mm.SetAbsolutePosition(155, 478);
mm.ScaleAbsolute(width, 18);
while (i < n)
{
i++;
under = stamp.GetOverContent(i);
under.AddImage(mm);
}
stamp.Close();
reader.Close(); ms.Close();
ms.Dispose();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货