tags:

views:

732

answers:

5

If I have an existing PDF that has a graphic on it, and I simple want a user to be able to click a point on the pdf, and drop a letter at the point the click, like A... B... etc. I'm thinking that ITextSharp could handle something like this, but frankly, I'm not sure how to accomplish it. Can you offer some guidance?

A: 

Wouldn't you probably want to load the PDF, turn it into something that you can render in your app (ie, some editable document object), make changes, and then write it back out to PDF?

Granted, that's probably a lot of work for what you are trying to accomplish, but most PDF tools I am aware of don't actually edit the .PDF directly.

iTextSharp may have the ability to load .PDF files into some sort of document tree that you can then manipulate. I would start by seeing if it does such a thing, and then try to change some of the nodes, or perhaps add nodes corresponding to your annotations.

It may not be possible to see your changes reflected in real time, so you probably will want to "fake" the changes by layering them over your render of the .PDF file as simple lable objects. That's probably the easiest way to start.

Christopher
I've used I text sharp to drop text onto an existing PDF before, in the instance that we built a pamphlet for a client that was going to be different at every location, so the grapchi designers created the pamphlet, then we dropped some data on a form into the corresponding positions on the pdf, and saved it. But in this scenario, I need to user to click a specific point on the pdf. Difference being, I knew the x and y I was dropping on before hand, and now it is up in the air...
A: 

No, itextsharp can not do that, since itextsharp can manipulate pdf-files, but there's no way of rendering a pdf in itextsharp.

A: 

You can certainly use ITextSharp to add content to an existing PDF. There are samples on the web that can show you how. (It's been a while since I've done this, and my needs were to add watermark images to existing PDFs.)

ITextSharp doesn't read PDFs well, so you'll probably want to find something else to display the PDF. You may need to overlay a transparent window over whatever displays the PDF so you can catch the click, but that all depends upon the viewer you find.

John Fisher
I was thinking of maybe using the adobe component that comes with reader to show the pdf, then letting them click on the document,and dropping the letters onto it, problem is that how will I determine the x and y of the click as it pertains to the pdf document, rather than the containing control?
You have two main choices, both requiring some P/Invoke calls to find the PDF display window. #1 - find the PDF display window and throw your own transparent window over it, to catch the clicks. #2 - use some of the system-wide hooks to catch a click into the actual PDF display window. #1 is likely to be simpler than #2.
John Fisher
A: 

Bearing in mind that I have only ever used PDFNet SDK for PDF manipulation tasks, I believe everything you are asking about can be achieved with that library, although you may have to deal with some complexity with coordinate conversion from the click to the location in the document, and again to building the matrix that locates the text where you need it on the write.

They do provide a simple WinForms sample PDF viewer application that might serve as a good starting point for your experimentation.

I haven't needed to go this deep into editing, myself. The starting points for documentation are in the FAQ (How to add a watermark to a page) and the knowledge base. The API documentation is also quite extensive and publicly available on the website.

IanGilham
That's an expensive library, espescially if you ever want to license an app commercially! LOL...
$899 Per processor!
+1  A: 

Here's a C# example using the commercial Quick PDF Library.

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using QuickPDFAX0714;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string LicenseKey = " your key here ";
        private string OriginalFileName = "D:\\QuickPDFLibrary\\hello1.pdf";
        private string NewFileName = "D:\\QuickPDFLibrary\\hello2.pdf";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ShowPDF(OriginalFileName);
        }

        private void ShowPDF(string fileName)
        {
            PDFLibrary qp = new PDFLibrary();
            qp.UnlockKey(LicenseKey);
            qp.LoadFromFile(fileName);

            // Fit width of PDF to width of picture box
            int dpi = Convert.ToInt32((pictureBox1.Width * 72) / qp.PageWidth());
            byte[] bmpData = (byte[])qp.RenderPageToVariant(dpi, 1, 0);

            MemoryStream ms = new MemoryStream(bmpData);
            Bitmap bmp = new Bitmap(ms);
            pictureBox1.Image = bmp;

            ms.Dispose();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            PDFLibrary qp = new PDFLibrary();
            qp.UnlockKey(LicenseKey);
            qp.LoadFromFile(OriginalFileName);

            // Calculate co-ordinates, width of PDF fitted to width of PictureBox
            double xpos = ((double)e.X / (double)pictureBox1.Width) * qp.PageWidth();
            double ypos = qp.PageHeight() - ((double)e.Y / (double)pictureBox1.Width) * qp.PageWidth();

            qp.SetTextSize(24);
            qp.SetTextColor(1, 0, 0);
            qp.DrawText(xpos, ypos, "A");

            qp.SaveToFile(NewFileName);
            ShowPDF(NewFileName);
        }

    }
}
Bing