Store BuildingBlocks in word document via Aspose - c#

I'm looking for an example to store building blocks in a word document with the Aspose Word Api. And I want to use these building blocks exclusiv in this document.
Has anybody an idea, how this can be realized?
Greetings
Tobi.

If you have MS Word 2013 installed on your machine, you'll most likely find a Word document 'Built-In Building Blocks.dotx' at the following location:
C:\Users\Awais\AppData\Roaming\Microsoft\Document Building Blocks\1033\15\Built-In Building Blocks.dotx
The predefined built-in building blocks entries that ship with Word are stored in above template document. Using Aspose.Words, you can extract any building block from it, paste inside another Word document and save new document for future use.
GlossaryDocument class in Aspose.Words represents such Building Blocks. Please try using the following code:
Document docBuildingBlocks = new Document(MyDir + #"Building Blocks.dotx");
Document doc = new Document(MyDir + #"input.docx");
GlossaryDocument glossaryDocument = docBuildingBlocks.GlossaryDocument;
foreach (BuildingBlock buildingBlock in glossaryDocument.BuildingBlocks)
{
if (buildingBlock.Gallery.ToString().StartsWith("CoverPage"))
{
Section sec = (Section)buildingBlock.FirstChild;
doc.AppendChild(doc.ImportNode(sec, true));
}
}
doc.Save(MyDir + #"15.6.0.docx");
Hope, this helps.
I work with Aspose as Developer Evangelist.

Related

get height and width of docx file without using office

I get count of pages in next code:
using DocumentFormat.OpenXml.Packaging;
WordprocessingDocument doc = WordprocessingDocument.Open( #"D:\2pages.docx", false );
Console.WriteLine(
doc.ExtendedFilePropertiesPart.Properties.Pages.InnerText.ToString()
);
can I get in this way height and width of file?
or in another way but without using office.
Aspose.Word (which is not free) has these things built in:
https://docs.aspose.com/display/wordsnet/Changing+Page+Setup+for+Whole+Document+using+Aspose.Words
Document doc = new Document();
// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.RemoveAllChildren();
// Create a new section node.
// Note that the section has not yet been added to the document,
// but we have to specify the parent document.
Section section = new Section(doc);
// Append the section to the document.
doc.AppendChild(section);
// Lets set some properties for the section.
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;
Someone had a similar problem and this is the discussion on SO (but with OpenXML):
Change Page size of Wor Document using Open Xml SDK 2.0
Maybe you can deduct your answer from this.
Please use PageSetup.PageHeight and PageSetup.PageWidth properties to get the page's height and width. Hope this helps you.
Document doc = new Document(MyDir + "input.docx");
Console.WriteLine(doc.FirstSection.PageSetup.PageHeight);
Console.WriteLine(doc.FirstSection.PageSetup.PageWidth);
I work with Aspose as Developer Evangelist.

Wrong values trying to get words count from a Microsoft Word document with OpenXML?

have a word document and I want to get word count programmatically using OpenXML sdk,
I managed to get word count but openXML returns wrong values.
note that the test document is mixed languages (Arabic, English) Arabic is RTL language.
if you open the word document using Microsoft word in the UI it gives you the correct number of words
but if you go and get the value stored in the app.xml file for the same document you will get different value.
I tried the code in this link
https://msdn.microsoft.com/en-us/library/office/bb521237(v=office.14).aspx
// To retrieve the properties of a document part.
public static void GetPropertyFromDocument(string document)
{
XmlDocument xmlProperties = new XmlDocument();
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(document, false))
{
ExtendedFilePropertiesPart appPart = wordDoc.ExtendedFilePropertiesPart;
xmlProperties.Load(appPart.GetStream());
}
XmlNodeList chars = xmlProperties.GetElementsByTagName("Characters");
MessageBox.Show("Number of characters in the file = " +
chars.Item(0).InnerText, "Character Count");
}
the file I tested contains
word count is 13 but using upper code it gives me 11!
DocIO is a .NET library that can read, write and render Word 2003/2007/2010/2013/2016 files. Using DocIO library of Syncfusion, you can get the correct word count. The whole suite of controls is available for free (commercial applications also) through the community license program if you qualify. The community license is the full product with no limitations or watermarks.
Step 1: Create a console application
Step 2: Add reference to Syncfusion.DocIO.Base, Syncfusion.Compression.Base and Syncfusion.OfficeChart.Base; You can add these reference to your project using NuGet also.
Step 3: Copy & paste the following code snippet.
This code snippet will produce the words count in the Word document as per your requirement.
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using System.IO;
namespace DocIO_MergeDocument
{
class Program
{
static void Main(string[] args)
{
//Creates a new Word document
WordDocument document = new WordDocument(#"InputDocument.docx");
//Update the words count in the document.
document.UpdateWordCount(false);
//Get the updated words count
int wordCount = document.BuiltinDocumentProperties.WordCount;
//Releases the resources occupied by WordDocument instance
document.Dispose();
}
}
}
For further information about DocIO, please refer our help documentation
Note: I work for Syncfusion

Word document orientation lost after using OpenXML SDK AddAlternativeFormatImportPart

I am attempting to merge several Word documents together into a single Word document. I am using the AltChunk capability from Microsoft's OpenXML SDK 2.5. The final report needs to be in landscape orientation, thus we have put each component document into landscape mode. I am merging the documents using the following code.
for (int i = 0; i < otherDocs.Length; i++)
{
using (var headerDoc = WordprocessingDocument.Open(headerPath, true))
{
var mainPart = headerDoc.MainDocumentPart;
string altChunkId = "AltChunkId" + i;
var chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
using (var fileStream = File.Open(otherDocs[i], FileMode.Open))
{
chunk.FeedData(fileStream);
}
var altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
mainPart.Document.Save();
DocumentWriter.SetPrintOrientation(headerDoc, PageOrientationValues.Landscape);
headerDoc.Close();
}
}
When I run this code, the final output document has a mix of landscape and portrait orientation if the component documents each have at least one section break.
The DocumentWriter.SetPrintOrientation() method is implemented according to instructions from MSDN. It seems to have no effect on the actual orientation of the document. I have also examined the underlying XML files, and all "orient" attributes are set to landscape.
Is there some configuration option or API call I can use to ensure the final document will have landscape orientation across all sections?
An OpenXML Word document is a zipped collections of XML documents that define its content, formatting, and metadata. When merging Word documents together using AddAlternativeFormatImportPart, the Word documents being merged into the original document (AltChunks) are copied into the zip archive, and XML elements referencing the documents are added into the XML document definition. Then, the next time anyone opens the resulting document in Microsoft Word (or any other OpenXML compatible document editor), the application handles merging in the AltChunks. In this case, Microsoft Word 2010 (the version we are using) has a bug causing Word to ignore some formatting information defined in Word document sections. One of these pieces of information is orientation, making the AltChunk approach ineffective at preserving orientation information.
Instead, we used DocumentBuilder from the OpenXml Power Tools project. This resulted in much simpler code that solved our problem in two lines:
var sourceList = documentPaths.Select(doc => new Source(new WmlDocument(doc), true)).ToList();
DocumentBuilder.BuildDocument(sourceList, outputPath);

Save the output as PDF file

We are currently using SoftArtisans to generate Excel and Word files.
We need to extend this to also create PDF files.
Does OfficeWriter currently support this?
If not, any plans to add this feature? Or any opensource library that can be used to convert Excel/Word files to PDF format?
PdfSharp and Migradoc as far as I know are the best and the most popular. Migradoc is the higher-level cover for PdfSharp.
Note: I work for SoftArtisans, makers of OfficeWriter.
OfficeWriter does not currently support converting Excel/Word files to PDF. We generally recommend a 3rd party component to convert to PDF. However, many of these components require having Office installed on the server, which Microsoft does not advise. Therefore, it is important that you choose a converter that either does not require having Office on the server, or manages it carefully
Here are a few solutions for converting Word to PDF that we’ve recommended to our users in the past:
• Word Services for Sharepoint – If you are using SharePoint Server 2010, then you can use Word Services to perform the format conversion. More information about this solution can be found at: http://msdn.microsoft.com/en-us/library/office/ff181518.aspx
• Rainbow PDF - rainbowpdf.com
• EasyPDF - pdfonline.com/easypdf/
For more information, please see our blog post: http://blog.softartisans.com/2011/08/05/kb-tools-to-convert-word-documents-to-pdf/
PfgSharp is quite popular. Here is an example from CodeProject on how to create a simple PDF to get some feeling on how to use it:
class Program
{
static void Main(string[] args)
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
// Save the document...
const string filename = "HelloWorld.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}
}

Create folder in document library using SharePoint workflow

I want to create folder inside my document library based on other document library in the root. And I want to run this workflow on new document library creation.
So let say I have 4 document library:
Help
News
Doc
Archive
I want to create folder inside "Archive" Document library when New document library is created.
Help
News
Doc
Archive
New Doc Library
and in Archive document library it will create folders
Help
News
Doc
New Doc Library
Is it possible..?
How can I do it.??
--
Thanks in advance.
string archiveListUrl = "http://myserver/Archive/";
SPSite site;
SPWeb web;
SPDocumentLibrary library;
using (site = new SPSite(archiveListUrl))
using (web = site.OpenWeb())
{
library = (SPDocumentLibrary)web.Lists["Archive"];
web.Folders.Add(archiveListUrl + "Help");
web.Folders.Add(archiveListUrl + "News");
web.Folders.Add(archiveListUrl + "Doc");
web.Folders.Add(archiveListUrl + "New Doc Library");
library.Update();
}
Thats difficult: Workflows are are always attached to one document/item. One thing you could do: Create the Library from a customized template - and you can customize this lib with an workflow to start on creation of a doc. Then add a document to the library and save the whole thing as template with content.
(Actually you might check if creating a lib template with existing folders might be easier...)

Categories