ITextSharp.dll,在将图片处理好后没法将图片放入已经处理好的pdf中;
private void pdf() //PDF的相关操作流程
{
string tempFilePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"testpdf\E0105-" + SN.Text + ".pdf"; //生成pdf文件的路径定义
iTextSharp.text.pdf.PdfDocument document = new iTextSharp.text.pdf.PdfDocument();
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\E0105插箱下线测试报告-模板.pdf"); //pdf模板文件定义
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(tempFilePath, FileMode.OpenOrCreate));
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfStamper.FormFlattening = true;
//pdf添加图片
string imagePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "image"; //插入图片的文件路径
//iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath + @"/E0105-" + SN.Text + ".jpg");
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"image\E0105-" + SN.Text + ".jpg");
float percentage = 1;
//这里都是图片最原始的宽度与高度
float resizedWidht = img.Width;
float resizedHeight = img.Height;
//这时判断图片宽度是否大于页面宽度减去也边距,如果是,那么缩小,如果还大,继续缩小,
//这样这个缩小的百分比percentage会越来越小
while (resizedWidht > (document.PageSize.Width - document.LeftMargin - document.RightMargin) * 0.8)
{
percentage = percentage * 0.9f;
resizedHeight = img.Height * percentage;
resizedWidht = img.Width * percentage;
}
while (resizedHeight > (document.PageSize.Height - document.TopMargin - document.BottomMargin) * 0.8)
{
percentage = percentage * 0.9f;
resizedHeight = img.Height * percentage;
resizedWidht = img.Width * percentage;
}
//这里用计算出来的百分比来缩小图片
img.ScalePercent(percentage * 100);
//图片定位,页面总宽283,高416;这里设置0,0的话就是页面的左下角 让图片的中心点与页面的中心店进行重合
img.SetAbsolutePosition(document.PageSize.Width / 2 - resizedWidht / 2, document.PageSize.Height / 2 - resizedHeight / 2);
document.Add(img); //无法将实例放到对象里?????
//
BaseFont simheiBase = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //添加中文显示
pdfFormFields.AddSubstitutionFont(simheiBase);
#region //将数据显示的数值添加至pdf文本域
pdfStamper.AcroFields.SetField("SN", SN.Text);
pdfStamper.AcroFields.SetField("TIME", label9.Text);
pdfStamper.AcroFields.SetField("VOLT", VOLT.Text);
pdfStamper.AcroFields.SetField("MAXV", MAXV.Text);
pdfStamper.AcroFields.SetField("MINV", MINV.Text);
pdfStamper.AcroFields.SetField("MAXT", MAXT.Text);
pdfStamper.AcroFields.SetField("MINT", MINT.Text);
pdfStamper.AcroFields.SetField("VD", CELLVD.Text);
pdfStamper.AcroFields.SetField("TD", TD.Text);
pdfStamper.AcroFields.SetField("V1", CELL1.Text);
pdfStamper.AcroFields.SetField("V2", CELL2.Text);
pdfStamper.AcroFields.SetField("V3", CELL3.Text);
pdfStamper.AcroFields.SetField("V4", CELL4.Text);
pdfStamper.AcroFields.SetField("V5", CELL5.Text);
pdfStamper.AcroFields.SetField("V6", CELL6.Text);
pdfStamper.AcroFields.SetField("V7", CELL7.Text);
pdfStamper.AcroFields.SetField("V8", CELL8.Text);
pdfStamper.AcroFields.SetField("V9", CELL9.Text);
pdfStamper.AcroFields.SetField("V10", CELL10.Text);
pdfStamper.AcroFields.SetField("V11", CELL11.Text);
pdfStamper.AcroFields.SetField("V12", CELL12.Text);
pdfStamper.AcroFields.SetField("V13", CELL13.Text);
pdfStamper.AcroFields.SetField("V14", CELL14.Text);
pdfStamper.AcroFields.SetField("V15", CELL15.Text);
pdfStamper.AcroFields.SetField("V16", CELL16.Text);
pdfStamper.AcroFields.SetField("T1", T1.Text);
pdfStamper.AcroFields.SetField("T2", T2.Text);
pdfStamper.AcroFields.SetField("T3", T3.Text);
pdfStamper.AcroFields.SetField("T4", T4.Text);
pdfStamper.AcroFields.SetField("T5", T5.Text);
pdfStamper.AcroFields.SetField("T6", T6.Text);
pdfStamper.AcroFields.SetField("T7", T7.Text);
pdfStamper.AcroFields.SetField("T8", T8.Text);
#endregion
//****************************************************************************************************************************************
// pdfStamper.AcroFields.SetField("chk", "yes", true);
pdfStamper.Close();
pdfReader.Close();
////解析文本
//PdfReaderContentParser parser = new PdfReaderContentParser(pdfReader);
//PdfWriter pdfWriter;
//ITextExtractionStrategy strategy;
//strategy = parser.ProcessContent(1, new SimpleTextExtractionStrategy());
//string ss = strategy.GetResultantText();
//byte[] buffer = pdfReader.GetPageContent(1);
//string strBody = System.Text.Encoding.Default.GetString(buffer);
}
我想按模板保存另一个pdf,在设置的文本域填充值;在将图片处理好大小放到文件里,再关闭文件;
目前填充值是可以的,一填充图片,就会报错;
document.Add(img);
这行代码打个断点,代码执行到这里时,看下img对象各属性值,哪个有异常
插入图片是用Document而不是PdfDocument
string tempFilePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"testpdf\test1.pdf";
//这里是Document,不是PdfDocument
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(tempFilePath, FileMode.OpenOrCreate));
document.Open();
//添加图片
string imagepath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\image\test.jpg";
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
document.Add(image);
document.Close();