Wednesday, March 27, 2013

Source Code in C# Existing PDF to PDF/A Conversion using ItextSharp dll

Download dll:-  Itextsharp.dll

https://docs.google.com/file/d/0B9u08qcO4YhqS29yNXNLNnNnU1E/edit?usp=sharing


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)

        {
            PdfReader reader = new PdfReader(@"c:\\mastering_microsoft_visual_basic_net.pdf"); //INPUT Multi PDF FILE path
            Document document = new Document(reader.GetPageSize(1));
            PdfCopy copier = new PdfCopy(document, new FileStream("c:\\2.pdf", FileMode.Create)); //OUTPUT PDF/A FILE PATH AND Name
            copier.PDFXConformance = PdfWriter.PDFA1B;
            document.Open();
            copier.SetFullCompression(); // IF You need a compress a PDF/A file
            PdfDictionary outi = new PdfDictionary(PdfName.OUTPUTINTENT);
            outi.Put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1"));
            outi.Put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
            outi.Put(PdfName.S, PdfName.GTS_PDFA1);

            ICC_Profile icc = ICC_Profile.GetInstance(@"C:\aaa\sRGB_IEC61966-2-1_black_scaled.icc"); //Passing a .ICC FILE

            PdfICCBased ib = new PdfICCBased(icc);
            ib.Remove(PdfName.ALTERNATE);
            outi.Put(PdfName.DESTOUTPUTPROFILE, copier.AddToBody(ib).IndirectReference);

            copier.ExtraCatalog.Put(PdfName.OUTPUTINTENTS, new PdfArray(outi));


            for (int pageCounter = 1; pageCounter < reader.NumberOfPages + 1; pageCounter++)

            {
                copier.AddPage(copier.GetImportedPage(reader, pageCounter));
            }

            copier.Add(new Paragraph("This is added text"));

            copier.CreateXmpMetadata();
            document.Close();
            reader.Close();

        }

    }
}

3 comments:

  1. hi,
    sorry but the file that I can get following your example doesn't pass the validation pdf/a (from http://www.pdf-tools.com/pdf/validate-pdfa-online.aspx).
    I'm losing something or you have the same result?
    Thank you,
    G.

    ReplyDelete
  2. I have found another C#/.NET Library that can convert PDF to PDF/A format known as Aspose.PDF for .NET Library. You can try this library also, it shares samples codes also.

    ReplyDelete
  3. The kernel of iTextSharp is really powerful, but pdf to text c# itself is relatively simple and easy to implement. This is a packaged commercial product that everyone can look at.

    ReplyDelete