使用iTextSharp将图像和文本放置在特定(绝对)位置

时间:2013-09-20 19:04:36

标签: c# itextsharp

我正在使用iTextSharp生成PDF,并且页面中间有动态内容,这会将我的PdfPTable的底部部分推得太远。有时甚至从页面底部到另一页面。

是否可以将底部PdfPTable定位为当它上方的桌子需要更多垂直空间时不会被推下来?

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。

视图\主页\ Index.cshtml:     

<h2>Create PDF</h2>

@Html.ActionLink("Create the PDF", "CreatePDF", "Home")

</div>

HomeController.cs:

    public ActionResult Index()
    {
        return View();
    }

    public FileStreamResult CreatePDF()
    {
        Stream fileStream = GeneratePDF();
        HttpContext.Response.AddHeader("content-disposition", "inline; filename=MyPDF.pdf");

        var fileStreamResult = new FileStreamResult(fileStream, "application/pdf");
        return fileStreamResult;
    }

    private Stream GeneratePDF()
    {
        var rect = new Rectangle(288f, 144f);
        var document = new Document(rect, 10, 10, 10, 10);
        document.SetPageSize(PageSize.LETTER.Rotate());

        MemoryStream memoryStream = new MemoryStream();
        PdfWriter pdfWriter = PdfWriter.GetInstance(document, memoryStream);
        document.Open();


        ////////////////////////
        //Place Image in a Specific (Absolute) Location
        ////////////////////////
        Image myImage = Image.GetInstance(Server.MapPath("../Content/BI_logo_0709.png"));
        myImage.SetAbsolutePosition(45, 45);
        //[dpi of page]/[dpi of image]*100=[scale percent]
        //72 / 200 * 100 = 36%
        //myImage.ScalePercent(36f);
        myImage.ScalePercent(36f);
        document.Add(myImage);



        ////////////////////////
        //Place Text in a Specific (Absolute) Location
        ////////////////////////

        //Create a table to hold everything
        PdfPTable myTable = new PdfPTable(1);
        myTable.TotalWidth = 200f;
        myTable.LockedWidth = true;

        //Create a paragraph with the text to be placed
        BaseFont bfArialNarrow = BaseFont.CreateFont(Server.MapPath("../Content/ARIALN.ttf"), BaseFont.CP1252, BaseFont.EMBEDDED);
        var basicSmaller = new Font(bfArialNarrow, 10);
        var myString = "Hello World!" +
                            Environment.NewLine +
                            "Here is more text." +
                            Environment.NewLine +
                            "Have fun programming!";
        var myParagraph = new Paragraph(myString, basicSmaller);

        //Create a cell to hold the text aka paragraph
        PdfPCell myCell = new PdfPCell(myParagraph);
        myCell.Border = 0;

        //Add the cell to the table
        myTable.AddCell(myCell);

        //Add the table to the document in a specific (absolute) location
        myTable.WriteSelectedRows(0, -1, 550, 80, pdfWriter.DirectContent);




        pdfWriter.CloseStream = false;
        document.Close();
        memoryStream.Position = 0;
        return memoryStream;
    }