Add HeaderFooter in PDF using Aspose.PDF for .NET | Footer overlap

Hi
I am using Aspose.pdf for generate PDF. I am facing problem with Footer Overlap in our production environment . See below attachment

image.png (90.0 KB)

My code is as below

 private string SaveAsposePDF(string FileServierPath, string FileName, string Content, CorrespondenceHeaderFooter Header, CorrespondenceHeaderFooter Footer, int LetterID = 0, int TagID = 0, bool lienceisGlobel = true)
        {

                    
            try
            {
                
                string LeftMarginValue = ConfigurationManager.AppSettings["LeftMargin"];
                if (String.IsNullOrEmpty(LeftMarginValue)) { LeftMarginValue = "50"; }

    string RightMarginValue = ConfigurationManager.AppSettings["RightMargin"];
                if (String.IsNullOrEmpty(RightMarginValue)) { RightMarginValue = "40"; }


                if (Header == null)
                    Header = new CorrespondenceHeaderFooter();
                if (Footer == null)
                    Footer = new CorrespondenceHeaderFooter();
               
pdflicense = new Aspose.Pdf.License();
                    pdflicense.SetLicense("Aspose.Pdf.lic");

                
                FileServierPath = FileServierPath + FileName;
               

                Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
                {
                    Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

                   
                    Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(Content);
text2.IsHtmlTagSupported = true;
                    section.Paragraphs.Add(text2);

                    section.PageInfo.Margin.Left = float.Parse(LeftMarginValue);
section.PageInfo.Margin.Right = float.Parse(RightMarginValue);


                    if (Header.HeaderHeight.HasValue)
                    {
                        
                        if (Header.HeaderHeight.Value >= 0)
                            section.PageInfo.Margin.Top = Header.HeaderHeight.Value;
                        else
                            section.PageInfo.Margin.Top = 50;
                    }
                    else
                        section.PageInfo.Margin.Top = 50;


                    if (Footer.FooterHeight.HasValue)
                    {
                        if (Footer.FooterHeight.Value >= 0)
                            section.PageInfo.Margin.Bottom = Footer.FooterHeight.Value;
                    }
                    
pdf.PageNumberFormat = PageNumberFormatType.EnglishUpper;
                    pdf.IsPageNumberForDocument = true;
                   
                    if (!string.IsNullOrEmpty(Header.ImageURL) || !string.IsNullOrEmpty(Header.Text))
                    {
                        Aspose.Pdf.Generator.HeaderFooter addHeader = new Aspose.Pdf.Generator.HeaderFooter(section);
section.OddHeader = addHeader;
                        section.EvenHeader = addHeader;
                        
                       
addHeader.IsFirstPageOnly = Header.IsFirstPageOnly;
                        Aspose.Pdf.Generator.Text textHeader = new Aspose.Pdf.Generator.Text(addHeader, Header.Text);
textHeader.IsHtmlTagSupported = true;
                        textHeader.TextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Right;
                        addHeader.Paragraphs.Add(textHeader);
                        
                        if (!string.IsNullOrEmpty(Header.ImageURL))
                        {
                            Aspose.Pdf.Generator.Image img1 = new Aspose.Pdf.Generator.Image(addHeader);
//Set the path of image file
img1.ImageInfo.File = Header.ImageURL;// "http://220.220.220.49/testTarantoICPS/Images/pbg.jpg";//  =getImageInfo().setFile("/mnt/sdcard/Aspose.JPG");
                            if (Header.Height.HasValue)
                                img1.ImageInfo.FixHeight = Header.Height.Value;
                            if (Header.Width.HasValue)
                                img1.ImageInfo.FixWidth = Header.Width.Value;
                            switch (Header.ImageAlignment)
                            {
                                case CorrespondenceHeaderFooter.ImageAlignmentEnum.Right:
                                    img1.ImageInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Right;
                                    break;
                                case CorrespondenceHeaderFooter.ImageAlignmentEnum.Centre:
                                    img1.ImageInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
                                    break;
                                default:
                                    img1.ImageInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Left;
                                    break;
                            }
                            addHeader.Paragraphs.Add(img1);
                            
                        }
                    }

                    Aspose.Pdf.Generator.HeaderFooter addFooter = new Aspose.Pdf.Generator.HeaderFooter(section);
section.OddFooter = addFooter;
                    section.EvenFooter = addFooter;
                    addFooter.IsFirstPageOnly = Footer.IsFirstPageOnly;
                    addFooter.DistanceFromEdge = (float) (Footer.FooterDistanceFromEdge == null ? 30 : Footer.FooterDistanceFromEdge.Value);                   
                    Aspose.Pdf.Generator.Text text = new Aspose.Pdf.Generator.Text(addFooter, Footer.Text);

text.IsHtmlTagSupported = true;
                    text.TextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Right;
                    addFooter.Paragraphs.Add(text);
                   
                    if (!string.IsNullOrEmpty(Footer.ImageURL))
                    {
                        Aspose.Pdf.Generator.Image img1 = new Aspose.Pdf.Generator.Image(addFooter);
//Set the path of image file
img1.ImageInfo.File = Footer.ImageURL;// "http://220.220.220.49/testTarantoICPS/Images/pbg.jpg";//  =getImageInfo().setFile("/mnt/sdcard/Aspose.JPG");
                        if (Footer.Height.HasValue)
                        {
                            img1.ImageInfo.FixHeight = Footer.Height.Value;                           
                        }
                        if (Footer.Width.HasValue)
                            img1.ImageInfo.FixWidth = Footer.Width.Value;
                        switch (Footer.ImageAlignment)
                        {
                            case CorrespondenceHeaderFooter.ImageAlignmentEnum.Right:
                                img1.ImageInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Right;
                                break;
                            case CorrespondenceHeaderFooter.ImageAlignmentEnum.Centre:
                                img1.ImageInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
                                break;
                            default:
                                img1.ImageInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Left;
                                break;
                        }
                        addFooter.Paragraphs.Add(img1);
                       
                    }
                   
bool isBlob = FileCommon.IsBlobFile(FileServierPath);


                    try
                    { using (MemoryStream outStream = new MemoryStream())
                        {
                            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                            pdf.Save(outStream);

                            using (IFileManager fileMan = FileCommon.GetFileInstance(FileServierPath))
                            {
                               
FileCommon.SplitToPathAndFile(FileServierPath, out string directoryOnly, out string fileNameOnly);
                               
fileMan.CreateFolderIfNeeded(directoryOnly);
                                
fileMan.DeleteFile(FileServierPath);
                                
fileMan.SaveFile(FileServierPath, outStream.ToArray());
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        TarantoLoggerWrite("file Created Ex" + ex.Message); tarantoLogger.LogException(ex); throw ex;
                    }
                    finally
                    {
                        //outStream.Flush();
                        //outStream.Dispose();
                    }


                    

                    while (!isBlob)
                    {
                        try
                        {
                            using (FileStream Fs = new FileStream(FileServierPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 100))
                            {

                                //TarantoLoggerWrite("5");
                                break;
                            }
                        }
                        catch (IOException ex)
                        {
                            throw ex;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return FileServierPath;
        }

Please suggest what we need to change.

@ramuchoudhary

We are afraid that you are using an obsolete approach i.e. Aspose.Pdf.Generator. Please note that this approach has been discontinued and we are not providing any support for it. The new recommended approach is DOM i.e. Aspose.PDF and you can please use it in order to add header/footer inside PDF.

Please check below useful links in order to achieve your requirement and use latest version of the API as well i.e. Aspose.PDF for .NET 21.5. In case you still face any issue, please let us know.