Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project that needs a hyperlink embedded in an image that is part of a header. The Header is replicating to additional PDF pages automatically.

Just to be clear, I want to generate the PDF, and then click on the image and a browser tab will open up and display a website. I do not want to pull an Image from a website and imbed it into my PDF.

The image is the first object in the document.

My PDF is working great otherwise.

Here is some of my code which gets the image and displays it:



C#
bmp.Save(combinesfinalpath);   // Save the Image


// Retrieve the Image and add to the Header    
MigraDoc.DocumentObjectModel.Shapes.Image image = sec.Headers.Primary.AddImage(@"C:\Users\" + userName + @"\" + "AppData" + @"\" + "Roaming" + @"\" + "PN" + @"\" + "PH2.png");


//Adjust the Image to fit page
image.Height = "3.0cm";
image.Width = "16.0cm";
image.LockAspectRatio = true;
        
 


// Start Adding Header Text       
sec.Headers.Primary.AddParagraph();  // insert blank paragraph

Paragraph paragraph = sec.Headers.Primary.AddParagraph();
        
paragraph.Format.Font.Color = Colors.Red;
paragraph.Format.Font.Size = "9";
paragraph.Format.Alignment = ParagraphAlignment.Right;
paragraph.AddFormattedText("Website  |  Facebook  |  Instagram        ", TextFormat.Bold);


....code continues


What I have tried:

Examples for Hypertexting just plain Text is fairly easy, but with a header image it's a little harder to find.

I did find this, but the example does not have the image as part of the header:


C#
Hyperlink hyperlink = paragraph.AddHyperlink(str, hyperlinkType.Web)
hyperlink.AddImage(...);


I don't define my first paragraph until after the Image is displayed, so I get an error on the line.  Again this image is in the Header, so I'm searching for an example or help on that.

So to be more specific, the error I get is "Cannot use local variable before it's declared".

For the heck of it I typed in this line of code right after the MigraDoc Image, but before I add the first paragraph (text):


Hyperlink hyperlink = paragraph.AddHyperlink("http://yahoo.com/", HyperlinkType.Web);


With the line right above, it didn't like paragraph because it has not yet been declared, which makes sense.

This leads me to the believe it might not be the right solution for me, not only because it does not reference the Header status, but because the image is declared before I need a paragraph to start displaying text.  

I can always add a new paragraph in, but not sure how to add the image, as a header, to the PDF, which is my original problem.

Sorry for the run on...
Posted
Updated 29-Oct-20 11:57am
v2
Comments
[no name] 29-Oct-20 0:18am    
Stick the text (with a transparent background) over the image: same as "plain text". Or make the image the "background".
74Camaro 29-Oct-20 0:46am    
Hi Gerry, Thank you for the comment.
Do you mean using GFX.Drawstring with a text color that is 100% transparent?
Isn't that PDFSharp and not MigraDoc?
[no name] 29-Oct-20 14:36pm    
That was the "pseudo-code" solution. It's a PDF; you're worrying about who does what when you should be thinking in terms of what the PDF does. It contains "meta data" that tells it how to present. I cannot see generating custom PDF's without some idea of what's inside a PDF and how it does what it does. Maybe you create a custom "pre / post" process step.
74Camaro 29-Oct-20 14:53pm    
Ok, thank you, good point!
I understand the Metadata describing other data, but without working code I'm having issues.

If anything I'm guessing I need Pre Process step, since everything gets defined before the image is added to the Section. However I can only intellisense my way through this so much, a working example or something even vaguely close to it would be nice.

Not asking anyone to write the whole project for me, this is the last few lines out of a project of over 20 thousand.
[no name] 30-Oct-20 12:44pm    
I love a good document.

https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf

1 solution

added a new paragraph for the hypertext command.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900