We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create a bibliography with source

Hello everybody,

I am new to Aspose and am contacting you to have some tips on creating a bibliography using a custom source.

I don’t know how to create my source and link it to my bibliography.

Thank you in advance for your answers.

@lafydo It seem Aspose.Words.BuildingBlocks namespace is what you are looking for.
Once you create a BuildingBlock, you can specify it’s Galery as BuildingBlockGallery.Bibliography.

I’m sorry, I don’t really understand the documentation (I’m french) do you have more information for the creation of the building block. I have metadatas like author, title… and how do you transform to Bibliography and how do you add to the word doc.

Thanks a lot for you help !

@alexey.noskov I did this :

var document = new Document();
var builder = new DocumentBuilder(document);
GlossaryDocument glossaryDoc = new GlossaryDocument();
BuildingBlock block = new BuildingBlock(glossaryDoc)
{
    Name = "Custom Block",
    Gallery = BuildingBlockGallery.Bibliography,
    Behavior = BuildingBlockBehavior.Paragraph,
    Category = "My custom building blocks",
    Description = "Using this block in the Quick Parts section of word will place its contents at the cursor."
};
block.Guid = Guid.NewGuid();
glossaryDoc.AppendChild(block);

document.GlossaryDocument = glossaryDoc;

Now I want to put this block in my document but I don’t know how.

Thanks a lot if you help me or not.

@lafydo Your code creates an empty building block, so its insertion will not have any effect. See the following modified code, where content is added into the created building block:

var document = new Document();

GlossaryDocument glossaryDoc = new GlossaryDocument();
BuildingBlock block = new BuildingBlock(glossaryDoc)
{
    Name = "Custom Block",
    Gallery = BuildingBlockGallery.Bibliography,
    Behavior = BuildingBlockBehavior.Paragraph,
    Category = "My custom building blocks",
    Description = "Using this block in the Quick Parts section of word will place its contents at the cursor."
};
block.Guid = Guid.NewGuid();
glossaryDoc.AppendChild(block);

// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(glossaryDoc);
block.AppendChild(section);
block.FirstSection.EnsureMinimum();
Run run = new Run(glossaryDoc, "Text inside " + block.Name);
block.FirstSection.Body.FirstParagraph.AppendChild(run);

document.GlossaryDocument = glossaryDoc;

document.Save(@"C:\Temp\template.dotx");

For insertion content of the created building block into the document, you should use the same approach as for copying nodes between documents. For example see the following code:

var document = new Document(@"C:\Temp\template.dotx");

// We can access the block that we just made from the glossary document.
BuildingBlock customBlock = document.GlossaryDocument.GetBuildingBlock(BuildingBlockGallery.Bibliography, "My custom building blocks", "Custom Block");

// Now, we can insert it into the document as a new section.
document.AppendChild(document.ImportNode(customBlock.FirstSection, true));

document.Save(@"C:\Temp\out.docx");

The code appends whole section from the create building block, but you can import only its content or part of content.

@alexey.noskov

Thank you very much for your help, I have a result!
I just didn’t use a doc to store a buildingblock then another to retrieve it I did everything in one. I have the sentence: “Text inside Custom Block”.

I still have 2 questions, if you don’t mind:

  • Is it possible to have the style linked to the bibliography

example :
image.png (959 Bytes)

Because for the moment, I just have :
The sentence: “Text inside Custom Block” without the title “Bibliography”

  • Is it possible to attach a source with my filled in metadata?

To have something like this:
image.png (959 Bytes)

I realize that I ask you a lot and I apologize for that

@lafydo Could you please create the expected document in MS Word and attach it here? We will investigate it and provide you more information and code example.

@alexey.noskov

This is an example of what I want.

Bibliographie example.docx (13.6 KB)

I really thanks you for your help.

@lafydo Thank you for additional information. I have investigated your document and you are using BIBLIOGRAPHY field. Unfortunately, Aspose.Words does not support updating this field. This feature is logged as WORDSNET-13854. I have linked your request to this issue. We will let you know once it is resolved.
However, you can still insert BIBLIOGRAPHY field into the document using Aspose.Words, but you will have to update it manually in MS Word. For example see the following code:

Document document = new Document();

