NoPassReversePages.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Pdf.Security;
  10. using GrapeCity.Documents.Text;
  11.  
  12. namespace DsPdfWeb.Demos
  13. {
  14. // This example shows how to load a password protected PDF without specifying the password,
  15. // and change (reverse) the order of the pages in the PDF.
  16. // The modified PDF is saved and re-opened with the password so that we can show it in the demo.
  17. public class NoPassReversePages
  18. {
  19. public int CreatePDF(Stream stream)
  20. {
  21. using var fsSrc = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands-password-user.pdf"));
  22. // Set up DecryptionOptions to allow loading password protected PDFs without password:
  23. var dopt = new DecryptionOptions() { ThrowExceptionIfInvalidPassword = false };
  24. var docSrc = new GcPdfDocument();
  25. docSrc.Load(fsSrc, dopt);
  26.  
  27. for (int i = 0; i < docSrc.Pages.Count / 2; ++i)
  28. docSrc.Pages.Swap(i, docSrc.Pages.Count - 1 - i);
  29.  
  30. // Demo site specific:
  31. // We save the modified password protected document to a temp file,
  32. // and load it again with the password, so that the demo site can show it
  33. // without asking the user for a password (the block is to delete the temp file):
  34. var fn = Path.GetTempFileName();
  35. {
  36. docSrc.Save(fn);
  37. var doc = new GcPdfDocument();
  38. using var fs = File.OpenRead(fn);
  39. doc.Load(fs, "user");
  40. doc.Save(stream);
  41. }
  42. File.Delete(fn);
  43. return docSrc.Pages.Count;
  44. }
  45. }
  46. }
  47.