从旧的pdf itextsharp .net MVC创建新的pdf [英] create new pdf from a old pdf itextsharp .net MVC

查看:214
本文介绍了从旧的pdf itextsharp .net MVC创建新的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用旧版pdf使用



和我的新pdf看起来像=>



我不知道为什么腐烂我的原始pdf



我试试..
更新1

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

仍然无效......



expert brothes ...请帮助....

解决方案


我不知道它为什么会腐烂我的原始pdf


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



更详细:



PDF格式指定页面属性旋转


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



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


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



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

  4 0 obj 
<<
/类型/页
/ MediaBox [0 0 595 842]
/旋转90
...
>>

因此,如果要通过轮播导入它,则必须考虑其轮换:

  PdfReader reader = new PdfReader(original); 
Rectangle size = reader.GetPageSizeWithRotation(1);
凭证凭证=新凭证(大小);

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();

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


just creating a new pdf using old pdf using itextsharp ==>my code...

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

problem is it rotating my original pdf like 90 degree.

below pic old pdf =>

and my new pdf looks like =>

i dont know why its rotsting my originial pdf

i try .. update 1

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

still not working....

expert brothes ... help please....

解决方案

i dont know why its rotsting my originial pdf

Actually it is not rotating the content while your original PDF requests rotation when being displayed.

In more detail:

The PDF format specifies a page property Rotate:

Rotate integer (Optional; inheritable) The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90. Default value: 0.

(ISO 32000-1 Table 30 – Entries in a page object)

The PdfWriter method GetImportedPage, on the other hand, ignores many page related properties and only imports the page content as described by its content stream. If you import a page with a non-trivial Rotate entry, therefore, it appears as if iText rotates the page while actually it takes the unrotated content.

In the PDF at hand this is the case, the only page in the original file is a A4 page with a Rotate entry:

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

Thus, if you want to import it with rotation, you have to take its rotation into account:

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();

As you see one needs a translation in addition to the rotation. This is due to the fact that the rotation would be executed around the coordinate system origin which here is the lower left corner. Without translation, therefore, the page content would be rotated out of the media box.

这篇关于从旧的pdf itextsharp .net MVC创建新的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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