Search code examples
c#landscapepdfsharp

PDFSharp is cutting landscape pages with C# and WPF


thanks for take the time to read my odd issue with PDFSharp.

I use PDFSharp Library Version 1.50.4000.0 (I update from 1.3.2 and have the same issue) at the time to protect a PDF file with a password.

The program works very good with portrait documents but some times I have documents with mixed orientations.

But all the times when a landscape page it's in the document the page is cutted.

The code for your reference:

PdfDocument document = PdfReader.Open(System.IO.Path.Combine("H:/Bloq1/", file.Name), "PasswordHere");

                    PdfSecuritySettings securitySettings = document.SecuritySettings;
                    securitySettings.OwnerPassword = "PasswordHere";


                    securitySettings.PermitAccessibilityExtractContent = false;
                    securitySettings.PermitAnnotations = false;
                    securitySettings.PermitAssembleDocument = false;
                    securitySettings.PermitExtractContent = false;
                    securitySettings.PermitFormsFill = true;
                    securitySettings.PermitFullQualityPrint = true;
                    securitySettings.PermitModifyDocument = false;
                    securitySettings.PermitPrint = true;

                    document.Save(System.IO.Path.Combine("H:/Bloq1/", file.Name));

                    tbx.Text = "Complete";
                    tbx.Background = Brushes.ForestGreen;

Thanks in advance for your time reading or answering this question =D

*****************************Solved*********************************************

I switch to iTextSharp and test a couple of documents and works pretty well I'll share the code for reference:

private void Button_Full(object sender, RoutedEventArgs e)//PROTEGE PDF PERMITIENDO IMPRESION
    {
        string Password = "password";

        DirectoryInfo dir = new DirectoryInfo("H:/Bloq1/");

        if(dir.GetFiles("*.pdf").Length ==0)
        {
            MessageBox.Show("There are no files in the default directory", "No Files", MessageBoxButton.OK, MessageBoxImage.Warning);
            tbx.Background = Brushes.OrangeRed;
            tbx.Text = "No Files Found";
        }
        else
        {
            tbx.Background = Brushes.White;
            tbx.Text = "Protecting....";

            foreach (FileInfo file in dir.GetFiles("*.pdf"))
            {
                try
                {     
                    string InputFile = System.IO.Path.Combine("H:/Bloq1/", file.Name);
                    string OutputFile = System.IO.Path.Combine("H:/Bloq1/", "@"+file.Name);
                    using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
                        {
                            PdfReader.unethicalreading = true;
                            PdfReader reader = new PdfReader(input);                                
                            PdfEncryptor.Encrypt(reader, output, true, null, Password, PdfWriter.AllowPrinting);                                 

                        }
                    }

                    file.Delete();
                    File.Move(@"H:\Bloq1\@"+file.Name, @"H:\Bloq1\"+file.Name);

                    tbx.Text = "Full Protected";
                    tbx.Background = Brushes.ForestGreen;
                }
                catch (Exception ex)
                {
                    tbx.Text = "Error in: " + file.Name + ex;
                    tbx.Background = Brushes.IndianRed;

                }
            }
        }                      
    }

Solution

  • I switch to iTextSharp and test a couple of documents and works pretty well I'll share the code for reference in the top.

    Thank You to all