Asked by:
Merge two document with iTextSharp

Question
-
User31164289 posted
I'm using iTextsharp to creating pdf. My code read a template file and write on it. Now I need to add to PDF a second page using another template file. Not found any sample to do it. How I can do it?
Response.Charset = "utf-8" Response.AddHeader("Last-Modified", DateTime.Now.ToString("r")) Response.CacheControl = "public" Response.AddHeader("Content-Disposition", "attachment; filename=Attestato.pdf") Response.ContentType = "application/pdf" Dim outputPdfStream As Stream = Response.OutputStream Dim strTemplateStampa As String Dim ListaMarche As List(Of String) = Prot.ListaMarche Dim ListaModelli As List(Of String) = Prot.ListaModelli Dim NrAttestato As String = Prot.NrAttestato Dim IdProd As Integer = Prot.GetIdProduttore Dim Produttore As Produttore = Produttore.Carica(IdProd) Dim myProd As Produttore = Produttore.Carica(IdProd) strTemplateStampa = Server.MapPath("~/Template/Template.pdf") Dim reader As New iTextSharp.text.pdf.PdfReader(strTemplateStampa) Dim mb As iTextSharp.text.Rectangle = reader.GetPageSize(1) Dim stamper As New iTextSharp.text.pdf.PdfStamper(reader, outputPdfStream) Dim bf As iTextSharp.text.pdf.BaseFont = iTextSharp.text.pdf.BaseFont.CreateFont(Request.PhysicalApplicationPath + "Fonts" + "\Arial.ttf", iTextSharp.text.pdf.BaseFont.WINANSI, iTextSharp.text.pdf.BaseFont.EMBEDDED) Dim cb As iTextSharp.text.pdf.PdfContentByte = stamper.GetOverContent(1) WriteToPdf(cb, bf, 10, 125, 150, NrAttestato) Dim riga As Integer = 440 For Each MarcaProt As String In ListaMarche WriteToPdf(cb, bf, 8, 60, riga, MarcaProt) riga += 11 Next riga = 440 For Each ModelloProt As String In ListaModelli WriteToPdf(cb, bf, 8, 300, riga, ModelloProt) riga += 11 Next WriteToPdf(cb, bf, 8, 120, 645, Prot.Protocollo) WriteToPdf(cb, bf, 16, Diff, 340, Prot.GetName) stamper.Close() Response.End()
Monday, October 15, 2018 7:44 PM
All replies
-
User283571144 posted
Hi clembo67,
Since you don't provide the WriteToPdf and Produttore.Carica method, I couldn't use your codes to merge two pdf.
I have merged the two pdf by using below codes, you could refer to it.
Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.pdf Public Class MergePDF Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim arr As String() = {"D:/Key.pdf", "D:/Key2.pdf"} MergePDFs(arr) End Sub Private Sub MergePDFs(ParamArray filesPath As String()) Response.Charset = "utf-8" Response.AddHeader("Last-Modified", DateTime.Now.ToString("r")) Response.CacheControl = "public" Response.AddHeader("Content-Disposition", "attachment; filename=Attestato.pdf") Response.ContentType = "application/pdf" Dim outputPdfStream As Stream = Response.OutputStream Dim readerList As List(Of PdfReader) = New List(Of PdfReader)() For Each filePath As String In filesPath Dim pdfReader As PdfReader = New PdfReader(filePath) readerList.Add(pdfReader) Next Dim document As Document = New Document(PageSize.A4, 0, 0, 0, 0) Dim writer As PdfWriter = PdfWriter.GetInstance(document, outputPdfStream) document.Open() For Each reader As PdfReader In readerList For i As Integer = 1 To reader.NumberOfPages Dim page As PdfImportedPage = writer.GetImportedPage(reader, i) document.Add(iTextSharp.text.Image.GetInstance(page)) Next Next document.Close() Response.End() End Sub End Class
Best Regards,
Brando
Tuesday, October 16, 2018 2:27 AM -
User31164289 posted
HI Brando, thanks for your response.
WriteToPdf was a function to write data on template.
I use a template pdf, modify it writing some text and open in browser window.
Thursday, October 18, 2018 8:57 AM -
User283571144 posted
Hi clembo67,
Could you please share the WriteToPdf codes?
Since this method is not a build-in method, I should know it and modify it according to your requirement.
Bes Regards,
Brando
Monday, October 22, 2018 6:22 AM