I want to create a PDF file that should contain a table and, under this table, three images; the images should have an horizontal layout (they should be on the same line).
How can I align the three images in MigraDoc?
If I add the images in this way
document.LastSection.AddImage("path1");
document.LastSection.AddImage("path2");
document.LastSection.AddImage("path2");
I obtain three images with a vertical layout under the table.
If I use
document.LastSection.LastParagraph.AddImage("...");
instead of
document.LastSection.AddImage("...")
I solve the problem but I introduce a new problem.
The table that I've added using
var table1 = new Table();
.....
document.LastSection.Add(table1);
appears under the three images.
What can I do to obtain the table and under the table the three images with horizontal layout?
How about var para=document.LastSection.AddParagraph(); and then calling para.AddImage(...); to add the three images to one paragraph?
MigraDoc is dynamic - and document.LastSection.LastParagraph obviously returns the last paragraph before the table. To cure this, just add a paragraph after the table and add the images to this paragraph.
Related
I recently started a project using PDF sharp + Migra Doc and I encounter a problem which I have seen in other posts, there is no fixing automatically. Table row will be generated on the next page if it doesn't have enough space and if there is still not enough space it will just go into the border and the text is lost. I am thinking of a workaround but I am not sure exactly how it can be done.
My think is as follows:
If I am able to check how many lines of text can fit in cell with the given string I can create a variable and increase it every time I add text. with the excess of text I can simply create a new row (which will be automatically be added on the next page) and thus fixing my problem. Even if I am not counting lines, is there a way to check if the row becomes too large for the current page? If at a given time I can check if the cell is too large and will be automatically sent to the next page I can trim the string up to the point it will fit, save the remaining words that didn't fit and maximise the space within the page.
this is how the document is generated currently
Is there a way to workaround this? That white space is useless and a waste of resources when it comes to a 30-40 pages document.
One extreme option: Make the layout in your code and use PDFsharp to draw the text.
See also:
https://forum.pdfsharp.net/viewtopic.php?f=8&t=3192
A MigraDoc cell can contain a mix of different fonts with different font attributes (regular, bold, ...) and sizes. Measuring the size and creating a new row can become complicated if you mix different fonts, but it can be simple if you only use a single font for your cell.
See also:
https://forum.pdfsharp.net/viewtopic.php?f=8&t=3196
The space problem with tables occur if table rows are rather large (more than just one or two lines of text). Maybe tables are not the best option to present the information. How strict are your requirements? Can you get away from tables?
The solution that finally worked was as follows:
set up the style for the document including the header
depending on the data used create a for-loop which will input the desired rows in the table
top of the loop must add a row in the document
save in a variable how many pages the document currently contains(initially declare as 1 before entering the loop)
clone the document checking if the document you are passing contains the same number or more than the current document. If the document contains more pages means that the row you inputted exceeds the page. I was able to achieve this by rendering the document every time I was adding a new row.
an inner loop is necessary to trim the text within the row. The way I did it is split the text into sentences and if it contains more than 3 sentences trim, otherwise just let go to the next page.
make sure you always delete the last row on the inner loop otherwise you will end up with the same data
It might not be the most efficient way but it renders 30+ pages documents in tables under 2 seconds on Azure servers. I hope this helps someone at some point.
Using Microsoft Interop for word, after adding a table to the document, how can the column width be set for all columns so that it fits the largest item there? For example, if the column header is only two letters, and each cell underneath is only one digit, the column should only be about a centimeter wide.
If you only have one table in your document, you would use something like this (where oDoc is your active document.)
oDoc.Tables(0).AllowAutoFit = True;
oDoc.Tables(0).AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
If you have more than one table, you'd want to choose the index of the one you wish to update or loop through them.
In the Word object model AutoFit is what the feature is called that allows (or doesn't allow) table columns to resize to fit the content, the width of the window/page or prevents them from resizing automatically.
To force the table columns to resize to fit their content:
tbl.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
To change the width of a single column to fit to the content:
tbl.Columns[index].AutoFit();
This can also be done for all columns:
tlb.Columns.AutoFit();
I'm adding an image to a Word document using OpenXml.
I am able to tell the size of the image being added, and I can resize it if I want to. But what I really want is to only resize it if it's wider than the current column. (In my case, there is only one column but perhaps that could change.)
Is there any way to know how wide the current column is and, therefore, be able to ensure that the image fits within the column? As it is, large images are extending off the page.
I've updated my answer, because it was an answer for a case when table columns are involved, but the word column in the question mean a column or text rather than a column in a table.
In the document, You should be able to get the following values:
// ...
var sectionProperties = body.GetFirstChild<SectionProperties>();
// pageSize contains Width and Height properties
var pageSize = sectionProperties.GetFirstChild<PageSize>();
// this contains information about surrounding margins
var pageMargin = sectionProperties.GetFirstChild<PageMargin>();
// this contains information about spacing between neighbouring columns of text
// this can be useful if You use page layout with multiple text columns
var columns = sectionProperties.GetFirstChild<Columns>();
var spaceBetweenColumns = columns.Space.Value;
var columnsCount = columns.ColumnCount.Value;
I haven't tested this, but I suppose You may be able to calculate the actual width of a text column using those values.
how can i create a column that can display 2 images in one column on datagridview?
i tried to create 2 datagridviewimagecolumn (for the different images)
and if it the cell in it contains the same image path.
the image will match depending on the path on the cell.
this must combine in one column (as shown on the figure below)
for example.
here i will create 2 datagridimagecolumn
DataGridViewImageColumn img1 - new DataGridViewImageColumn();
Image Simg1 = Image.FromFile (C:\Desktop\Green.jpg);
img1.Image = Simg1;
dgv.Columns.Add(img1); //it will display on the datagridview
DataGridViewImageColumn img2 - new DataGridViewImageColumn();
Image Simg2 = Image.FromFile (C:\Desktop\Red.jpg);
img2.Image = Simg1;
dgv.Columns.Add(img2);
if the generated column from the sql server looks like this
column1
---------
c:\Desktop\Greeen.jpg
c:\Desktop\Green.jpg
c:\Desktop\Red.jpg
c:\Desktop\Green.jpg
how will i do this one.
the green path will display green image.
and the red path will contain red image.
I don't know if I understand well your question but I think you can work around creating an Image that is the tiling of the 2 images and print it inside your column.
you can see here or here how to do that.
You'll need to write your own merged cell class implementing onpaint event. You can see an example of it's being done here.
I want to add Bookmark in my PDF using MigraDoc.
For example: two images on a single page.
1. Images1
2. Images2**
and the same name bookmark will generate.
If I click on image1 bookmark that image will be shown to me. Remember both images are on single page.
MigraDoc creates bookmarks automatically for headings.
To create bookmarks without visible text on the page, you can create headings with a font size of e.g. 0.0001 and white colour.
There is one drawback: up to PDFsharp 1.50 beta 1, these bookmarks jump to the correct page, but not the correct area on the page. So with two images on one page, the bookmarks will not work as intended by the OP.