I looked into the pdfmerge project and got the functionality that I wanted.
This example simply creates two PDF documents and then merges them
while keeping the bookmarks from each document using the PDFmerge library.
I also took the three files from SplitMergeLib and created a PDFMerge class library.
Sub Main:Code:
Sub Main()
CreatDocument("testpdf1.pdf")
CreatDocument("testpdf2.pdf")
Dim merger As New PdfMerge.SplitMergeLib.SplitMergeCmdFile()
Dim MergeListItem As New PdfMerge.SplitMergeLib.MergeListFiles()
MergeListItem.Path = "testpdf1.pdf"
MergeListItem.Level = 0
merger.MergeListFileArray.Add(MergeListItem)
Dim MergeListItem2 As New PdfMerge.SplitMergeLib.MergeListFiles()
MergeListItem2.Path = "testpdf2.pdf"
MergeListItem2.Level = 0
merger.MergeListFileArray.Add(MergeListItem2)
MergeDocuments(merger)
Console.ReadLine()
End Sub
MergeDocuments():Code:
Public Sub MergeDocuments(ByVal merger As PdfMerge.SplitMergeLib.SplitMergeCmdFile)
Dim psm As New PdfMerge.SplitMergeLib.PdfSharpSplitterMerger()
Dim line As String = ""
For Each merge As PdfMerge.SplitMergeLib.MergeListFiles In merger.MergeListFileArray
line = merge.Descriptor
Dim pagelst As String = merge.Pages
Dim page As Integer = 1
Dim pagearr As String() = pagelst.Split(New Char() {"-"})
Dim start As Integer = 0
Dim endp As Integer = 0
Dim ps As New ArrayList()
Try
start = Integer.Parse(pagearr(0))
endp = start
If pagearr.Length > 1 Then
endp = Integer.Parse(pagearr(1))
End If
If endp > 0 Then
For x As Integer = start To endp
ps.Add(x - 1)
Next
End If
Catch ex As Exception
End Try
If (ps.Count > 0) Then
page += psm.Add(merge.Path, ps.ToArray(System.Type.GetType("Integer")))
Else
page += psm.Add(merge.Path)
End If
Dim err As String = ""
psm.AddBookmarksFromFile("", merge.Level, True, err)
Next
psm.Finish("output.pdf", Nothing, False, 1)
End Sub
CreateDocument():Code:
Public Sub CreatDocument(ByVal name As String)
Dim doc As New MigraDoc.DocumentObjectModel.Document()
doc.Info.Title = "name"
Dim section As MigraDoc.DocumentObjectModel.Section = doc.AddSection()
section.PageSetup.TopMargin = "1.0cm"
Dim header As MigraDoc.DocumentObjectModel.HeaderFooter = section.Headers.Primary
Dim paragraph As MigraDoc.DocumentObjectModel.Paragraph = section.AddParagraph()
paragraph.AddText("this is text on page 1")
paragraph.AddBookmark("Page 1")
paragraph.Format.OutlineLevel = MigraDoc.DocumentObjectModel.OutlineLevel.Level1
paragraph.ExpandOutline = False
section = doc.AddSection()
paragraph = section.AddParagraph()
paragraph.AddText("this is text on page 2")
paragraph.AddBookmark("Page 2")
paragraph.Format.OutlineLevel = MigraDoc.DocumentObjectModel.OutlineLevel.Level1
paragraph.ExpandOutline = False
MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(doc, "MigraDoc.mdddl")
Dim DocRenderer As New MigraDoc.Rendering.PdfDocumentRenderer(True, Global.PdfSharp.Pdf.PdfFontEmbedding.Always)
DocRenderer.Document = doc
DocRenderer.RenderDocument()
DocRenderer.PdfDocument.Save(name)
DocRenderer.PdfDocument.Close()
DocRenderer.PdfDocument.Dispose()
End Sub
I added the .ExpandOutline property.
Here are the steps.
MigraDoc.DocumentObjectModel -> Paragraph.cs:Add:
Code:
/// <summary>
/// Gets or sets a boolean value indicating whether or not to expand the outlines defined by paragraphs.
/// </summary>
public bool ExpandOutline
{
get { return this.expandOutline; }
set { this.expandOutline = value; }
}
internal bool expandOutline = true;
MigraDoc.Rendering -> DocumentRenderer.csLook for:
Code:
function internal void AddOutline(int level, string title, PdfPage destinationPage)
Change to:
Code:
AddOutline(int level, string title, PdfPage destinationPage, bool expandOutline)
In the same function, Look for lines:
Code:
PdfOutline outline = outlines.Add(" ", destinationPage, true);
// ... and
outlines.Add(title, destinationPage, true);
Change to:
Code:
PdfOutline outline = outlines.Add(" ", destinationPage, expandOutline );
//... and
outlines.Add(title, destinationPage, expandOutline );
MigraDoc.Rendering -> ParagraphRenderer.csLook for:
Code:
Internal override void Render()
Change:
Code:
// ... stuff before
InitRendering();
if ((int)this.paragraph.Format.OutlineLevel >= 1 && this.gfx.PdfPage != null) // Don't call GetOutlineTitle() in vain
this.documentRenderer.AddOutline((int)this.paragraph.Format.OutlineLevel, GetOutlineTitle(), this.gfx.PdfPage );
// ... stuff After
To:
Code:
// ... stuff before
InitRendering();
if ((int)this.paragraph.Format.OutlineLevel >= 1 && this.gfx.PdfPage != null) // Don't call GetOutlineTitle() in vain
this.documentRenderer.AddOutline((int)this.paragraph.Format.OutlineLevel, GetOutlineTitle(), this.gfx.PdfPage, this.paragraph.ExpandOutline );
// ... stuff After
I noticed that the PdfOutline object has a public Opened property. I'm guessing I could have looped through the Renderer.PdfDocument's Outlines
and set the expanded that way. I just like the option being there when I'm using MigraDoc.
Justin