PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PdfDocument.Pages.Add(PdfPage) overwriting existing bookmark
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2412
Page 1 of 1

Author:  MonkFox [ Wed Apr 17, 2013 4:30 pm ]
Post subject:  PdfDocument.Pages.Add(PdfPage) overwriting existing bookmark

OK I have a PDF document that I create with MigraDoc. Later on down the line I eventually append addendum information to it. I add a bookmark to the combined documents on the second page of the addendum PDF (which would be original pdf.pagecount + offset).

The bookmark is added successfully, but it overwrites the existing ones from the original PDF before merging.

Is there a way to make it not do this? If not I guess I could try and change the source to remember the previous Catalog's bookmarks every page add.

Thanks,

Justin

Author:  MonkFox [ Wed Apr 17, 2013 4:39 pm ]
Post subject:  Re: PdfDocument.Pages.Add(PdfPage) overwriting existing book

Actually, now that I've looked into it more.

It's the line 'pdf.Outlines.Add("Addendums",page,false)'.

I'm using:

Code:
PdfSharp.Pdf.IO.PdfReader.Open(System.IO.Path.Combine(path, "Document.pdf"))


Do I need to specific a certain type of opening? For instance, like:

Code:
 PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import


for the document that's being appended to?

Author:  MonkFox [ Fri Apr 19, 2013 1:54 pm ]
Post subject:  Re: PdfDocument.Pages.Add(PdfPage) overwriting existing book

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.cs
Look 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.cs
Look 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

Author:  MonkFox [ Fri Apr 19, 2013 2:04 pm ]
Post subject:  Re: PdfDocument.Pages.Add(PdfPage) overwriting existing book

How do I mark this issue as resolved?

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/