使用 itextsharp(asp.net,C#) 在 pdf 中的图像上方和下方添加文本
Add text above and below an image in a pdf using itextsharp(asp.net,C#)
我已经使用 memorystream 将图像转换为 byte[]
,然后使用 itextsharp 将 byte[]
添加到 pdf。
现在我的要求是在图片上方添加一段文字,提供一些关于图片的信息。
这是我的代码
private void generatepdf(byte[] byteImage)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(byteImage);
image.ScalePercent(0.3f * 100);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
Document document = new Document(PageSize.A4, 188f, 88f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
document.Add(image);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
}
如何添加一些文本并将其转换为字节,然后将其发送到 pdf 对象。或者有没有更好的方法来指导我正确的方向。
你可以这样试试
private void generatepdf(byte[] byteImage)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(byteImage);
image.ScalePercent(0.3f * 100);
using (MemoryStream memoryStream = new System.IO.MemoryStream())
{
Document document = new Document(PageSize.A4, 188f, 88f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
string text1 = "before image";
Paragraph text1Title = new Paragraph(text1);
string text2 = "after image";
Paragraph text2Title = new Paragraph(text2);
document.Open();
document.Add(text1Title);
document.Add(image);
document.Add(text2Title);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
}
}
我已经使用 memorystream 将图像转换为 byte[]
,然后使用 itextsharp 将 byte[]
添加到 pdf。
现在我的要求是在图片上方添加一段文字,提供一些关于图片的信息。
这是我的代码
private void generatepdf(byte[] byteImage)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(byteImage);
image.ScalePercent(0.3f * 100);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
Document document = new Document(PageSize.A4, 188f, 88f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
document.Add(image);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
}
如何添加一些文本并将其转换为字节,然后将其发送到 pdf 对象。或者有没有更好的方法来指导我正确的方向。
你可以这样试试
private void generatepdf(byte[] byteImage)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(byteImage);
image.ScalePercent(0.3f * 100);
using (MemoryStream memoryStream = new System.IO.MemoryStream())
{
Document document = new Document(PageSize.A4, 188f, 88f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
string text1 = "before image";
Paragraph text1Title = new Paragraph(text1);
string text2 = "after image";
Paragraph text2Title = new Paragraph(text2);
document.Open();
document.Add(text1Title);
document.Add(image);
document.Add(text2Title);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
}
}