我正在尝试使用itextsharp来获取动态信息并使用它生成PDF .. PDF在每个页面上使用不同的背景图像,文本内容位于需要使用contentByte帮助器的位置。无论如何,这个计划。当我尝试添加另一个页面然后将图像和文本放到该页面上时,我挂断了...我的第一页最终显示应该在我的第二页上的图像,并且第一页图像根本无法显示在任一页面上......到目前为止,我的代码如下:
Function ID_and_Parking(ByVal id As Integer) As ActionResult
Dim _reg_info As reg_info = db.reg_info.Single(Function(r) r.id = id)
Dim _conf_info As conf_info = db.conf_info.Single(Function(f) f.id = 0)
Dim _name As String = String.Empty
If Not String.IsNullOrWhiteSpace(_reg_info.name_tag_pref) Then
_name = _reg_info.name_tag_pref
Else
_name = _reg_info.first_name + " " + _reg_info.last_name
End If
Dim _LastName As String = _reg_info.last_name
Dim _Employer As String = _reg_info.business_name
Dim _Class_1 As String = _reg_info.tues_class
Dim _Class_2 As String = _reg_info.wed_class
Dim _Class_3 As String = _reg_info.thur_class
Dim _Class_4 As String = _reg_info.fri_class
Dim _BeginDate As String = _conf_info.conf_start_date
Dim _endDate As String = _conf_info.conf_end_date
Dim _dates As String = _BeginDate + "-" + _endDate
If IsDBNull(_reg_info.tues_class) Then
_Class_1 = ""
End If
If IsDBNull(_reg_info.wed_class) Then
_Class_2 = ""
End If
If IsDBNull(_reg_info.thur_class) Then
_Class_3 = ""
End If
If IsDBNull(_reg_info.fri_class) Then
_Class_4 = ""
End If
Dim pdfpath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\PDF_Files\"
Dim imagepath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\PDF_Files\"
Dim _PdfName As String = _LastName + ".pdf"
Dim doc As New Document
doc.SetPageSize(iTextSharp.text.PageSize.LETTER)
doc.SetMargins(0, 0, 0, 0)
Dim _PnameFont As iTextSharp.text.Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 18, iTextSharp.text.Font.NORMAL)
Dim BF_Times As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False)
Dim _Parking_Name As New Font(BF_Times, 18, Font.NORMAL, BaseColor.BLACK)
Dim _Parking_Date As New Font(BF_Times, 24, Font.BOLD, BaseColor.BLACK)
Try
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdfpath + _PdfName, FileMode.Create))
doc.Open()
Dim jpg As Image = Image.GetInstance(imagepath + "/Parking_Pass.jpg")
jpg.Alignment = iTextSharp.text.Image.UNDERLYING
jpg.ScaleToFit(612, 792)
doc.add(jpg)
Dim cb As PdfContentByte = writer.DirectContent
'Render Parking Permit
cb.BeginText()
cb.SetFontAndSize(BF_Times, 16)
cb.SetTextMatrix(145, 135.5)
cb.ShowText(_BeginDate)
cb.EndText()
cb.BeginText()
cb.SetFontAndSize(BF_Times, 16)
cb.SetTextMatrix(429, 135.5)
cb.ShowText(_endDate)
cb.EndText()
Dim _idJpg As Image = Image.GetInstance(imagepath + "/Id_Tag.jpg")
Dim imageWidth As Decimal = _idJpg.Width
Dim imageHeight As Decimal = _idJpg.Height
doc.SetPageSize(iTextSharp.text.PageSize.LETTER)
_idJpg.Alignment = iTextSharp.text.Image.UNDERLYING
_idJpg.ScaleToFit(612, 792)
doc.NewPage()
doc.Add(_idJpg)
cb.BeginText()
cb.SetFontAndSize(BF_Times, 18)
cb.SetTextMatrix(100, 50)
cb.ShowText(_name)
cb.EndText()
cb.BeginText()
cb.SetFontAndSize(BF_Times, 18)
cb.SetTextMatrix(200, 100)
cb.ShowText(_Employer)
cb.EndText()
cb.BeginText()
cb.SetFontAndSize(BF_Times, 18)
cb.SetTextMatrix(300, 150)
cb.ShowText(_Class_1)
cb.EndText()
cb.BeginText()
cb.SetFontAndSize(BF_Times, 18)
cb.SetTextMatrix(310, 50)
cb.ShowText(_Class_2)
cb.EndText()
cb.BeginText()
cb.SetFontAndSize(BF_Times, 18)
cb.SetTextMatrix(320, 50)
cb.ShowText(_Class_3)
cb.EndText()
cb.BeginText()
cb.SetFontAndSize(BF_Times, 18)
cb.SetTextMatrix(330, 50)
cb.ShowText(_Class_4)
cb.EndText()
doc.Close()
Catch dex As DocumentException
Response.Write(dex.Message)
Catch ioex As IOException
Response.Write(ioex.Message)
Catch ex As Exception
Response.Write(ex.Message)
End Try
Return RedirectToAction("showUserPDF", New With {.pdfName = _PdfName})
End Function
我已经遍布每个论坛,但是我发现的所有信息似乎都与我正在寻找的信息有关,或者我可能正在解决这个问题,所有这些都在一起...任何帮助都会非常感谢...
答案 0 :(得分:1)
在你的代码中你得到了这个:
Dim jpg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath + "/Parking_Pass.jpg")
jpg.Alignment = iTextSharp.text.Image.UNDERLYING
jpg.ScaleToFit(612, 792)
但您实际上从未将jpg
添加到doc
答案 1 :(得分:0)
我实际上已经弄清楚了我忽略了我的新页面位置和图片位置的问题...我在新页面之前有第二页图片...感谢您的帮助...如果有其他人需要动态填写上面解决方案的数据图像......