【问题标题】:PDFSharp is cutting landscape pages with C# and WPFPDFSharp 正在使用 C# 和 WPF 剪切横向页面
【发布时间】:2016-04-01 17:47:47
【问题描述】:

感谢您花时间阅读我关于 PDFSharp 的奇怪问题。

我当时使用 PDFSharp Library 版本 1.50.4000.0(我从 1.3.2 更新并遇到同样的问题)用密码保护 PDF 文件。

该程序非常适用于纵向文档,但有时我的文档具有混合方向。

但是当横向页面在文档中时,页面总是被剪切。

供您参考的代码:

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;

提前感谢您抽出宝贵时间阅读或回答此问题 =D

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

我切换到 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;

                }
            }
        }                      
    }

【问题讨论】:

标签: c# landscape pdfsharp


【解决方案1】:

对于那些认为“我切换到 iText”不是答案的人,我找到了 PDFSharp 的“修复”。

在不深入研究源代码的情况下,PDFSharp 似乎在横向页面上进行了冗余旋转。这更正了我使用的同时具有纵向和横向页面的文档中的横向页面。

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;
        }
    }
}

【讨论】:

  • 这应该被接受的答案:它解释了为什么会发生和解决方法
【解决方案2】:

如果您使用 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;
}

如果您必须进行进一步的更改以使其正常工作,我很高兴看到反馈。

另请参阅:
http://forum.pdfsharp.net/viewtopic.php?p=9591#p9591

【讨论】:

  • 我切换到itextsharp,因为我没有源代码版本=(,我将代码分享在顶部,感谢@ThomasH的回答
【解决方案3】:

我切换到 iTextSharp 并测试了几个文档,效果很好。我将在顶部分享代码以供参考。

谢谢大家

【讨论】:

    【解决方案4】:

    由于 PDFSharp 只能正确处理纵向页面上的转换,我的解决方法是使用 .page.Rotate = 0 将横向页面转换为纵向页面,然后应用我的转换同时考虑到页面现在 横向 .在保存文件之前,我使用.page.Rotate = 90 将页面转换回横向。效果很好!

    我遇到了这个同样的问题,我的页面被截断了。这就是为什么旋转页面而不是直接更改Page.Orientation 属性很重要的原因!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-03
      • 2015-11-06
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      相关资源
      最近更新 更多