简体   繁体   English

PDFSharp 正在使用 C# 和 WPF 剪切横向页面

[英]PDFSharp is cutting landscape pages with C# and WPF

thanks for take the time to read my odd issue with PDFSharp.感谢您花时间阅读我关于 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.我当时使用 PDFSharp 库版本 1.50.4000.0(我从 1.3.2 更新并遇到同样的问题)用密码保护 PDF 文件。

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提前感谢您阅读或回答这个问题 =D

*****************************Solved********************************************* ********************************已解决******************** ****************************

I switch to iTextSharp and test a couple of documents and works pretty well I'll share the code for reference:我切换到 iTextSharp 并测试了几个文档,效果很好,我将分享代码以供参考:

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;

                }
            }
        }                      
    }

For those that believe "I switched to iText" is not an answer, I've found a "fix" for PDFSharp.对于那些认为“我切换到 iText”不是答案的人,我找到了 PDFSharp 的“修复”。

Without diving into the source code PDFSharp appears to do a redundant rotation on landscape pages.不深入研究源代码 PDFSharp 似乎在横向页面上进行了冗余旋转。 This corrected the landscape pages in documents I worked with that had both portrait and landscape pages.这更正了我使用的同时具有纵向和横向页面的文档中的横向页面。

PdfPages pageCollection = pdfDoc.Pages;
for (int i = 0; i < pageCollection.Count; i++)
{
    if (pageCollection[i].Orientation.ToString().Equals("Landscape"))
    {
        if (pageCollection[i].Rotate == 90)
        {
            pageCollection[i].Orientation = PageOrientation.Portrait;
        }
    }
}

If you use the source code version of PDFsharp you can make this change in PdfPage.cs to see if it solves your problem:如果您使用 PDFsharp 的源代码版本,您可以在 PdfPage.cs 中进行此更改以查看它是否能解决您的问题:

internal PdfPage(PdfDictionary dict)
    : base(dict)
{
    // Set Orientation depending on /Rotate.
    //int rotate = Elements.GetInteger(InheritablePageKeys.Rotate);
    //if (Math.Abs((rotate / 90)) % 2 == 1)
    //    _orientation = PageOrientation.Landscape;
}

I'd be glad to see feedback if you have to make further changes to get it working.如果您必须进行进一步更改以使其正常工作,我很高兴看到反馈。

See also:另见:
http://forum.pdfsharp.net/viewtopic.php?p=9591#p9591 http://forum.pdfsharp.net/viewtopic.php?p=9591#p9591

I switch to iTextSharp and test a couple of documents and works pretty well I'll share the code for reference in the top.我切换到 iTextSharp 并测试了几个文档,效果很好,我将在顶部分享代码以供参考。

Thank You to all谢谢大家

Since PDFSharp can only properly handle transformations on portrait pages, my work-around was converting the landscape pages to portrait with .page.Rotate = 0 and then applying my transformations while accounting for that the page was now sideways .由于 PDFSharp 只能正确处理纵向页面上的转换,我的解决方法是使用.page.Rotate = 0将横向页面转换为纵向,然后应用我的转换,同时考虑到页面现在是sideways Before saving the file, I converted the page back to landscape with .page.Rotate = 90 .在保存文件之前,我使用.page.Rotate = 90将页面转换回横向。 Worked fine!工作得很好!

I had this same problem with my pages getting cut off.我的页面被切断时遇到了同样的问题。 That's why its important to rotate the pages instead of directly changing the Page.Orientation attribute!这就是为什么旋转页面而不是直接更改Page.Orientation属性很重要的原因!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM