Insert header while converting to PDF

Hi,

We are currently working on a document system. The users can send emails from the application based on templates with mergefields etc.
Eventually the output is a string with wordml code in it.

I convert that string with Aspose.Words to a PDF without problems with the following code:
The document variable in the method is my string containing wordml code.

public byte[] ConvertTemplateToPdf(string document)
{
    var stream = new MemoryStream();
    var encoding = new UTF8Encoding();
    Document doc;

    var newStream = new MemoryStream(encoding.GetBytes(document));
    using (newStream)
    {
        doc = new Document(newStream);
    }

    foreach (Section section in doc.Sections)
    {
        // Set PaperSize to A4
        section.PageSetup.PaperSize = PaperSize.A4;
    }

    try
    {

        var pdfoptions = new PdfSaveOptions { Compliance = PdfCompliance.PdfA1b, SaveFormat = SaveFormat.Pdf, TextCompression = PdfTextCompression.None };
        doc.Save(stream, pdfoptions);
    }
    catch (Exception ex)
    {
        throw new ApplicationException("ERROR conversie : " + ex.Message);
    }

    byte[] convertedDocument;
    using (stream)
    {
        convertedDocument = stream.GetBuffer();
    }

    return convertedDocument;
}

Now the users are asking to add the email from, to and subject at the top of this document.
I found something about adding headers in the documentation pages from aspose.Words but i’m not sure how to do this or if it will work.

Is it possible to add a header to my wordml string before converting to total to pdf?

thx in advance.

Hi
Thanks for your request. Sure you can insert header into the document before converting it to PDF. Please see the following link:
https://docs.aspose.com/words/net/working-with-headers-and-footers/
but in your case, I think, it is not necessary to use Header. I think it would be enough to insert “email from, to and subject” at the beginning of your document. To achieve this, you should modify your code as shown below:

public byte[] ConvertTemplateToPdf(string document, string from, string to, string subject)
{
    var stream = new MemoryStream();
    var encoding = new UTF8Encoding();
    Document doc;
    var newStream = new MemoryStream(encoding.GetBytes(document));
    using (newStream)
    {
        doc = new Document(newStream);
    }
    foreach (Section section in doc.Sections)
    {
        // Set PaperSize to A4
        section.PageSetup.PaperSize = PaperSize.A4;
    }
    // Insert from, to and subject at the beggining of the document.
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.Writeln("From: " + from);
    builder.Writeln("To: " + to);
    builder.Writeln("Subject: " + subject);
    try
    {
        var pdfoptions = new PdfSaveOptions { Compliance = PdfCompliance.PdfA1b, SaveFormat = SaveFormat.Pdf, TextCompression = PdfTextCompression.None };
        doc.Save(stream, pdfoptions);
    }
    catch (Exception ex)
    {
        throw new ApplicationException("ERROR conversie : " + ex.Message);
    }
    byte[] convertedDocument;
    using (stream)
    {
        convertedDocument = stream.GetBuffer();
    }
    return convertedDocument;
}

Hope this helps.
Best regards,