开发者

Itextsharp: Adjust 2 elements on exactly one page

So, I'm having this problem using C# (.NET 4.0 + WinForms) and iTextSharp 5.1.2.

I have some scanned images stored on a DB and need to build on the fly PDF with those images. Some files have just one page and other ones hundreds. That is working just fine using:

    foreach (var page in pages)
    {
        I开发者_如何学运维mage pageImage = Image.GetInstance(page.Image);
        pageImage.ScaleToFit(document.PageSize.Width,document.PageSize.Height);
        pageImage.Alignment = Image.ALIGN_TOP | Image.ALIGN_CENTER;
        document.Add(pageImage);
        document.NewPage();
        //...
    }

The problem is:

I need to add an small table at the bottom of the last page.

I try:

    foreach (var page in pages)
    {
        Image pageImage = Image.GetInstance(page.Image);
        pageImage.ScaleToFit(document.PageSize.Width,document.PageSize.Height);
        pageImage.Alignment = Image.ALIGN_TOP | Image.ALIGN_CENTER;
        document.Add(pageImage);
        document.NewPage();
        //...
    }
    Table t = new table....
    document.Add(t);

The table is successfully added but IF the size of the image fits the page size of the document then the table is added on the next page.

I need to resize the last image of the document (if it has multiple ones, or the first if has only 1) in order to put the table directly on that page (with the image) and that both ocuppy just one page.

I try to scale the image by percent BUT given that the image size of the image that'll be on the last page is unknow and that it must FILL the biggest portion of the page I need to do that dinamically.

Any idea?


Let me give you a couple of things that might help you and then I'll give you a full working example that you should be able to customize.

The first thing is that the PdfPTable has a special method called WriteSelectedRows() that allows you to draw a table at an exact x,y coordinate. It has six overloads but the most commonly used one is probably:

PdfPTable.WriteSelectedRows(int rowStart,int rowEnd, float xPos, float yPos, PdfContentByte canvas)

To place a table with the upper left corner positioned at 400,400 you would call:

t.WriteSelectedRows(0, t.Rows.Count, 400, 400, writer.DirectContent);

Before calling this method you are required to set the table's width using SetTotalWidth() first:

//Set these to your absolute column width(s), whatever they are.
t.SetTotalWidth(new float[] { 200, 300 });

The second thing is that the height of the table isn't known until the entire table is rendered. This means that you can't know exactly where to place a table so that it truly is at the bottom. The solution to this is to render the table to a temporary document first and then calculate the height. Below is a method that I use to do this:

    public static float CalculatePdfPTableHeight(PdfPTable table)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (Document doc = new Document(PageSize.TABLOID))
            {
                using (PdfWriter w = PdfWriter.GetInstance(doc, ms))
                {
                    doc.Open();

                    table.WriteSelectedRows(0, table.Rows.Count, 0, 0, w.DirectContent);

                    doc.Close();
                    return table.TotalHeight;
                }
            }
        }
    }

This can be called like this:

        PdfPTable t = new PdfPTable(2);
        //In order to use WriteSelectedRows you need to set the width of the table
        t.SetTotalWidth(new float[] { 200, 300 });
        t.AddCell("Hello");
        t.AddCell("World");
        t.AddCell("Test");
        t.AddCell("Test");

        float tableHeight = CalculatePdfPTableHeight(t);

So with all of that here's a full working WinForms example targetting iTextSharp 5.1.1.0 (I know you said 5.1.2 but this should work just the same). This sample looks for all JPEGs in a folder on the desktop called "Test". It then adds them to an 8.5"x11" PDF. Then on the last page of the PDF, or if there's only 1 JPEG to start with on the only page, it expands the height of the PDF by however tall the table that we're adding is and then places the table at the bottom left corner. See the comments in the code itself for further explanation.

