Adam O'Neil
Adam O'Neil

Reputation: 134

iTextSharp - Opening a file and saving PdfDestination and PdfAction

I am trying to do something relatively simple with iTextSharp, but I always find it very confusing and can't figure out this without asking for some help.

I have a situation where a third party product I use generates a PDF, but doesn't have the option of setting initial view settings (zoom, fit to width, etc).

I have found some code that will allows me to do this in iTextSharp :-

Developer Barn

The bit I cannot work out is how to apply this to a file that already exists - this seems to be fine for any new files, or something I am creating in iTextSharp, but not an existing PDF. Is there a way of doing this, and how can it be done?

Many thanks in advance,

Adam

PS - Already found the answer to this.. StackOverflow won't let me close my own question though? Seems a bit daft, but anyway do it like this -

        PdfReader reader = new PdfReader(new FileStream(fileName, FileMode.Open, FileAccess.Read));
        Rectangle size = reader.GetPageSizeWithRotation(1);

        using (Document document = new Document(size))
        {
            using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Path.Combine(Path.GetDirectoryName(fileName), "Zoom" + Path.GetFileName(fileName)), FileMode.Create, FileAccess.ReadWrite)))
            {
                //open our document
                document.Open();

                PdfContentByte cb = writer.DirectContent;

                //this creates a new destination to send the action to when the document is opened.
                PdfDestination pdfDest = new PdfDestination(PdfDestination.FITH, reader.GetPageSize(1).Top);

                //create a new action to send the document to our new destination.
                PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);

                for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
                {
                    //need to change page size for landscape / portrait
                    document.SetPageSize(reader.GetPageSizeWithRotation(pageNumber));

                    document.NewPage();
                    PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
                    cb.AddTemplate(page, 0, 0);
                }

                //set the page mode
                int PageMode = 0;
                PageMode += PdfWriter.PageLayoutOneColumn;


                //set the open action for our writer object
                writer.SetOpenAction(action);
                writer.ViewerPreferences = PageMode;
                writer.SetFullCompression();

                //finally, close our document
                document.Close();
            }
        }

Upvotes: 3

Views: 4929

Answers (1)

Tim Bee
Tim Bee

Reputation: 2230

I don't think there is an edit functionnality per se, neither in iTextSharp nor in iText. I think the way to go is to open the existing document, create a new writer, copy the old document into the new writer while adding the enrichments you'd like to see and overwrite the original doc afterwards as decribed here.

Upvotes: 3

Related Questions