【问题标题】:create new pdf from a old pdf itextsharp .net MVC从旧的 pdf itextsharp .net MVC 创建新的 pdf
【发布时间】:2017-05-27 09:28:41
【问题描述】:

只是使用itextsharp ==>我的代码使用旧 pdf 创建一个新 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 度。

下图旧pdf =>

我的新 pdf 看起来像 =>

我不知道为什么它会破坏我的原始 pdf

我试试.. 更新 1

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

还是不行……

专家兄弟...请帮助....

【问题讨论】:

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


    【解决方案1】:

    我不知道为什么它会破坏我的原始 pdf

    实际上,当您的原始 PDF 在显示时请求旋转时,它不是旋转内容。

    更详细:

    PDF格式指定页面属性Rotate

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

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

    另一方面,PdfWriter 方法 GetImportedPage 忽略许多与页面相关的属性,只导入其内容流所描述的页面内容。因此,如果您导入带有非平凡 Rotate 条目的页面,看起来 iText 似乎在旋转页面,而实际上它采用了未旋转的内容。

    在手头的 PDF 中就是这种情况,原始文件中唯一的页面是带有 Rotate 条目的 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();
    

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

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2011-08-29
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多