Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Jan 12, 2022 12:47 pm

Hi,

I have a page template that I would like to draw onto another page but I would like to flip the page so it is mirrored.

There is no flip transform on the canvas so I tried using a scale transform of -1,0 but the resulting PDF is blank. I tried with and without a translate transform of (template.width,0) as I assumed the resulting flipped template might be off the page but still blank.

I also tried a scale transform of 0.2f,0 because this should show up without any translate transform but the resulting PDF is still blank.

If I don't use any transform then the resulting PDF does contain the original content.

This is the code I'm using

Code: Select all
var pdf = new PdfDocument();
pdf.LoadFromFile("filepath");
var newPdf = new PdfDocument();
var t = pdf.Pages[0].CreateTemplate();
var page = newPdf.Pages.Add(t.Size, new PdfMargins(0));
page.Canvas.ScaleTransform(0.2f, 0);
page.Canvas.DrawTemplate(t, new PointF(0, 0));
newPdf.SaveToFile("newFilepath");


Am I missing something? Is there another way to flip a pagetemplate?

Thanks

emaginationstore
 
Posts: 15
Joined: Mon Sep 25, 2017 2:02 pm

Thu Jan 13, 2022 5:59 am

Hello,

Thank you for your inquiry.
Sorry, currently our Spire.PDF does not support flipping PDF pages or PDF canvases directly. I have submitted this requirement to our Dev team and the ticket number is SPIREPDF-4894. Once it is achieved in the future, I will inform you in time. However, there is a temporary solution, which is to convert the PDF page to an image, then flip the image, and finally draw the flipped image on the PDF canvas. The sample code is as follows.
Code: Select all
 PdfDocument pdf = new PdfDocument();
 pdf.LoadFromFile("filepath");
 var newPdf = new PdfDocument();
 PdfPageBase p = pdf.Pages[0];
 var page = newPdf.Pages.Add(p.Size, new PdfMargins(0));
 Image image = pdf.SaveAsImage(0);
 image.RotateFlip(RotateFlipType.Rotate180FlipY);
 page.Canvas.DrawImage(PdfImage.FromImage(image), new PointF(0, 0));
 newPdf.SaveToFile("result.pdf");

In addition, you mentioned that the result PDF file is blank when running the code you provided, which is caused by the zero value of the Y axis in the ScaleTransform method.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Jan 13, 2022 9:23 am

Thanks for the reply but the work around won't work for me as the PDFs may contain spot colours.

emaginationstore
 
Posts: 15
Joined: Mon Sep 25, 2017 2:02 pm

Fri Jan 14, 2022 2:23 am

Hello,

Thanks for your feedback.
After investigation, we found out that flipping of PDF pages can be achieved using the code below. Please test it on your side.
Code: Select all
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("filepath");
var newPdf = new PdfDocument();
var t = pdf.Pages[0].CreateTemplate();
var page = newPdf.Pages.Add(t.Size, new PdfMargins(0));

page.Canvas.ScaleTransform(-1, 1);
page.Canvas.TranslateTransform(-t.Size.Width,0);

page.Canvas.DrawTemplate(t, new PointF(0, 0));
newPdf.SaveToFile("result.pdf");

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Jan 14, 2022 10:15 am

Oh yes, how silly of me :lol:

I set the y value on the scale transform to 0 so the output had no height!

Thank you and sorry for wasting your time.

emaginationstore
 
Posts: 15
Joined: Mon Sep 25, 2017 2:02 pm

Mon Jan 17, 2022 1:08 am

Hello,

You're welcome.
If you encounter other issues related to our products in the future, please feel free to contact us.
Have a nice day.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF

cron