我有一个问题。我想从Listview表中生成一些图像并将其写入PDF。我曾经使用过ItextSharp,它的工作原理就像一个咒语-但是就现在而言,当用户添加一个依赖项时,它应该从数据库表中获取图像,然后转换为Image并保存为PDF
本部分可以100%
private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
// string query = "select * from Base64TestTableW where id = @id";
string query = "";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@id", id.Text);
using (SqlDataReader rd = cmd.ExecuteReader())
{
if (rd.Read())
{
string base64imgString = rd["base64String"].ToString();
byte[] finalBase64 = Convert.FromBase64String(base64imgString);
using (MemoryStream ms = new MemoryStream(finalBase64))
{
using (Stream inputPdfStream = new FileStream(@"C:\Users\emi\Desktop\Wema K2Service_HMO_Form (3).pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream outputPdfStream = new FileStream(@"C:\Users\emi\Desktop\pdfSharpExample\test2.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(finalBase64);
image.SetAbsolutePosition(491.8f, 550f);
pdfContentByte.AddImage(image);
stamper.Close();
MessageBox.Show("Good");
}
}
}
}
}
}
}
}
现在,当我想在PDF上使用相同的方法来获取其他依赖项时,而不是保存到PDF时,它们似乎相互重叠。此时,有关受抚养人的代码如下:
private void GetChildImagePDFImage()
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
string querychildpic = "select * from child_image where sid = @sid";
using (SqlCommand cmd = new SqlCommand(querychildpic, con))
{
cmd.Parameters.AddWithValue("@sid", id.Text);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
string base64imgString = rdr["base64String"].ToString();
byte[] finalBase64 = Convert.FromBase64String(base64imgString);
using (MemoryStream ms = new MemoryStream(finalBase64))
{
using (Stream inputPdfStream = new FileStream(@"C:\Users\emi\Desktop\Wema K2Service_HMO_Form (3).pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream outputPdfStream = new FileStream(@"C:\Users\emi\Desktop\pdfSharpExample\test2.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(finalBase64);
image.SetAbsolutePosition(450f, 250f);
image.SetAbsolutePosition(450f, 150f);
image.SetAbsolutePosition(450f, 50f);
pdfContentByte.AddImage(image);
stamper.Close();
MessageBox.Show("Good");
}
}
}
}
}
}
}
}
当放入For Loop中时,它会使PDF崩溃,我似乎不明白为什么会这样。
完整代码如下
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BrowseandPutImagePDF
{
public partial class Form1 : Form
{
string constring = @"Data Source=DESKTOP-V2G35NU\SQLEXPRESS;Initial Catalog=base64ImageExample;Integrated Security=True";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Title = "Select A File";
openDialog.Filter = "Text Files (*.txt)|*.txt" + "|" +
"Image Files (*.png;*.jpg)|*.png;*.jpg" + "|" +
"All Files (*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
//textBox1.Text = openDialog.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
// string query = "select * from Base64TestTableW where id = @id";
string query = "";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@id", id.Text);
using (SqlDataReader rd = cmd.ExecuteReader())
{
if (rd.Read())
{
string base64imgString = rd["base64String"].ToString();
byte[] finalBase64 = Convert.FromBase64String(base64imgString);
using (MemoryStream ms = new MemoryStream(finalBase64))
{
using (Stream inputPdfStream = new FileStream(@"C:\Users\emi\Desktop\Wema K2Service_HMO_Form (3).pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream outputPdfStream = new FileStream(@"C:\Users\emi\Desktop\pdfSharpExample\test2.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(finalBase64);
image.SetAbsolutePosition(491.8f, 550f);
pdfContentByte.AddImage(image);
stamper.Close();
MessageBox.Show("Good");
}
}
}
}
}
}
}
}
private void GetChildImagePDFImage()
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
string querychildpic = "select * from child_image where sid = @sid";
using (SqlCommand cmd = new SqlCommand(querychildpic, con))
{
cmd.Parameters.AddWithValue("@sid", id.Text);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
string base64imgString = rdr["base64String"].ToString();
byte[] finalBase64 = Convert.FromBase64String(base64imgString);
using (MemoryStream ms = new MemoryStream(finalBase64))
{
using (Stream inputPdfStream = new FileStream(@"C:\Users\emi\Desktop\Wema K2Service_HMO_Form (3).pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream outputPdfStream = new FileStream(@"C:\Users\emi\Desktop\pdfSharpExample\test2.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(finalBase64);
image.SetAbsolutePosition(450f, 250f);
image.SetAbsolutePosition(450f, 150f);
image.SetAbsolutePosition(450f, 50f);
pdfContentByte.AddImage(image);
stamper.Close();
MessageBox.Show("Good");
}
}
}
}
}
}
}
}
}
}