Free Support Forum - aspose.com

Add page break in PDF document using Aspose.PDF for .NET - Header/Footer is not repeating after AddPageBreak()

After adding a page break to an existing PDF using AddPageBreak(), the new page doesn’t include the header or footer. Is it possible to add these after the page break? Also, is it possible to reformat the rest of the PDF so that the page breaks after the one that was added are located in natural positions. For example, if I add a page break in the middle of page 2, then page 3 will contain half the text from what was originally page 2, followed by white space, and then page 4 (originally page 3). I’d prefer to see the white space in page 3 removed and as much of the text in page 4 that will fit moved up. Subsequent page breaks should then occur at regular intervals.

@scschommer

Thanks for contacting support.

Would you kindly share your sample code snippet along with respective PDF output. We will test the scenario in our environment and address it accordingly.

Sure, here’s the code I’m using to add a page break:

// The path to the documents directory.
var docDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.ToString() + “\docs\”;

// Apply the Aspose license
License license = new Aspose.Pdf.License();
license.SetLicense(docDir + “Aspose.Pdf.lic”);

// Create a regular expression that will match the line where the final diagnosis starts
var search = “Final Pathologic Diagnosis”;

// Create TextAbsorber object to find all instances of the search criteria
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(search);

// Accept the absorber for all the pages in the source document
var sourcePath = docDir + “source.pdf”;
Document sourceDoc = new Document(sourcePath);
sourceDoc.Pages.Accept(textFragmentAbsorber);

// Add a page break at the position just before the search text
TextFragment textFragment = textFragmentAbsorber.TextFragments[1];
Document outputDoc = new Document();
PdfFileEditor fileEditor = new PdfFileEditor();
fileEditor.AddPageBreak(sourceDoc, outputDoc, new PdfFileEditor.PageBreak[] {
new PdfFileEditor.PageBreak(textFragment.Page.Number, textFragment.Rectangle.URY - 1) });

// Save the output
var outputPath = docDir + “output.pdf”;
outputDoc.Save(outputPath);

Here are the source and output files:
output.pdf (33.1 KB)
source.pdf (43.5 KB)

@scschommer

We were able to notice the mentioned behavior of the API in our environment with Aspose.PDF for .NET 20.1. Please note that header/footer is specified at the time of PDF generation and once it is generated, there is no way to determine them particularly. However, we have logged an investigation ticket as PDFNET-47555 in our issue tracking system for further investigation of this scenario.

We will check the feasibility of your requirement and will surely inform you as soon as we have some certain news on ticket resolution. Please be patient and spare us little time.

We are sorry for the inconvenience.

It seems like I could use Aspose to manually add a new header after the page break, but adding a header doesn’t move the existing text on the page down to accommodate it. I’ve tried creating a TextStamp and using AddStamp, and I’ve also tried using the HeaderFooter class, and neither works. Is there a way to move the text down so it doesn’t overlap with the header? Here are some code examples:

// Add the header to the new page using a TextStamp
TextStamp headerStamp = new TextStamp(headerText);
headerStamp.VerticalAlignment = VerticalAlignment.Top;
headerStamp.HorizontalAlignment = HorizontalAlignment.Center;
headerStamp.TopMargin = 10;
headerStamp.BottomMargin = 10;
outputDoc.Pages[textFragment.Page.Number + 1].AddStamp(headerStamp);

// Add the header using HeaderFooter
HeaderFooter header = new HeaderFooter();
TextFragment text = new TextFragment(headerText);
header.Paragraphs.Add(text);
header.Margin.Top = 10;
header.Margin.Bottom = 10;
outputDoc.Pages[textFragment.Page.Number + 1].Header = header;

Here’s an example of the output. You can see the overlapping header text on page 3 (after the page break).
output.pdf (34.1 KB)

@scschommer

Thanks for getting back to us.

You may please try to resize Page content in PDF document and add TextStamp after it. In case you still face any issue, please feel free to let us know.

That link takes me to a page titled “create a project that does Epub to PDF, Preview PDF converter”, which doesn’t seem right. Was that your intention?

@scschommer

We apologize for the inconvenience.

We have edited the link in our previous reply now.