【问题标题】:Blank PDF generate when WriteCompatiblePdf method call of ITextSharp调用 ITextSharp 的 WriteCompatiblePdf 方法时生成空白 PDF
【发布时间】:2014-05-19 11:06:24
【问题描述】:

您好,我正在使用以下代码。当我上传一些包含某些内容的 PDF 时,上传后,它们在上传的 PDF 中没有内容。上传的 PDf 为空白。我正在使用 ItextSharp 的以下方法将原始 PDF 的版本更改为定义的版本。

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

有什么建议吗?

【问题讨论】:

  • 您上传的所有 PDF 最终都没有内容吗?还是只是一些?你能提供一个损坏的样本吗?话虽如此,请注意您的内容复制代码非常有损;您应该使用 PdfReader-PdfStamper 对而不是 PdfReader-PdfWriter 对。
  • 某些 pdf 文件会出现这种情况。执行此代码时我没有收到任何错误。但是对于 1 或 2 个 pdf,我发现了这个问题。
  • 如果您的问题只出现在某些 PDF 中,您应该提供一个示例 PDF 来说明。否则其他人只能猜测。
  • 你好 mkl,请找到 pdf 的链接。 drive.google.com/file/d/0B4KYV-9yFCBSQk9IRnE5Tl8yY2s/…,如您所见,如果您上传此 pdf,则 pdf 变为空白。
  • 嗯,文件看起来不是空白的,但看起来好像缺少某些东西。好像有一个填写好的表格,现在只有填写。这也是你的观察吗?

标签: c# pdf


【解决方案1】:

OP 提供了一个示例“空白”文件。

事实证明,这个“空白”文件确实包含所需的内容,只是离页面太远了。

详情

文档的页面有这个媒体框定义:

/MediaBox[0 7072 612 7864]

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

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

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

决议

有不同的方法可以解决这个问题:

  1. 在坐标正确的位置添加内容,即使用

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

    其中xyLeftBottomreader.GetPageSizeWithRotation(1)Rectangle);或

  2. Document构造函数的页面大小矩形标准化为基于0,0;或

  3. 使用PdfStamperPdfReader 而不是PdfWriter,并使用它来选择所需的版本。

【讨论】:

    【解决方案2】:

    这是因为您没有设置任何要写入 PDF 的文本。这是一个如何编写文本的简单示例

     string newFile = @"C:\the path\file.pdf";
     Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);
     PdfWriter write = PdfWriter.GetInstance(doc, new FileStream(newFile,  
     FileMode.Create));    
    
     doc.Open(); // open the connection
     Paragraph p1 = new Paragraph("text to be displayed in the first paragraph");
    doc.Add(p1); // close the connection
    

    link 将告诉您从更高级的 iTextSharp 编写的正确方法

    【讨论】:

    • 我认为,您没有正确查看代码。他们已经是那个代码,即 iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));已经得到源pdf的内容,然后就对了。
    猜你喜欢
    • 1970-01-01
    • 2012-09-13
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多