Consider a 1-page PDF document named in.pdf
that looks like this:
We execute the following code in C#:
using (var reader = new PdfReader(@"C:\<path>\in.pdf"))
using (var writer = new PdfWriter(@"C:\<path>\out.pdf"))
using (var pdfDoc = new PdfDocument(reader, writer))
{
var page = pdfDoc.GetFirstPage();
var pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
using (var canvas = new Canvas(pdfCanvas, new Rectangle(400, 300)))
{
canvas.Add(new Paragraph("new text\r\nnew text")
.SetFontColor(new DeviceRgb(255, 0, 0))
.SetFontSize(50));
}
pdfCanvas.Release();
}
We get new text appearing under the original text, as expected:
Now we execute the same code with one change - we create PdfCanvas
as follows:
var pdfCanvas = new PdfCanvas(page);
Result is still as expected - new text is above the original text:
Finally, we execute the same code with PdfCanvas
created as follows:
var pdfCanvas = new PdfCanvas(page.NewContentStreamAfter(), page.GetResources(), pdfDoc);
New text is above the original text as expected, but it's flipped!
Is this a bug? Getting the same flipped output creating PdfCanvas
like this:
var pdfCanvas = new PdfCanvas(page.GetFirstContentStream(), page.GetResources(), pdfDoc);
//OR
var pdfCanvas = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdfDoc);
Using nuget package itext7 v7.1.15
.