using System;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace Full_Profile1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static float CalculatePdfPTableHeight(PdfPTable table)
        {
            //Create a temporary PDF to calculate the height
            using (MemoryStream ms = new MemoryStream())
            {
                using (Document doc = new Document(PageSize.TABLOID))
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, ms))
                    {
                        doc.Open();

                        table.WriteSelectedRows(0, table.Rows.Count, 0, 0, w.DirectContent);

                        doc.Close();
                        return table.TotalHeight;
                    }
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //Create our table
            PdfPTable t = new PdfPTable(2);
            //In order to use WriteSelectedRows you need to set the width of the table
            t.SetTotalWidth(new float[] { 200, 300 });
            t.AddCell("Hello");
            t.AddCell("World");
            t.AddCell("Test");
            t.AddCell("Test");

            //Calculate true height of the table so we can position it at the document's bottom
            float tableHeight = CalculatePdfPTableHeight(t);

            //Folder that we are working in
            string workingFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test");

            //PDF that we are creating
            string outputFile = Path.Combine(workingFolder, "Output.pdf");

            //Get an array of all JPEGs in the folder
            String[] AllImages = Directory.GetFiles(workingFolder, "*.jpg", SearchOption.TopDirectoryOnly);

            //Standard iTextSharp PDF init
            using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document document = new Document(PageSize.LETTER))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(document, fs))
                    {
                        //Open our document for writing
                        document.Open();

                        //We do not want any margins in the document probably
                        document.SetMargins(0, 0, 0, 0);

                        //Declare here, init in loop below
                        iTextSharp.text.Image pageImage;

                        //Loop through each image
                        for (int i = 0; i < AllImages.Length; i++)
                        {
                            //If we only have one image or we are on the second to last one
                            if ((AllImages.Length == 1) | (i == (AllImages.Length - 1)))
                            {
                                //Increase the size of the page by the height of the table
                                document.SetPageSize(new iTextSharp.text.Rectangle(0, 0, document.PageSize.Width, document.PageSize.Height + tableHeight));
                            }

                            //Add a new page to the PDF
                            document.NewPage();

                            //Create our image instance
                            pageImage = iTextSharp.text.Image.GetInstance(AllImages[i]);
                            pageImage.ScaleToFit(document.PageSize.Width, document.PageSize.Height);
                            pageImage.Alignment = iTextSharp.text.Image.ALIGN_TOP | iTextSharp.text.Image.ALIGN_CENTER;
                            document.Add(pageImage);

                            //If we only have one image or we are on the second to last one
                            if ((AllImages.Length == 1) | (i == (AllImages.Length - 1)))
                            {
                                //Draw the table to the bottom left corner of the document
                                t.WriteSelectedRows(0, t.Rows.Count, 0, tableHeight, writer.DirectContent);
                            }

                        }

                        //Close document for writing
                        document.Close();
                    }
                }
            }

            this.Close();
        }
    }
}

EDIT

Below is an edit based on your comments. I'm only posting the contents of the for loop which is the only part that changed. When calling ScaleToFit you just need to take tableHeight into account.

                    //Loop through each image
                    for (int i = 0; i < AllImages.Length; i++)
                    {
                        //Add a new page to the PDF
                        document.NewPage();

                        //Create our image instance
                        pageImage = iTextSharp.text.Image.GetInstance(AllImages[i]);

                        //If we only have one image or we are on the second to last one
                        if ((AllImages.Length == 1) | (i == (AllImages.Length - 1)))
                        {
                            //Scale based on the height of document minus the table height
                            pageImage.ScaleToFit(document.PageSize.Width, document.PageSize.Height - tableHeight);
                        }
                        else
                        {
                            //Scale normally
                            pageImage.ScaleToFit(document.PageSize.Width, document.PageSize.Height);
                        }

                        pageImage.Alignment = iTextSharp.text.Image.ALIGN_TOP | iTextSharp.text.Image.ALIGN_CENTER;
                        document.Add(pageImage);

                        //If we only have one image or we are on the second to last one
                        if ((AllImages.Length == 1) | (i == (AllImages.Length - 1)))
                        {
                            //Draw the table to the bottom left corner of the document
                            t.WriteSelectedRows(0, t.Rows.Count, 0, tableHeight, writer.DirectContent);
                        }

                    }


Just use a method table.CalculateHeights() if you want to know the height of table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