Adding a Hyperlink to an Anchor in a Different C1PrintDocument
To link a location in one document to a location in another, you must do the following:
• As described above, add an anchor to the target document, generate that document and save it as a C1D file on your disk. You can save the document using the Save button of the preview control, or in code using the Save method on the document itself.
• Add a link pointing to that anchor to another document, in a way very similar to how an internal link is added. The only difference is that in addition to the target' anchor name you must also provide the name of the file containing the document.
Here is the text of a complete program that creates a document with an anchor in it, and saves it in a disk file (myDocument1.c1d). It then creates another document, adds a link to the anchor in the first document to it, and shows the second document in a preview dialog box:
' Make target document with an anchor.
Dim targetDoc As New C1.C1Preview.C1PrintDocument
Dim rt1 As New C1.C1Preview.RenderText("This is anchor1 in myDocument1.")
rt1.Anchors.Add(New C1.C1Preview.C1Anchor("anchor1"))
targetDoc.Body.Children.Add(rt1)
targetDoc.Generate()
targetDoc.Save("c:\myDocument1.c1d")
' Make document with a hyperlink to the anchor.
Dim sourceDoc As New C1.C1Preview.C1PrintDocument
Dim rt2 As New C1.C1Preview.RenderText("This is hyperlink to myDocument1.")
Dim linkTarget As C1.C1Preview.C1LinkTarget = New C1.C1Preview.C1LinkTargetExternalAnchor("c:\myDocument1.c1d", "anchor1")
rt2.Hyperlink = New C1.C1Preview.C1Hyperlink(linkTarget)
sourceDoc.Body.Children.Add(rt2)
sourceDoc.Generate()
' Show document with hyperlink in preview.
Dim preview As New C1.Win.C1Preview.C1PrintPreviewDialog()
preview.Document = sourceDoc
preview.ShowDialog()
• C#
// Make target document with an anchor.
C1PrintDocument targetDoc = new C1PrintDocument();
RenderText rt1 = new RenderText("This is anchor1 in myDocument1.");
rt1.Anchors.Add(new C1Anchor("anchor1"));
targetDoc.Body.Children.Add(rt1);
targetDoc.Generate();
targetDoc.Save(@"c:\myDocument1.c1d");
// Make document with a hyperlink to the anchor.
C1PrintDocument sourceDoc = new C1PrintDocument();
RenderText rt2 = new RenderText("This is hyperlink to myDocument1.");
C1LinkTarget linkTarget = new C1LinkTargetExternalAnchor(@"c:\myDocument1.c1d", "anchor1");
rt2.Hyperlink = new C1Hyperlink(linkTarget);
sourceDoc.Body.Children.Add(rt2);
sourceDoc.Generate();
// Show document with hyperlink in preview.
C1PrintPreviewDialog preview = new C1PrintPreviewDialog();
preview.Document = sourceDoc;
preview.ShowDialog();
Note the following:
• The anchor is created in exactly the same manner as for links within the same document. In fact, there is no difference; the same anchor may be the target of links both from the same document and from other documents.
• To save the document, the Save method is used. This method saves the document in native C1PrintDocument format, the default extension for which is C1D. Files saved in that format can be later loaded into a C1PrintDocument object for further processing, or previewed using the ComponentOne print preview control.
• Before creating the hyperlink, a link target object must be created which is then passed to the hyperlink constructor. Several link target types are provided, derived from the C1LinkTarget base class. For external anchors, the C1LinkTargetExternalAnchor type is used. The link target contains information needed to process the jump to the link, in this case the document filename and the name of the anchor in it.
|