ITextSharp的WriteCompatiblePdf方法调用时生成空白PDF [英] Blank PDF generate when WriteCompatiblePdf method call of ITextSharp

查看:218
本文介绍了ITextSharp的WriteCompatiblePdf方法调用时生成空白PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码.当我上载包含某些内容的PDF时,上载后,它们在上载的PDF上不存在任何内容.上载的PDf为空白.我正在使用ItextSharp的以下方法将原始PDF的版本更改为定义的版本".

Hi i am using below code. When i upload some of PDF's which contain some content and after uploading, their is no content on uploaded PDF. Uploaded PDf is blank. I am using below method of ItextSharp for change the version of Orginial PDF to Defined Version.

private int WriteCompatiblePdf(string fileName, FileUpload filePath)
        {
            string sNewPdf = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["InterfaxPath"]) + fileName;
            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(filePath.FileBytes);
            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            // step 1: creation of a document-object
            iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(sNewPdf, FileMode.Create, FileAccess.ReadWrite));
            //write pdf that pdfsharp can understand
            writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
            // step 3: we open the document
            document.Open();
            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            iTextSharp.text.pdf.PdfImportedPage page;
            int rotation;
            int i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);                    
                cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);                    
            }
            // step 5: we close the document
            document.Close();
            return n;
        }

有什么建议吗?

推荐答案

OP提供了一个示例空白"文件.

The OP provided a sample "blank" file.

事实证明,此空白"文件确实包含所需的内容,只是页面外.

As it turns out, this "blank" file does contain the desired content, it merely is way off-page.

文档的页面具有以下媒体框定义:

The pages of the document have this media box definition:

/MediaBox[0 7072 612 7864]

因此,可见区域的x坐标为0..612,y坐标为7072..7864.不过,在添加导入的页面内容时,OP会将其显式地锚定在坐标0,0上:

Thus, the visible area has x coordinates in 0..612 and y coordinates in 7072..7864. When adding the imported page contents, though, the OP explicitly anchors them explicitly at the coordinates 0,0:

cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);

因此,前一页的内容将添加到y坐标为0..792的区域中.因此,它们是不可见的.

The contents of the former page, therefore, are added in the area with y coordinates 0..792. So, they are not visible.

有多种方法可以解决此问题:

There are different ways to resolve this issue:

  1. 将内容添加到具有正确坐标的位置,即使用

  1. add the contents at the position with the correct coordinates, i.e. use

cb.AddTemplate(page,1f,0,0,1f,x,y);

cb.AddTemplate(page, 1f, 0, 0, 1f, x, y);

其中xyreader.GetPageSizeWithRotation(1)LeftBottom(a Rectangle);或

where x and y are Left and Bottom of reader.GetPageSizeWithRotation(1) (a Rectangle); or

标准化Document构造函数的页面大小矩形,使其以0,0为基础;或

normalize the page size rectangle for the Document constructor to be based in 0,0; or

使用PdfStamperPdfReader代替PdfWriter,并使用它选择所需的版本.

use a PdfStamper with the PdfReader instead of the PdfWriter and use it to select the desired version.

这篇关于ITextSharp的WriteCompatiblePdf方法调用时生成空白PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