1

I've some pdf with a lot of sheets. I made a serach in those files and I've a list with the page numers where the string is found. Now I have to open those files to the exact page, perhaps with a zoom and a rectangle that shows the result.

Any idea?

P.S. The code I use for reading and open the pdf files is:

        Dim reader As New PdfReader(FileToOpen)
        Dim stamper As New PdfStamper(reader, New System.IO.FileStream("j:\Zoom.pdf", FileMode.Open, FileAccess.ReadWrite))
        Dim pdfDest As New PdfDestination(PdfDestination.FITH)
        Dim OA As PdfAction = PdfAction.GotoLocalPage(2, pdfDest, stamper.Writer)
        stamper.Writer.SetOpenAction(OA)
        stamper.Close()
        reader.Close()
Jotric
  • 49
  • 1
  • 1
  • 6

1 Answers1

1

I almost closed this question as a duplicate of How to set initial view properties? because I found the question in the official iText documentation with a link to Stack Overflow: How to set initial view properties?

Of as a duplicate of How to set zoom level to pdf using iTextSharp? (see How to set the zoom level of a PDF using iTextSharp?)

However, I see that the answer on the official iText web site is better than the answer on Stack Overflow, because it combines everything you need to know.

In short: you need an open action:

//Create a destination that fit's width (fit horizontal)
var D = new PdfDestination(PdfDestination.FITH);
//Create an open action that points to a specific page using this destination
var OA = PdfAction.GotoLocalPage(1, D, stamper.Writer);
//Set the open action on the writer
stamper.Writer.SetOpenAction(OA);

Obviously, you can choose to create another destination when you define D.

If you are by any chance using iText 7, then you should read chapter 7 of the iText 7: Building Blocks tutorial to learn about the open action, and chapter 6 where all the possible destinations (XYZ, FitH, FitB,...) are explained.

Please read the documentation before posting a question.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I apologize, but I am a newbie and it's two days I'm looking for the documentation to do a simple task like opening a pdf file to the page I want .... On the official site I could not find documentation. Can you direct me to a link? – Jotric Aug 22 '16 at 10:12
  • There is a search box on the official site. When you fill in **zoom** (which is an important keyword in your question), you get this page: http://itextpdf.com/search/node/zoom If you look at this page, you will find the pages I refer to. That's how I found the answer to your question. – Bruno Lowagie Aug 22 '16 at 10:49
  • Note that I assume that your question was genuine and that you were not a troll trying to provoke a reaction such as *"Surely you are familiar with the concept of a search box."* – Bruno Lowagie Aug 22 '16 at 10:50
  • Maybe I'm too stupid or I don't understand nothing....but I've used the search box and I found the example you posted here, but it does not work ...I've used the example code and I can modify or create a copy of the file but I can not open it for view the content on the display. – Jotric Aug 22 '16 at 13:12
  • Share the PDF because "*it does not work* is not a problem description that a software developer should give when asking a question on an internet forum. That's a problem description most commonly employed by standard users that do not understand or care about how computers work." See http://lowagie.com/doesntwork – Bruno Lowagie Aug 22 '16 at 13:15
  • If you have tried the code, you should have made it part of your question. Your question is not a good question for Stack Overflow because it doesn't show what you've tried. – Bruno Lowagie Aug 22 '16 at 13:16
  • I've uploaded the question and also share the file here : (https://drive.google.com/file/d/0B8vxXZ11kiMWLUxOa253dWIxS0k/view?usp=sharing) – Jotric Aug 22 '16 at 13:45
  • I have downloaded the file. I opened it, and it opened on the second page. In other words: what is the problem? Maybe you are blaming iText and your own programming skills whereas you should in fact be blaming the PDF viewer. Maybe the PDF viewer doesn't support open actions. One remark though: you use `FITH` without a parameter. (1.) This is wrong, only `FIT` doesn't require a parameter, and (2.) you explicitly ask to change the zoom factor. Only `XYZ` allows you to define a zoom factor. – Bruno Lowagie Aug 22 '16 at 13:55
  • Ok ... I begin to understand. I thought iText to launch with the "OpenAction" instruction to the shell, instead I have to throw myself. So if I understand correctly, IText arranges the file which is opened by the viewer. Correct? – Jotric Aug 22 '16 at 14:04
  • iText is not a PDF viewer. iText implements ISO-32000-1 (the PDF specification) to create documents in a reliable way. PDF viewers should also implement that ISO standard to render PDFs in a reliable way. Interactive features such as an open action can be applied to a PDF using iText, but it is the viewer that is responsible to perform those actions. – Bruno Lowagie Aug 22 '16 at 14:10
  • Ok... but to understanding the philosophy, the code to sent at the viewer must be sended before the closing instructions? – Jotric Aug 22 '16 at 14:24
  • I'm not sure what that question means. iText creates and manipulates PDF files. It doesn't send anything to the viewer. iText creates the bytes and you provide the `OutputStream`. – Bruno Lowagie Aug 22 '16 at 14:26
  • I do not have to create a file, but open an existing one at the page x and possibly highlight a word. When I've made this I open the file with the viewer. Correct? – Jotric Aug 22 '16 at 15:02
  • You can't manipulate a PDF while it's open in the viewer. You have to close the file, manipulate it, and than open it again to see the difference. – Bruno Lowagie Aug 22 '16 at 15:50
  • I noticed it, but can I modify an existing file without creating a copy? If I try to do: ` Dim reader As New PdfReader(FileToOpen) Dim fs As New FileStream(FileToOpen, FileMode.Open, FileAccess.ReadWrite) Dim stamper As New PdfStamper(reader, fs)` I've an error – Jotric Aug 22 '16 at 16:06
  • Please stop asking duplicate questions. See **How to update a PDF without creating a new PDF?** on [Stack Overflow](http://stackoverflow.com/questions/16081831) and on the [official web site](http://developers.itextpdf.com/question/how-update-pdf-without-creating-new-pdf). – Bruno Lowagie Aug 22 '16 at 16:17
  • I'm sorry, but maybe it isn't the same question. As I told, I need to open a file to a pag. number with a scaling zoom to a word. Where is the write operation ? When I use a FileStream, and I set it as FileAccess.Read, it means I'll wouldn't write into it, so why the stamper dosn't recognize this case? – Jotric Aug 23 '16 at 06:32
  • You need to read one file with `PdfReader`. You need to write to *another file* using `PdfStamper`. What is unclear about that? The real challenge will be: do you have the coordinates of the word you need to zoom on. That could turn out to be almost impossible if you don't have those coordinates. – Bruno Lowagie Aug 23 '16 at 06:39