Search code examples
c#pdfitext

Create separate PDF's (one page per PDF) C# ItextSharp


A long time ago, an application was created with which e-tickets could be sold online.

The e-tickets are in PDF format.

The software uses an old library (itextsharp.dll) but it still works perfectly.

What happens now when someone buys 4 tickets, for example, is that the system creates 1 PDF file (with 4 pages) and sends it as an attachment by e-mail.

What I now want is for the system to create 4 separate PDF files and send them as 4 attachments.

As I can see now, the PDF is placed in a MemoryStream and this MemoryStream (1 PDF with 4 pages) is then sent as an attachment. And this works well!

Is it possible to put 4 separate PDF objects into a MemoryStream and then send them as an attachment? So that the recipient receives 4 separate PDF files?

Naturally, it is impossible to predict in advance how many e-tickets someone will buy.

Below is a piece of C# code that now ensures that all pages end up in 1 PDF.

Do you have the solution for me?

PhonepageBytes = Phonestream.ToArray();
PhonepagesAll.Add(PhonepageBytes);
}

MemoryStream Phonetickets = new MemoryStream();

Document Phonedoc = new Document();

PdfSmartCopy Phonecopy = new PdfSmartCopy(Phonedoc, Phonetickets);

Phonedoc.Open();
Phonedoc.OpenDocument();

foreach (var b in PhonepagesAll)
{
    Phonedoc.NewPage();
    Phonecopy.AddPage(copy.GetImportedPage(new PdfReader(b), 1));
    // Create a new output PDF document (and save it into memorystream)
}

Phonecopy.CloseStream = false;
Phonecopy.Close();

SendTickets(fullName, emailAddress, Phonetickets);

I really don't have a clue. :-(

The code is working perfect to create 1 PDF with an e-ticket on each page. I want a separate PDF for each e-ticket and send all those PDFs as attachment.


Solution

  • Check the below code.

    List<MemoryStream> ticketStreams = new List<MemoryStream>();
    
    foreach (var ticketData in PhonepagesAll)
    {
        MemoryStream ticketStream = new MemoryStream();
    
        Document ticketDoc = new Document();
        PdfSmartCopy ticketCopy = new PdfSmartCopy(ticketDoc, ticketStream);
    
        ticketDoc.Open();
        ticketDoc.OpenDocument();
    
        ticketDoc.NewPage();
        ticketCopy.AddPage(new PdfReader(ticketData));
    
        ticketCopy.CloseStream = false;
        ticketCopy.Close();
    
        // Add the ticket MemoryStream to the list
        ticketStreams.Add(ticketStream);
    }
    
    SendTicket(emailAddress, ticketStreams)
    
    void SendTicket(string emailAddress, List<MemoryStream> ticketStreams)
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("[email protected]");
        mail.To.Add(new MailAddress(emailAddress));
        mail.Subject = "Your Tickets";
    
        // Attach all the ticket MemoryStreams to the email
        int ticketNo = 0;
        foreach (var ticketStream in ticketStreams)
        {
            ticketNo++;
            ticketStream.Position = 0; // Reset stream position to start
            Attachment attachment = new Attachment(ticketStream, $"Ticket-{ticketNo}.pdf");
            mail.Attachments.Add(attachment);
        }
    
        SmtpClient smtpClient = new SmtpClient("your_smtp_server");
        smtpClient.Send(mail);
    }
    

    The above code will create separate MemoryStreams for each e-ticket, add them to a list, and then attach each MemoryStream as a separate attachment to a single email. Here I used an SMTP client to send the email with all the attached tickets, you need to implement your email sending logic.