mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-20 11:37:57 +08:00
support custom page sizes for document builder #147
page size custom is not supported for the document builder so a new overload which supports user defined page sizes is provided.
This commit is contained in:
@@ -25,6 +25,34 @@
|
|||||||
Assert.EndsWith("%%EOF", str);
|
Assert.EndsWith("%%EOF", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CanCreateSingleCustomPageSize()
|
||||||
|
{
|
||||||
|
var builder = new PdfDocumentBuilder();
|
||||||
|
|
||||||
|
var page = builder.AddPage(120, 250);
|
||||||
|
|
||||||
|
var font = builder.AddStandard14Font(Standard14Font.Helvetica);
|
||||||
|
|
||||||
|
page.AddText("Small page.", 12, new PdfPoint(25, 200), font);
|
||||||
|
|
||||||
|
var bytes = builder.Build();
|
||||||
|
|
||||||
|
WriteFile(nameof(CanCreateSingleCustomPageSize), bytes);
|
||||||
|
|
||||||
|
using (var document = PdfDocument.Open(bytes, ParsingOptions.LenientParsingOff))
|
||||||
|
{
|
||||||
|
Assert.Equal(1, document.NumberOfPages);
|
||||||
|
|
||||||
|
var page1 = document.GetPage(1);
|
||||||
|
|
||||||
|
Assert.Equal(120, page1.Width);
|
||||||
|
Assert.Equal(250, page1.Height);
|
||||||
|
|
||||||
|
Assert.Equal("Small page.", page1.Text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanReadSingleBlankPage()
|
public void CanReadSingleBlankPage()
|
||||||
{
|
{
|
||||||
|
@@ -33,7 +33,8 @@
|
|||||||
public DocumentInformationBuilder DocumentInformation { get; } = new DocumentInformationBuilder();
|
public DocumentInformationBuilder DocumentInformation { get; } = new DocumentInformationBuilder();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The current page builders in the document and the corresponding 1 indexed page numbers. Use <see cref="AddPage"/> to add a new page.
|
/// The current page builders in the document and the corresponding 1 indexed page numbers. Use <see cref="AddPage(double,double)"/>
|
||||||
|
/// or <see cref="AddPage(PageSize,bool)"/> to add a new page.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IReadOnlyDictionary<int, PdfPageBuilder> Pages => pages;
|
public IReadOnlyDictionary<int, PdfPageBuilder> Pages => pages;
|
||||||
|
|
||||||
@@ -136,19 +137,19 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add a new page with the specified size, this page will be included in the output when <see cref="Build"/> is called.
|
/// Add a new page with the specified size, this page will be included in the output when <see cref="Build"/> is called.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="size">The size of the page to add.</param>
|
/// <param name="width">The width of the page in points.</param>
|
||||||
/// <param name="isPortrait">Whether the page is in portait or landscape orientation.</param>
|
/// <param name="height">The height of the page in points.</param>
|
||||||
/// <returns>A builder for editing the new page.</returns>
|
/// <returns>A builder for editing the new page.</returns>
|
||||||
public PdfPageBuilder AddPage(PageSize size, bool isPortrait = true)
|
public PdfPageBuilder AddPage(double width, double height)
|
||||||
{
|
{
|
||||||
if (!size.TryGetPdfRectangle(out var rectangle))
|
if (width < 0)
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"No rectangle found for Page Size {size}.");
|
throw new ArgumentOutOfRangeException(nameof(width), $"Width cannot be negative, got: {width}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isPortrait)
|
if (height < 0)
|
||||||
{
|
{
|
||||||
rectangle = new PdfRectangle(0, 0, rectangle.Height, rectangle.Width);
|
throw new ArgumentOutOfRangeException(nameof(height), $"Height cannot be negative, got: {height}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
PdfPageBuilder builder = null;
|
PdfPageBuilder builder = null;
|
||||||
@@ -166,12 +167,39 @@
|
|||||||
builder = new PdfPageBuilder(pages.Count + 1, this);
|
builder = new PdfPageBuilder(pages.Count + 1, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.PageSize = rectangle;
|
builder.PageSize = new PdfRectangle(0, 0, width, height);
|
||||||
pages[builder.PageNumber] = builder;
|
pages[builder.PageNumber] = builder;
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a new page with the specified size, this page will be included in the output when <see cref="Build"/> is called.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="size">The size of the page to add.</param>
|
||||||
|
/// <param name="isPortrait">Whether the page is in portait or landscape orientation.</param>
|
||||||
|
/// <returns>A builder for editing the new page.</returns>
|
||||||
|
public PdfPageBuilder AddPage(PageSize size, bool isPortrait = true)
|
||||||
|
{
|
||||||
|
if (size == PageSize.Custom)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Cannot use ${nameof(PageSize.Custom)} for ${nameof(AddPage)} using the ${nameof(PageSize)} enum, call the overload with width and height instead.",
|
||||||
|
nameof(size));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!size.TryGetPdfRectangle(out var rectangle))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"No rectangle found for Page Size {size}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isPortrait)
|
||||||
|
{
|
||||||
|
return AddPage(rectangle.Height, rectangle.Width);
|
||||||
|
}
|
||||||
|
|
||||||
|
return AddPage(rectangle.Width, rectangle.Height);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Builds a PDF document from the current content of this builder and its pages.
|
/// Builds a PDF document from the current content of this builder and its pages.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Reference in New Issue
Block a user