CustomXmlPart xmlPart = document.CustomXmlParts.Add("Books",
    "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
    "<b:Sources xmlns:b=\"http://schemas.openxmlformats.org/officeDocument/2006/bibliography\" xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/bibliography\" SelectedStyle=\"\\APASixthEditionOfficeOnline.xsl\" StyleName=\"APA\" Version=\"6\">" +
        "<b:Source>" +
            "<b:Tag>Jul992</b:Tag>" +
            "<b:SourceType>Report</b:SourceType>" +
            "<b:Guid>{FC4116BB-C7A6-433C-8502-29FDC9E376F8}</b:Guid>" +
            "<b:Author>" +
                "<b:Author>" +
                    "<b:NameList>" +
                        "<b:Person>" +
                            "<b:Last>Julien</b:Last>" +
                        "</b:Person>" +
                    "</b:NameList>" +
                "</b:Author>" +
            "</b:Author>" +
            "<b:Title>MyTitle</b:Title>" +
            "<b:Year>1999</b:Year>" +
            "<b:City>Paris</b:City>" +
            "<b:RefOrder>1</b:RefOrder>" +
        "</b:Source>" +
    "</b:Sources>");


DocumentBuilder builder = new DocumentBuilder(document);
builder.InsertField(FieldType.FieldBibliography, true);

document.Save(@"C:\Temp\out.docx");

To update fields in MS Word you can press F9. The custom XML in the code was extracted from your sample document.
Also, you can achieve something similar using structured document tags. For example see the following code:

Document document = new Document();

CustomXmlPart xmlPart = document.CustomXmlParts.Add("Books",
"<books>" +
    "<book>" +
        "<title>Everyday Italian</title>" +
        "<author>Giada De Laurentiis</author>" +
    "</book>" +
    "<book>" +
        "<title>The C Programming Language</title>" +
        "<author>Brian W. Kernighan, Dennis M. Ritchie</author>" +
    "</book>" +
    "<book>" +
        "<title>Learning XML</title>" +
        "<author>Erik T. Ray</author>" +
    "</book>" +
"</books>");

StructuredDocumentTag sdtTitle = new StructuredDocumentTag(document, SdtType.RichText, MarkupLevel.Inline);
sdtTitle.XmlMapping.SetMapping(xmlPart, "/books[1]/book/title", string.Empty);

StructuredDocumentTag sdtAuthor = new StructuredDocumentTag(document, SdtType.RichText, MarkupLevel.Inline);
sdtAuthor.XmlMapping.SetMapping(xmlPart, "/books[1]/book/author", string.Empty);
sdtAuthor.ContentsFont.Italic = true;

DocumentBuilder builder = new DocumentBuilder(document);
builder.InsertNode(sdtTitle);
builder.Write(" ");
builder.InsertNode(sdtAuthor);

document.Save(@"C:\temp\out.docx");

The idea is to put each part of your custom XML into a separate structured document tag and apply the appropriate formatting.

Another option is using BuildingBlock as you already do and format its content as you need. For example see the following code:

var document = new Document();

GlossaryDocument glossaryDoc = new GlossaryDocument();
BuildingBlock block = new BuildingBlock(glossaryDoc)
{
    Name = "Custom Block",
    Gallery = BuildingBlockGallery.Bibliography,
    Behavior = BuildingBlockBehavior.Paragraph,
    Category = "My custom building blocks",
    Description = "Using this block in the Quick Parts section of word will place its contents at the cursor."
};
block.Guid = Guid.NewGuid();
glossaryDoc.AppendChild(block);

// Add a section with text.
// Inserting the block into the document will append this section with its child nodes at the location.
Section section = new Section(glossaryDoc);
block.AppendChild(section);
block.FirstSection.EnsureMinimum();
Run run = new Run(glossaryDoc, "Text inside " + block.Name);
block.FirstSection.Body.FirstParagraph.AppendChild(run);
Run run1 = new Run(glossaryDoc, " And this is italic text.");
run1.Font.Italic = true;
block.FirstSection.Body.FirstParagraph.AppendChild(run1);

document.GlossaryDocument = glossaryDoc;

document.Save(@"C:\Temp\template.dotx");

@alexey.noskov Sorry for my delay I was at school and I couldn’t answer you (I’m on a work-study program). Thank you very much, you helped me a lot BUT isn’t there a way to display the bibliography without pressing the F9 key?

@lafydo As I already mentioned, Aspose.Words does not support pdating Bibliography fields. This feature request is logged as WORDSNET-13854 in our defect tracking system. We will keep you informed and let you know once it is supported.