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 Jun 17, 2020 5:50 pm

When I copy each page from one pdf document to a new pdf document, the copy process does not respect the landscape page properties.
Not the page AFTER page 53 is landscape in the existing pdf, but is portrait in the new document.

Could you modify my code so the document is exactly like the original document please.

Code: Select all
public static void CopyPages()
{
   var existingFileName = "c:\\temp\\Locked3480.pdf";
   var newFileName = $"c:\\temp\\UnLocked3480_{DateTime.Now.Millisecond.ToString()}.pdf";
   var existingStream = new FileStream(existingFileName, FileMode.Open);
   var newFileStream = new FileStream(newFileName, FileMode.OpenOrCreate);

   var existingPDF = new Spire.Pdf.PdfDocument();
   var newPDF = new Spire.Pdf.PdfDocument();
   existingPDF.LoadFromStream(existingStream);

   for (var i = 0; i < existingPDF.Pages.Count; i++)
   {
      var currentPage = existingPDF.Pages[i];
      var size = currentPage.Size;
      var template = currentPage.CreateTemplate();
      var page = newPDF.Pages.Add(size, new PdfMargins(0));
      page.Canvas.DrawTemplate(template, new PointF(0, 0));
   }

   newPDF.SaveToStream(newFileStream);
}


terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Thu Jun 18, 2020 2:07 am

Hello,

Thanks for your inquiry.
Please refer to the following modified code. If there is any question, please feel free to contact us.
Code: Select all
        public static void CopyPages()
        {
           var existingFileName = "c:\\temp\\Locked3480.pdf";
           var newFileName = $"c:\\temp\\UnLocked3480_{DateTime.Now.Millisecond.ToString()}.pdf";
           var existingStream = new FileStream(existingFileName, FileMode.Open);
           var newFileStream = new FileStream(newFileName, FileMode.OpenOrCreate);

           var existingPDF = new Spire.Pdf.PdfDocument();
           var newPDF = new Spire.Pdf.PdfDocument();
           existingPDF.LoadFromStream(existingStream);

           for (var i = 0; i < existingPDF.Pages.Count; i++)
           {
              var currentPage = existingPDF.Pages[i];
              var size = currentPage.Size;
              var template = currentPage.CreateTemplate();
              /************************************/
              PdfPageBase page;
              if (currentPage.Rotation == PdfPageRotateAngle.RotateAngle90)
              {
                  page = newPDF.Pages.Add(size, new PdfMargins(0), PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape);
              }
              else
              {
                  page = newPDF.Pages.Add(size, new PdfMargins(0));
              }
              /************************************/
              page.Canvas.DrawTemplate(template, new PointF(0, 0));
           }
           newPDF.SaveToStream(newFileStream);
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Jun 18, 2020 1:15 pm

Thank you.

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Fri Jun 19, 2020 1:03 am

Hello,

Thanks for your reply.
If you need further assistance, just feel free to let us know. Have a nice day!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri May 06, 2022 5:34 pm

We process about 100 locked documents a day. We copy each of them to an unlocked version.

Our code is getting increasingly difficult to maintain. There must be a better way to do this than the way I am doing it.
I am uploading a document that gets the orientation wrong and this code does not address the issue. Page 23 and 26 do not get converted over properly.


Code: Select all
public static void CopyToUnlocked(Stream lockedStream, Stream outputStream)
{
   var lockedPDF = new PdfDocument();
   var unlockedPDF = new PdfDocument();
   int lockedPDFPageCount;
   var errorUnlockingPDF = false;

   try { lockedPDF.LoadFromStream(lockedStream); }
   catch (Exception) { errorUnlockingPDF = true; }

   try { lockedPDFPageCount = lockedPDF.Pages.Count; }
   catch (Exception) { errorUnlockingPDF = true; }

   if (!errorUnlockingPDF)
   {
      for (var i = 0; i < lockedPDF.Pages.Count; i++)
      {
         var currentLockedPage = lockedPDF.Pages[i];
         var template = currentLockedPage.CreateTemplate();
         PdfPageBase page = null;

         if (currentLockedPage.Rotation == PdfPageRotateAngle.RotateAngle0 && currentLockedPage.Size.Width > currentLockedPage.Size.Height) //D5186
            page = unlockedPDF.Pages.Add(currentLockedPage.Size, new PdfMargins(0), PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape);

         //this added 02/22/2022 page was RotateAngle180, but portrait, so I added the check for height vs width 2131fos20220201.pdf page 24
         else if (currentLockedPage.Rotation == PdfPageRotateAngle.RotateAngle90 || currentLockedPage.Rotation == PdfPageRotateAngle.RotateAngle180 && currentLockedPage.Size.Width < currentLockedPage.Size.Height) //D5060
            page = unlockedPDF.Pages.Add(currentLockedPage.Size, new PdfMargins(0), PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);

         else if (currentLockedPage.Rotation == PdfPageRotateAngle.RotateAngle90 || currentLockedPage.Rotation == PdfPageRotateAngle.RotateAngle180) //D5060
            page = unlockedPDF.Pages.Add(currentLockedPage.Size, new PdfMargins(0), PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape);

         else
            page = unlockedPDF.Pages.Add(new SizeF(new PointF(0, 0)), new PdfMargins(0)); //Bingo

         page.Canvas.DrawTemplate(template, new PointF(0, 0));
      }

      unlockedPDF.SaveToStream(outputStream);
   }
}


terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Sat May 07, 2022 7:40 am

Hello,

Thanks for your inquiry.
For the issue that page 23 and 26 do not get converted over properly, I used your code and pdf file to do a test, the content of page 23 and 26 is not fully displayed in the resulting file, is that the case with you? And do you want the conversion result to look like the screenshot below? If so, you need to add some code to the first “else if ” in your code, as shown in the screenshot below. In addition, I deleted some unnecessary parameters in your code, and I attached the full code after modifying.

In addition, sorry that we do not have a unified way to operate the various structures of PDF file at present.
If you have any issue, please feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Mon May 16, 2022 10:11 am

Hello,

Hope you are doing well.
Has the solution I provided solved your issue? Could you let me know how is the issue going? Thanks for your assistance in advance.

Sincerely,
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Mon May 16, 2022 8:10 pm

Yes, the code you posted works great.

Thank you.

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Mon May 30, 2022 10:25 am

Hello,

Thanks for your feedback.
I'm glad to hear it worked for you, if you have any issue, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF