tehDorf
tehDorf

Reputation: 795

How do I create an Action to open another PDF at page with a zoom of Fit Page in iTextSharp?

How do I make a PdfAction that will open another PDF at a specific page with the zoom level Fit Page? One of the constructors for PdfAction takes a PDF path and target page number, but it defaults the zoom to Fit Width. I don't see any methods/constructors that include a 'desired zoom' parameter. Am I not seeing it, or is there a way to change the zoom level once the PdfAction has been made?

Here is the code I have so far:

var pdfAnnotation = PdfAnnotation.CreateLink(
    pdfStamper.Writer,
    linkOutline,
    PdfAnnotation.HIGHLIGHT_NONE,
    new PdfAction(pdfPath, targetPageNumber));

pdfAnnotation.BorderStyle = new PdfBorderDictionary(0.0F, 0);

pdfStamper.AddAnnotation(pdfAnnotation, sourcePageNumber);

Upvotes: 1

Views: 108

Answers (1)

tehDorf
tehDorf

Reputation: 795

I'm not sure if this is the idiomatic solution to my problem, but I was able to update the zoom level after the PdfAction was created by replacing one of the hash table values:

var action = new PdfAction(pdfPath, targetPageNumber);

action.Remove(PdfName.D);
action.Put(PdfName.D, new PdfLiteral($@"[{targetPageNumber - 1} /Fit]"));


var pdfAnnotation = PdfAnnotation.CreateLink(
    pdfStamper.Writer,
    linkOutline,
    PdfAnnotation.HIGHLIGHT_NONE,
    action);

pdfAnnotation.BorderStyle = new PdfBorderDictionary(0.0F, 0);


pdfStamper.AddAnnotation(pdfAnnotation, sourcePageNumber);

Upvotes: 1

Related Questions