We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Header is only shown on the first page

Hello, working with aspose.pdf for net, I try to put a header on all the pages of an existing pdf and it only shows in the first one.

Code
byte[] IConversorPdf.CrearEncabezado(byte[] pdf, string html, string numeroDocumento, string secuencia, int margensuperior)
{
Aspose.Pdf.License licensepdf = new Aspose.Pdf.License();
licensepdf.SetLicense(“Aspose.Total.lic”);
licensepdf.Embedded = true;
Stream streamPdf = new MemoryStream();
streamPdf.Write(pdf, 0, pdf.Length);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(streamPdf) {OptimizeSize = true};
Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();
html = html.Replace("%%NUMERODOCUMENTO%%", numeroDocumento).Replace("%%SECUENCIA%%", secuencia);
HtmlFragment htmlstring = new HtmlFragment(html)
{
Margin = {Top = margensuperior},
HorizontalAlignment = HorizontalAlignment.Center
};
header.Paragraphs.Add(htmlstring);
foreach (Page page in pdfDocument.Pages)
{
page.Header = header;
}
Stream stream = new MemoryStream();
pdfDocument.Save(stream);
return ConvertirStreamAArrayBytes(stream);
}

@acoviant7,

We have tested your scenario with the latest version 18.2 of Aspose.PDF for .NET API and can add HTML header to all pages. Kindly send us your source PDF sample and parameter values which you are using to call CrearEncabezado method. We will investigate your scenario in our environment. Your response is awaited.

Hello, this is the code that we use to convert to pdf and add the headers and footers when they correspond:

private void ProcesarOrdenConversion(OrdenConversion ordenConversion, string pEntorno)
{
Entorno entorno = ManagerEntornosConfig.Instance.GetEntornoByName(pEntorno);

        //Carga el blob original en la orden de conversion
        ordenConversion.Archivo = ServicioBlobProxy.ObtenerDocumentoOriginal(entorno.Servicio, false, ordenConversion.BlobId);

        //convierte a pdf
        ordenConversion.Archivo.ArrayDatos = ConversorPdf.CrearPDF(ordenConversion.Archivo.ArrayDatos,
        , (ordenConversion.AltoCabezal > 0 ? ordenConversion.AltoCabezal : 0) + ordenConversion.MargenSuperior);

        //agrega el cabezal
        if (ordenConversion.AgregarCabezal == Constantes.Verdadero)
        {
            //Obtenemos el archivo original
            Archivo cabezal = ServicioBlobProxy.ObtenerDocumentoOriginal(entorno.Servicio, false, ordenConversion.BlobCabezalId);
            string html = System.Text.Encoding.UTF8.GetString(cabezal.ArrayDatos);
            ordenConversion.Archivo.ArrayDatos = ConversorPdf.CrearEncabezado(ordenConversion.Archivo.ArrayDatos, html, ordenConversion.NumeroDocumento, ordenConversion.NumeroSecuencia.ToString(), ordenConversion.MargenSuperior);
        }
            
        //Graba el archivo convertido a PDF.
        int idBlobPdf = ServicioBlobProxy.GrabarDocumentoPDF(entorno.Servicio, ordenConversion.BlobIndexable == 1, ordenConversion.Archivo, ordenConversion.TipoContenido);
        //setea el ID del nuevo blob
        ordenConversion.CargarBlodPDF(idBlobPdf);

        //Paso la orden a Estado Convertido
        Estado estadoConvertida = ConversionRepositorio.ObtenerEstado(Enumerados.EstadoOrdenConversion.Convertida.ToString());
        ordenConversion.CambiarEstado(estadoConvertida);
        UnitOfWork.Commit();
    }

These are the codes of the main functions that are referenced:

byte[] IConversorPdf.CrearPDF(byte[] documentArray, double margenSuperior)
{
Aspose.Words.License licensewords = new Aspose.Words.License();
licensewords.SetLicense(“Aspose.Total.lic”);
MemoryStream outStream = null;
Stream stream = null;
byte[] retorno;
try
{
stream = new MemoryStream(documentArray);
var doc = new Aspose.Words.Document(stream);
foreach (Aspose.Words.Section sec in doc.Sections)
{
Aspose.Words.PageSetup ps = sec.PageSetup;
ps.TopMargin = margenSuperior;
}
outStream = new MemoryStream();
var so = Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Pdf);
so.MemoryOptimization = true;
doc.Save(outStream, so);
retorno = outStream.ToArray();
}
finally
{
if (outStream != null)
outStream.Close();
if (stream != null)
stream.Close();
GC.Collect();
}
return retorno;
}

html: “<table id=“tblCabezal” name=“tblCabezal” width=“570px” align=“center”>\r\n

