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.

Mon Oct 18, 2021 1:11 pm

In my example, when I copy each page, an empty space is added to the right.
The example in the attachment - https://1drv.ms/u/s!AodGrH0MILN8jmedq79TK9RsfxLu?e=SDDulr
Source file: source.pdf,
Response file: response.pdf
Code:
Code: Select all
        public static Stream CopyPdfFile(Stream sourceFile)
        {
            var targetPdf = new PdfDocument();
            var sourcePdf = new PdfDocument(sourceFile);

            foreach (PdfPageBase sourcePage in sourcePdf.Pages)
            {
                var targetPage = targetPdf.Pages.Add(sourcePage.Size, new Spire.Pdf.Graphics.PdfMargins(0, 0));
                targetPage.Canvas.DrawTemplate(sourcePage.CreateTemplate(), new System.Drawing.PointF(0, 0));
            }

            var response = new MemoryStream();

            try
            {
                targetPdf.SaveToStream(response);
                response.Position = 0;

                return response;
            }
            catch
            {
                response.Dispose();
                throw;
            }
        }

burz00m21
 
Posts: 12
Joined: Thu Sep 16, 2021 12:20 pm

Tue Oct 19, 2021 1:58 am

Hello Lgor,

Thanks for your inquiry.
I failed to access your link( https://1drv.ms/u/s!AodGrH0MILN8jmedq79 ... u?e=SDDulr) to download your files. Could you please upload them as .zip or send them to [email protected]?
Thank you for your assistance in advance.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3044
Joined: Wed Jun 27, 2012 8:50 am

Tue Oct 19, 2021 4:27 am

I have sending attached by email.
I forgot define my environment:
Spire.Pdf, Version=7.7.0.0
Windows 10 x64

burz00m21
 
Posts: 12
Joined: Thu Sep 16, 2021 12:20 pm

Tue Oct 19, 2021 6:03 am

Dear Lgor,

I got your files from my email. Thanks for your sharing.
As your source pdf page's rotation angle is 270, so page size of target Pdf should be SizeF pageSize = new SizeF(sourcePage.Size.Height, sourcePage.Size.Width). Please refer to the modified code below.
Code: Select all
public static Stream CopyPdfFile(Stream sourceFile)
            {
                var targetPdf = new PdfDocument();
                var sourcePdf = new PdfDocument(sourceFile);
                 SizeF size;
                foreach (PdfPageBase sourcePage in sourcePdf.Pages)
                {
                     if (sourcePage.Rotation == PdfPageRotateAngle.RotateAngle270)
                     {

                       size = new SizeF(sourcePage.Size.Height, sourcePage.Size.Width);
                     }
                   else
                    {
                      size = sourcePage.Size;
                    }
                     var targetPage = targetPdf.Pages.Add(size, new Spire.Pdf.Graphics.PdfMargins(0, 0));
                     targetPage.Canvas.DrawTemplate(sourcePage.CreateTemplate(), new System.Drawing.PointF(0, 0));
                   }

                var response = new MemoryStream();

                try
                {
                    targetPdf.SaveToStream(response);
                    response.Position = 0;

                    return response;
                }
                catch
                {
                    response.Dispose();
                    throw;
                }
            }


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3044
Joined: Wed Jun 27, 2012 8:50 am

Tue Oct 19, 2021 8:08 am

Will this code work for all types of rotation? Why exactly for 270 the width and height are swapped?

burz00m21
 
Posts: 12
Joined: Thu Sep 16, 2021 12:20 pm

Tue Oct 19, 2021 8:11 am

probably for 90, too

burz00m21
 
Posts: 12
Joined: Thu Sep 16, 2021 12:20 pm

Tue Oct 19, 2021 9:26 am

Hi Lgor,

The code we provided is for your PDF which has 270 degree rotated pages. You could change the condition as below to apply to the 90 rotation. If there is other question, please feel free to write back.
Code: Select all
if (sourcePage.Rotation == PdfPageRotateAngle.RotateAngle90|| sourcePage.Rotation==PdfPageRotateAngle.RotateAngle270)
{
      size = new SizeF(sourcePage.Size.Height, sourcePage.Size.Width);
}

Sincertely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Tue Oct 19, 2021 9:35 am

Thank you! Problem solved.

burz00m21
 
Posts: 12
Joined: Thu Sep 16, 2021 12:20 pm

Tue Oct 19, 2021 10:00 am

Hi,

You are welcome.
Free free to contact us if you need further assistance.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF