create new pdf from a old pdf itextsharp .net MVC

亡梦爱人 提交于 2019-12-23 05:25:18

问题


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....


回答1:


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.



来源:https://stackoverflow.com/questions/44215202/create-new-pdf-from-a-old-pdf-itextsharp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!