Delete blank pages from WORD open XML - c#

I have successfully generated a word document file using open XML, but I have got too many blank pages,
how can i remove them ?

This depends on how those blank pages are represented in the Open XML; you may want to post a sample document to demonstrate exactly how your blank pages are represented.
But let's take the case of a Word document in which a user has inserted extra page breaks (by hitting ctrl-enter in Word), resulting in blank pages. These page breaks will be represented in the XML as:
<w:br w:type="page"/>
The page will still have plenty of tags in it for spacing, fonts, etc.; and the page may display header and footers, too. But let's define a blank page as one which has no new paragraph text. In Open XML, new text is displayed with a w:t tag.
So, in order to remove blank pages created by extra page breaks with no text in between, we can run the following regular expression on the XML document, replacing with blank (""):
<w:br w:type="page"/>(.(?!<w:t>))*(?=<w:br w:type="page"/>)
This regex will search for a series of two or more page breaks with no new text in between, removing all but the last one.
(Note that this won't take care of blank pages at the end of the document, which is a bit trickier. Additionally, if you'd like to account for pages with images, textboxes, etc., the regex will have to be expanded to include the relevant items).

Related

How to renumber pages in a WordProcessingDocument?

I've created a large (~ 1000 pages) word document using the openxml-sdk. When i open it the first time using the word application, it shows "Word is renumbering the pages of test.docx" in the statusbar and does so for about 15 seconds. I've made a
german screenshot of this behaviour. After this step the document is changed and need to be saved. The new version of the file is about two times in size of the original one.
The document is saved the first time by simply calling
document.SaveAs("someFilePath");
What exactly is this behaviour? How can i renumber the pages in (or after) the creation of the document programmatically?
You can't renumber pages in a WordprocessingDocument, because those page numbers are not stored in the WordprocessingDocument but rather created while laying out, or rendering, the document.
A typical document defines page numbers as a complex field that you would find in the Open XML markup contained in the FooterPart (or possibly HeaderPart). Assuming the page number field is stored in the FooterPart, you might have a single FooterPart in your WordprocessingDocument in the simplest case (e.g., document with a single section). Even if you have multiple FooterParts, e.g., because you have multiple sections or you have a different layout for the first, odd, and even pages, you have relatively few FooterParts in your document (at least compared to your 1.000 pages).
When Word renders the document for printing or viewing, it also renders the page numbers based on your FooterParts (still using my example). For 1.000 pages, that takes time since Word is simply not built for documents that large.
Should you want to do Word's job and perform the layout yourself, you need to understand that building a layout engine is extremely complex and requires a lot of effort.

MigraDoc page break on sub-sections

I am using MigraDoc which might generate a document like the following example:
However I want to be able to 'bind' a given number of paragraphs/tables (or anything else) together so that if a page break is detected anywhere during any of the items, the whole block is moved onto the next page - for example (where highlighted text is all 'bound' together somehow):
Hope the question makes sense...?! I’m not sure where to start with this but have a definitive requirement for it!
Paragraphs have a KeepTogether property that prevents pagebreaks within the paragraph.
Paragraphs have a KeepWithNext property that prevents pagebreaks between this paragraph and the next one. A typical use case are head lines that make no sense at the bottom of the page.
For tables, see here:
https://stackoverflow.com/a/1327228/162529

Hide part of text temporarily, show after user clicks certain element

I'm making a detail page about certain items.
This detail page can contain large blocks of text, and the customer would like to only show the first 100 letters and then put a " ... more " at the end.
When the user clicks this " ... more " the rest of the text can be shown.
Biggest problem: the text is currently is a CMS and has large varieties. Some is pure text, some have html elements in them ...
I tried to cut off the text and put them in spans. Then i could show/hide these spans as i please. The issue here is that there can be a starting element of a certain tag in the first span and the closing element can be in the second span. This causes the DOM hierarchyto be faulty and the result is never pretty.
Does anyone know a ( other ) way to achieve this or a library i can use ?
To be able to extract "readable" characters you need to get the content into a plain text format (get rid of the mark-up).
Since the content is stored in a cms it is likely that the content is structured to be well formed - thus xhtml.
If that is the case you can treat the content as XML. Get the root node and get the innertext property there-of. Then you will have plain text - no tags - and can easily cut it after the first 100 characters or whatever the requirement is.
Hopefully the content doesn't contain js/css!
Edit:
It seems that the markup must be retained.
Try the following xsl to transform and truncate the content:
https://gist.github.com/allen/65817

How to tell height of paragraph in OpenXML?

I'm generating an MS Word document from user data. The data is placed in a container which is serialized to XML, and the resulting XML is converted to OpenXML using XSLT. There are a few minor changes done programmatically in C# to generate the Word document, as they can't be done with XSLT.
There is a user requirement that an item be placed completely on one page without any associated data being split onto another page. Sometimes one item will fill up an entire page, and sometimes I can fit three or four items on one page (I need to insert a separator (horizontal rule) between items that fit on the same page.)
Is there a way to determine whether or not one item or OpenXML paragraph will fit entirely on the "current" page? This can be either via C# or XSLT, and I can work something out.
Unfortunately, the only way this can be reliably done is to actually render the output, including all of the font sizes, bolding, kerning and all that. Which means you have to do the pagination in Word, and then save it back to the OpenXML.

How to retrieve all the paragraph in the specified particullar page using C#(4.0) and Open Xml Sdk(2.0)?

We are developing C#.Net(4.0) Windows Form Based Application with the use of Open Xml Sdk(2.0) for manipulating MS-WORD Files.Now i want to get the all the paragraphs in particular page.The user prompted for getting particular page no of the word file to get the all the paragraphs inside the user selected page number. How i do it?
Taking a quick look at the underlying XML it doesn't look like there is an attribute on the paragraph element that will tell you which page it will appear on. The best suggestion I can give you is to have some placeholder text at the top and bottom of each page. Then search for the a certain instance of the placeholder text based on which page the user specifies. Once you have a starting point you could retrieve all paragraphs between the two placeholder paragraph elements.
For example, if a user enters in page two, you would search for the third instance of a paragraph that contains this placeholder text and then retrieve all paragraphs until you reach the next instance of the placeholder text. I know this isn't ideal, but its one workaround I could think of that might be feasible.

Categories

Resources