Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PdfSmartCopy正在复制新PDF文件底部(页脚)的内容_C#_Pdf_Itextsharp - Fatal编程技术网

C# PdfSmartCopy正在复制新PDF文件底部(页脚)的内容

C# PdfSmartCopy正在复制新PDF文件底部(页脚)的内容,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我有一个功能是裁剪pdf文件的特定部分并将其添加到新的pdf文件中,但我遇到的主要问题是,它将页面的裁剪部分显示在新创建的pdf文件的底部(页脚)。 这是密码 public static void CropPdfFile(string sourceFilePath, string outputFilePath) { // Allows PdfReader to read a pdf document without the owner's password PdfReader.u

我有一个功能是裁剪pdf文件的特定部分并将其添加到新的pdf文件中,但我遇到的主要问题是,它将页面的裁剪部分显示在新创建的pdf文件的底部(页脚)。 这是密码

public static void CropPdfFile(string sourceFilePath, string outputFilePath)
{
    // Allows PdfReader to read a pdf document without the owner's password
    PdfReader.unethicalreading = true;

    // Reads the PDF document
    using (PdfReader pdfReader = new PdfReader(sourceFilePath))
    {
        // Set which part of the source document will be copied.
        // PdfRectangel(bottom-left-x, bottom-left-y, upper-right-x, upper-right-y)

        PdfRectangle rect = new PdfRectangle(0f, 9049.172f, 594.0195f, 700.3f);

        using (var output = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write))
        {
            // Create a new document
            using (Document doc = new Document())
            {

                // Make a copy of the document
                PdfSmartCopy smartCopy = new PdfSmartCopy(doc, output);

                // Open the newly created document
                doc.Open();              

                // Loop through all pages of the source document
                for (int i = 4; i <= pdfReader.NumberOfPages; i++)
                {
                    // Get a page
                    var page = pdfReader.GetPageN(i);

                    // Apply the rectangle filter we created
                      page.Put(PdfName.CROPBOX, rect);
                      page.Put(PdfName.MEDIABOX, rect);

                    // Copy the content and insert into the new document

                      smartCopy.SetLinearPageMode();

                    var copiedPage = smartCopy.GetImportedPage(pdfReader, i);
                    smartCopy.AddPage(copiedPage);

                }

                // Close the output document
                doc.Close();
            }
        }
    }
publicstaticvoidcropdffile(stringsourcefilepath、stringoutputfilepath)
{
//允许PdfReader在没有所有者密码的情况下读取pdf文档
PdfReader.unethicalreading=true;
//阅读PDF文档
使用(PdfReader PdfReader=newpdfreader(sourceFilePath))
{
//设置将复制源文档的哪一部分。
//PdfRectangel(左下角x、左下角y、右上角x、右上角y)
PDF矩形=新的PDF矩形(0f,9049.172f,594.0195f,700.3f);
使用(var output=newfilestream(outputFilePath,FileMode.CreateNew,FileAccess.Write))
{
//创建新文档
使用(文档文档=新文档())
{
//复印一份文件
PdfSmartCopy smartCopy=新的PdfSmartCopy(文档,输出);
//打开新创建的文档
doc.Open();
//循环浏览源文档的所有页面
对于(int i=4;iPDF页面的大小(以用户单位表示)取决于媒体框的值。例如:A4页面的媒体框通常定义如下
[0 0 595 842]

在这种情况下,坐标系(0,0)的原点与左下角重合。右上角的坐标是(595842)

A4页面的另一个可能值是
[0 842 595 1684]
。相同的宽度(595个用户单位),相同的高度(1684-842=842个用户单位),但左下角现在有坐标(0,842),右上角的坐标是(595,1684)

您编写了一个使用以下参数创建的
PdfRectangle
(左下角x、左下角y、右上角x、右上角y)。但是,您使用的是这些硬编码值:
0f、9049.172f、594.0195f、700.3f

你的左下角(9049.172)比你右上角(700.3)的位置高。这是没有意义的。因此:你应该考虑把这个值变为有意义的东西。它应该是什么值,只是你可以回答的问题,因为只有你知道你要种植的文件的MyIATOX的值。 在您的评论中,您解释说您的PDF是A4页面。您可以使用名为

getPageSize()
PdfReader
方法来检查这一点。如果您希望裁剪页面以便只看到文档的标题,则需要使用类似以下内容:

PdfRectangle rect = new PdfRectangle(0f, 842 - x, 595, 842);
其中
x
是标头的高度。例如,如果标头为100个用户单位,则需要:

PdfRectangle rect = new PdfRectangle(0f, 742, 595, 842);
不清楚你为什么总是说100px。如果你想把像素转换成点,请阅读


使用上面提到的公式,100像素等于75点。

在新创建的pdf文件的底部(页脚)显示页面的裁剪部分-您能说明您的期望和得到的结果吗?现在您的描述听起来好像矩形坐标对于您的任务来说是错误的(0楼的左下角,9049.172f看起来有点奇怪)。@mkl是的,我正在尝试(裁剪)pdf页面的顶部,并将其添加到新创建的pdf页面的顶部。使用当前代码裁剪部分可以,但它正在添加到新创建的pdf页面的底部。实际上,我也不知道pdf坐标。因此,我应该做什么来完成这件事…当我使用[0 0 595 842]我看到整个页面被裁剪了,如果我是对的话,这表明页面是A4大小的。事实上,先生,我是一个网页开发人员,所以不知道坐标系(只有高中时学习的那么多).因此,如果我需要从页面上部裁剪100%宽度100px;高度。坐标系是什么..我不明白“100%宽度100px”是什么意思。单位
px
代表“像素”.PDF不是以像素为单位的,而是以用户单位为单位的。100%是相对测量值,而100px是绝对大小。就好像我说的是“荷兰语”,而你说的是“葡萄牙语”.我们理解彼此的一些单词,但我们不知道彼此在说什么。好吧。我明白你的意思。我有一个疑问,我的维度中的哪个部分将被裁剪的部分放在页面的下端?`PdfRectangle rect=new PdfRectangle(0,0,595,842)这一行复制了整个pdf页面。所以它告诉我们页面是A4大小。现在,请原谅我,先生,因为我是一名网络开发人员,如果我想裁剪100像素;请将100像素的web转换为pdf用户单位的近似值,结果坐标是什么?只有你知道。假设我问你:“我显示了多少个手指?”你能回答这个问题吗?不,你不能,你必须猜测。好吧,你的问题也是如此:你有PDF,我只能猜测。你要我选择一个介于0和842之间的数字。你不明白吗???