创建第二页具有不同背景图像的 2 页 PDF
我正在尝试使用 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
我已经遍及了所有有关此问题的论坛,但我发现的所有信息似乎都与我正在寻找的内容不符,或者也许我只是在讨论这个错误全部都在一起......任何帮助将不胜感激......
I am trying to use itextsharp to take dynamic information and generate a PDF with it.. The PDF's use a background Image on each page which is different and the text content is positioned where it needs to be using the contentByte helper. Thats the plan anyways. I hit a hang up when I tried to add another page and then drop the image and text onto that page... My first page ends up with the image that should be on my second page and the first page image fails to display at all on either page... My code so far is as follows:
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
I have been all over every forum about this but all of the information I have found seems to be off from what I am looking for, OR maybe I am just going about this the wrong was all together... Any help would be greatly appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码中,您有这样的内容:
但您实际上从未将
jpg
添加到doc
In your code you've got this:
But you are never actually adding
jpg
to thedoc
我实际上已经弄清楚了我忽略了新页面的位置和图像位置的问题...我在新页面之前有第二页图像...感谢您的帮助...如果其他人需要动态填充图像数据上述解决方案有效...
I actually have figured out the issue I overlooked my placement of the newpage and the image placement... I had the 2nd page image before the newpage... Thanks for your help... If anyone else neededs to dynamically fill a image with data the above solution works...