The following is the anonymized version of my code, though you may have to find a replacement SVG. What I can tell you for sure is that the documentation page you linked does not do what it says it does. For reference, I am using the latest NuGet package for Aspose.PDF, 18.4.1
.
Things that do not do anything:
-
ProcessParagraphs()
, when run, leaves the number of pages in the document at 1, directly contradicting its purpose as defined in your documentation.
- The
Rectangle
-typed properties. Yes, I can change them, and some of them have a noticeable effect: they will move all content on the page. What I need is to control the area that, when overflowed, generates a new page. This is functionality that exists, and I’m willing to believe it is simply not exposed, but I definitely want to find out.
class Program
{
static void Main(string[] args)
{
const string output = "C:\\doc\\hello2.pdf";
const string companyName = "XYZ Company";
var report = new MoreSpecificReport(output, companyName);
report.CreatePdf();
}
}
public class MoreSpecificReport : Report
{
public MoreSpecificReport(string output, string companyName)
: base(output, companyName, "Actual Real Report")
{
}
protected override IEnumerable<BaseParagraph> InsertParagraphs()
=> new BaseParagraph[]
{
new TextFragment("Hello"),
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
},
new Image
{
File = "C:\\doc\\rect.svg",
FileType = ImageFileType.Svg
}
};
}
public abstract class Report
{
private string outputFile;
private string companyName;
private string reportName;
private const string copyrightLine = @"This is a copyright line.";
private readonly HeaderFooter header;
public Report(string outputFile, string companyName, string reportName)
{
this.outputFile = outputFile;
this.companyName = companyName;
this.reportName = reportName;
header = new HeaderFooter
{
Paragraphs =
{
new Table
{
ColumnWidths = "33%",
Rows =
{
new Row
{
Cells =
{
CreateHeaderCell(HorizontalAlignment.Left, companyName),
CreateHeaderCell(HorizontalAlignment.Center, reportName),
CreateHeaderCell(HorizontalAlignment.Right, "Our Company's Name")
}
}
}
}
}
};
}
public void CreatePdf()
{
var document = new Document();
var newPage = document.Pages.Add();
newPage.Header = header;
//var modRect = newPage.Rect;
//newPage.TrimBox = new Rectangle(modRect.LLX, modRect.LLY + 300, modRect.URX, modRect.URY);
newPage.OnBeforePageGenerate += OnPageGenerate;
var paragraphs = InsertParagraphs();
document.ProcessParagraphs(); // And yet, after this line runs, there is still only one page.
foreach(var paragraph in InsertParagraphs())
{
newPage.Paragraphs.Add(paragraph);
}
document.Save(outputFile);
}
private void OnPageGenerate(Page page)
{
page.Footer = CreateFooter(page.Number);
}
protected abstract IEnumerable<BaseParagraph> InsertParagraphs();
private HeaderFooter CreateFooter(int pageNumber, int maximumPageNumber = 0)
{
var footer = new HeaderFooter();
var footerTable = new Table
{
ColumnWidths = "50%",
HorizontalAlignment = HorizontalAlignment.Center
};
var locationRow = footerTable.Rows.Add();
locationRow.Cells.Add(new Cell
{
Paragraphs =
{
new TextFragment(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"))
{
HorizontalAlignment = HorizontalAlignment.Left
}
}
});
var pagingFragment = new TextFragment
{
Segments = {
new TextSegment("Page "),
new TextSegment(pageNumber.ToString())
{
TextState = new TextState
{
FontStyle = FontStyles.Bold
}
},
},
HorizontalAlignment = HorizontalAlignment.Right
};
if(maximumPageNumber > 0)
{
pagingFragment.Segments.Add(new TextSegment(" of "));
pagingFragment.Segments.Add(new TextSegment(maximumPageNumber.ToString())
{
TextState = new TextState
{
FontStyle = FontStyles.Bold
}
});
}
locationRow.Cells.Add(new Cell
{
Paragraphs =
{
pagingFragment
}
});
footer.Paragraphs.Add(footerTable);
footer.Paragraphs.Add(new TextFragment
{
Segments =
{
new TextSegment(copyrightLine)
{
TextState = new TextState
{
FontSize = 7
}
}
},
Margin = new MarginInfo(0, 0, 0, 2)
});
return footer;
}
private Cell CreateHeaderCell(HorizontalAlignment alignment, string content)
{
var cell = new Cell
{
Alignment = alignment,
Margin = new MarginInfo(0, 0, 0, 20)
};
if (!string.IsNullOrWhiteSpace(content))
{
var text = new TextFragment(content)
{
HorizontalAlignment = alignment
};
text.TextState.FontStyle = FontStyles.Bold;
text.TextState.FontSize = 18;
cell.Paragraphs.Add(text);
}
return cell;
}
}
EDIT: Supplementary information: I have tried interacting with the various rectangles, but so far only the CropBox
affects overflow in any way, but it changes the entire page size. I’m sure there has to be a way, though.