\r\n <td width=“35%”>\r\n <img src=“data:image/gif;base64,R0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/AEdpGvgr08BNv1pF0pTplq2Fmn5J/EfxH7qLGDNWxFjRYsaPIEFuDEkSHStEkCBFuoUSUURS\r\nLW+1QrnyV8eS6kaiu1my50WdPjHKbEnKVcuXKDMlROnKJtCL6S7mpMjxadCPVn1GcjUz5VJILn/B\r\njCRxJiJbPH/+sydVZ9qOcOP+U6dWrly6FzUh4ooS0leEYlGWRRQp7U6q6KZ6PIyYsV24eB/fxahJ\r\nZWCvZkdJhOk3ISTDIxVXbSw5buTSHfGiy5QSKSJSSxEBPkn21y3QiEXXXey299y6V6NebJUyLMy9\r\nsTfduUMG0ZvnuD3qdjx6tPSKpxdfRWfrV3HXTWdy/3n+pjl0oKF9XyftO/t2jJFKefcaGFGrO2UQ\r\nkSFvPnr13f9RB9xuqAmHjl6teEfYZoiU8Vx++z1HxmfoscfbY9ZZh5ppGLH2moK1NWfGg/qRV4p/\r\nAzoWV4YEbpgaRidBohSId7zBBSJnkEjeGyiGJBmLKrpYkYGX3SJRSvwhMuIbZUhCXo8iYQhgi1n1\r\npBlnRs5HHheQbBnJeY0xVtKPUwZZZUmZKIVljUg+x4UmXt4BJVZSCmhmmCUZqBdsnJHXpo1wuqlJ\r\ndEK+pV6hFKmWyYeBLfkGJE4KSp4phCJ6oYWW/nYRS/Yx6GiXWwbK41OF2mloppqiQwqnCf7SXI7P\r\ngT4q6ahnBmVYlNq9l5FBrP5io5J+7siFFHLCpauGONV61Sgr3ZLJVm/cQQomrzzn7CvYSjuKXLpy\r\nOyaex6ITEAA7” width=“132” height=“33”>\r\n \r\n <td width=“30%”>\r\n <td width=“15%”>\r\n <td width=“12%” style=“BORDER-RIGHT:black 1pt solid;\r\n BORDER-TOP:black 1pt solid;\r\n BORDER-LEFT:black 1pt solid;\r\n BORDER-BOTTOM:black 1pt solid;\r\n COLOR: black;”>\r\n <label width=“100%”\r\n style=“FONT-FAMILY: Tahoma;FONT-SIZE: 8pt;MARGIN-LEFT: 2pt;”>Documento:%%SECUENCIA%%\r\n \r\n \r\n <tr width=“100%”>\r\n <td colspan=“4” width=“100%”> \r\n \r\n \r\n <td width=“35%”>\r\n <td width=“30%”\r\n align=“center”\r\n style=“FONT-FAMILY: Tahoma;FONT-SIZE: 8pt;\r\n BACKGROUND-COLOR:#d5eaff;\r\n BORDER-RIGHT:black 1pt solid;\r\n BORDER-TOP:black 1pt solid;\r\n BORDER-LEFT:black 1pt solid;\r\n BORDER-BOTTOM:black 1pt solid;\r\n COLOR: black;”>\r\n EXPEDIENTE\r\n \r\n <td width=“35%” colspan=“2”>\r\n \r\n \r\n <td width=“35%”>\r\n <td width=“30%”\r\n align=“center”\r\n style=“FONT-FAMILY: Tahoma;FONT-SIZE: 8pt;\r\n BORDER-RIGHT:black 1pt solid;\r\n BORDER-TOP:black 1pt solid;\r\n BORDER-LEFT:black 1pt solid;\r\n BORDER-BOTTOM:black 1pt solid;\r\n COLOR:black;”>\r\n %%NUMERODOCUMENTO%%\r\n \r\n <td width=“35%” colspan=“2”>\r\n \r\n”
numeroDocumento: “EE18000004 - U.T.E.”
secuencia: “1”
margensuperior: 5

byte[] IConversorPdf.CrearEncabezado(byte[] pdf, string html, string numeroDocumento, string secuencia, int margensuperior)
{
Aspose.Pdf.License licensepdf = new Aspose.Pdf.License();
licensepdf.SetLicense(“Aspose.Total.lic”);
licensepdf.Embedded = true;
Stream streamPdf = new MemoryStream();
streamPdf.Write(pdf, 0, pdf.Length);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(streamPdf);
Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();
html = html.Replace("%%NUMERODOCUMENTO%%", numeroDocumento).Replace("%%SECUENCIA%%", secuencia);
HtmlFragment htmlstring = new HtmlFragment(html);
header.Paragraphs.Add(htmlstring);
header.Margin.Top = margensuperior;
foreach (Page page in pdfDocument.Pages)
{
page.Header = header;
}
Stream stream = new MemoryStream();
pdfDocument.Save(stream);
return ConvertirStreamAArrayBytes(stream);
}

private static byte[] ConvertirStreamAArrayBytes(Stream input)
{
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
}

El documento original es este.Informe.zip (8.7 KB)

@acoviant7,

Your code is not in fully compilable form and have unknown objects. Kindly create a small project application and make sure that it reproduces the said problem in your environment, and then send us a ZIP of this project. We will investigate your scenario and share our findings with you.

Hi, This is the project with the files. Best regards.

@acoviant7,

We have tested your scenario with the latest version of Aspose.PDF for .NET 18.2 and Aspose.Words for .NET 18.3. We can add header on the second page, but the image is missing and the text formatting is also incorrect. We have logged an investigation under the ticket ID PDFNET-44341 in our bug tracking system and will keep you informed regarding any available updates. This is the output PDF: FicheroSalida.pdf (86.1 KB)

Ok, thank you and best regards.

Hello and good day, for this problem do you have an estimate of how long the solution can take?

@acoviant7,

The linked ticket ID PDFNET-44341 has just been identified. Our product team will investigate as per their development schedules and it is difficult say about the ETA before the completion of the analysis phase. However, we recommend our clients to post their critical issues (or already logged ticket IDs) in the paid support forum. Please refer to this helping link: Aspose support options

The issues you have found earlier (filed as PDFNET-44341) have been fixed in Aspose.PDF for .NET 18.5. This message was posted using BugNotificationTool by asad.ali