从旧的pdf itextsharp .net MVC创建新的pdf

时间:2017-05-27 09:28:41

标签: .net asp.net-mvc-4 pdf itext

使用旧版pdf使用itextsharp ==>我的代码创建新的pdf ...

    public void certificate()
    {
        //get user info using UserId from database

        //UserDetail UserDetail = db.UserDetails.Where(x => x.UserId == UserId).FirstOrDefault();
        string oldFile = Server.MapPath("~/Content/img/tsms/Certificate/Certificate-of-Completion-Award-Template-Blue.pdf");
        string newFile = Server.MapPath("~/Content/img/tsms/Certificate/newFile.pdf");

        // open the reader
        PdfReader reader = new PdfReader(oldFile);
        Rectangle size = reader.GetPageSizeWithRotation(1);
        Document document = new Document(size);
        document.SetPageSize(PageSize.A4);

        // open the writer
        FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
        PdfWriter writer = PdfWriter.GetInstance(document, fs);
        document.Open();

        // the pdf content
        PdfContentByte cb = writer.DirectContent;

        // select the font properties
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.SetColorFill(BaseColor.DARK_GRAY);
        cb.SetFontAndSize(bf, 8);

        //// write the text in the pdf content
        //cb.BeginText();
        //string text = "Some random blablablabla...";
        //// put the alignment and coordinates here
        //cb.ShowTextAligned(1, text, 520, 640, 0);
        //cb.EndText();

        //// write the text in the pdf content
        //cb.BeginText();
        //text = "Other random blabla...";
        //// put the alignment and coordinates here
        //cb.ShowTextAligned(2, text, 100, 200, 0);
        //cb.EndText();

        // create the new page and add it to the pdf
        PdfImportedPage page = writer.GetImportedPage(reader, 1);
        cb.AddTemplate(page, 0, 0);

        // close the streams and voilá the file should be changed :)
        document.Close();
        fs.Close();
        writer.Close();
        reader.Close();
    }

    public ActionResult Print()
    {
        certificate();
        return null;
    }

问题是它将我原来的pdf旋转为90度。

低于pic old pdf =>

old pdf pic

我的新pdf看起来像=> new pdf pic

我不知道为什么它最原始的pdf

我试试.. 更新1

document.SetPageSize(PageSize.A4.Rotate());

仍然无法正常工作....

请专家帮忙......

1 个答案:

答案 0 :(得分:0)

  

我不知道为什么它最原始的pdf

实际上,当原始PDF在显示时请求旋转时,旋转内容。

更详细:

PDF格式指定页面属性旋转

  

<强>旋转   整数   (可选;可继承)显示或打印时页面顺时针旋转的度数。该值应为90的倍数。默认值:0。

     

(ISO 32000-1表30 - 页面对象中的条目)

另一方面,PdfWriter方法GetImportedPage忽略了许多与页面相关的属性,并且只按其内容流的描述导入页面内容。因此,如果您导入的页面包含非平凡的旋转条目,则看起来好像iText会旋转页面,而实际上它会显示未旋转的内容。

在手边的PDF中就是这种情况,原始文件中唯一的页面是带有旋转条目的A4页面:

4 0 obj
<<
  /Type/Page
  /MediaBox[ 0 0 595 842]
  /Rotate 90
  ...
>>

因此,如果要使用旋转导入它,则必须考虑其旋转:

PdfReader reader = new PdfReader(original);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);

FileStream fs = new FileStream(result, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();

PdfContentByte cb = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(reader, 1);
AffineTransform transform = AffineTransform.GetRotateInstance(-Math.PI / 2);
transform.Translate(-document.PageSize.Height, 0);
cb.AddTemplate(page, transform);

document.Close();
reader.Close();

如您所见,除旋转外还需要翻译。这是因为旋转将围绕坐标系原点执行,这里是左下角。因此,如果没有翻译,页面内容将被旋转出媒体框。