2

I have requirement to convert images into PDF with a template. I'm able to create one page PDF using itextsharp. Assumption 1 image for 1 page PDF. The problem comes when I have multiple images. How I can create multiple pages PDF with predefined template. Below is my code:

public static string CreatePDFDocument(string docname, string imagePath, string mediaField)
    {
        PdfReader pdfReader = null;
        string pdfPortrait = ConfigurationManager.AppSettings["PdfPortraitTemplate"];
        string pdfLandscape = ConfigurationManager.AppSettings["PdfLandscapeTemplate"];

        iTextSharp.text.Image instanceImg = iTextSharp.text.Image.GetInstance(imagePath);

        string filename = ConfigurationManager.AppSettings["LocalFolder"] + docname + ".pdf";

        FileStream pdfOutputFile = new FileStream(filename, FileMode.Create);

        if (instanceImg.ScaledHeight >= instanceImg.ScaledWidth)
        {
            pdfReader = new PdfReader(pdfPortrait);
        }
        else
        {
            pdfReader = new PdfReader(pdfLandscape);
        }

        PdfStamper pdfStamper = null;
        pdfStamper = new PdfStamper(pdfReader, pdfOutputFile);

        AcroFields testForm = pdfStamper.AcroFields;
        testForm.SetField("MediaField", mediaField);

        PdfContentByte overContent = pdfStamper.GetOverContent(1);
        IList<AcroFields.FieldPosition> fieldPositions = testForm.GetFieldPositions("ImageField");

        if (fieldPositions == null || fieldPositions.Count <= 0) throw new ApplicationException("Error locating field");
        AcroFields.FieldPosition fieldPosition = fieldPositions[0];
        iTextSharp.text.Rectangle imageRect = new Rectangle(fieldPosition.position.Top, fieldPosition.position.Left, fieldPosition.position.Bottom, fieldPosition.position.Right);

        instanceImg.ScaleToFit(imageRect.Height, -1 * imageRect.Width);
        instanceImg.SetAbsolutePosition(fieldPosition.position.Left, (fieldPosition.position.Top - (instanceImg.ScaledHeight)));

        overContent.AddImage(instanceImg);
        pdfStamper.FormFlattening = true;

        pdfStamper.Close();
        pdfReader.Close();

        return filename;
    }

Thanks in advance!

  • 1
    Do you have an absolute requirement to use existing PDFs as a base, and even more importantly, do you really need `AcroFields`? If you just want to add (convert) images to a PDF you can do it with much less code and can easily support multiple pages. – Chris Haas Dec 10 '13 at 14:12
  • @ChrisHaas , yes I really need 'AcroFields', because there is some field to be filled in template as image information. Existing PDF it was important because it was the template and has header. So final document, it's not only image but image with header template and some information about image. – febriyana.sudrajat Dec 11 '13 at 00:23
  • Okay, that's good to know. If you didn't need `AcroFields` but wanted to use templates still I would probably recommend switching to `GetUnderContent()` which allows you to draw your image behind the template's header. You could also use page events as in this official example: http://itextpdf.com/examples/iia.php?id=114. But because you have to deal with form fields I'd really recommend just creating multiple PDFs and merging them together at the end. – Chris Haas Dec 11 '13 at 14:04
  • Okay @ChrisHaas, thanks for your information. It's good to know, but for my case, I have found solution with pdfConcatenate. I have posted my final code as answer below. – febriyana.sudrajat Dec 11 '13 at 14:09

1 Answers1

3

Finally I found the solution,

Here is my final code

        public static string MainStamping(string docname, List<string> imagePath, string mediaField)
        {
            string filename = ConfigurationManager.AppSettings["LocalFolder"] + docname + ".pdf";

            FileStream pdfOutputFile = new FileStream(filename, FileMode.Create);
            PdfConcatenate pdfConcatenate = new PdfConcatenate(pdfOutputFile);

            PdfReader result = null;

            for (int i = 0; i < imagePath.Count; i++)
            {
                result = CreatePDFDocument1(imagePath[i], mediaField);
                pdfConcatenate.AddPages(result);
            }

            pdfConcatenate.Close();
            return filename;
        }

        public static PdfReader CreatePDFDocument1(string imagePath, string mediaField)
        {
            PdfReader pdfReader = null;
            string pdfPortrait = ConfigurationManager.AppSettings["PdfPortraitTemplate"];
            string pdfLandscape = ConfigurationManager.AppSettings["PdfLandscapeTemplate"];

            iTextSharp.text.Image instanceImg = iTextSharp.text.Image.GetInstance(imagePath);

            if ((instanceImg.ScaledHeight >= instanceImg.ScaledWidth) || (instanceImg.ScaledWidth <= 714 ))
            {
                pdfReader = new PdfReader(pdfPortrait);
            }
            else
            {
                pdfReader = new PdfReader(pdfLandscape);
            }

            MemoryStream memoryStream = new MemoryStream();
            PdfStamper pdfStamper = new PdfStamper(pdfReader, memoryStream);

            AcroFields testForm = pdfStamper.AcroFields;
            testForm.SetField("MediaField", mediaField);

            PdfContentByte overContent = pdfStamper.GetOverContent(1);
            IList<AcroFields.FieldPosition> fieldPositions = testForm.GetFieldPositions("ImageField");

            if (fieldPositions == null || fieldPositions.Count <= 0) throw new ApplicationException("Error locating field");
            AcroFields.FieldPosition fieldPosition = fieldPositions[0];
            iTextSharp.text.Rectangle imageRect = new Rectangle(fieldPosition.position.Top, fieldPosition.position.Left, fieldPosition.position.Bottom, fieldPosition.position.Right);

            instanceImg.ScaleToFit(imageRect.Height, -1 * imageRect.Width);
            instanceImg.SetAbsolutePosition(fieldPosition.position.Left, (fieldPosition.position.Top - (instanceImg.ScaledHeight)));

            overContent.AddImage(instanceImg);
            pdfStamper.FormFlattening = true;
            pdfStamper.Close();

            PdfReader resultReader = new PdfReader(memoryStream.ToArray());
            pdfReader.Close();

            return resultReader;
        }

Thanks,