This is a migrated thread and some comments may be shown as answers.

RadFlowDocument export to pdf - images position on page

3 Answers 607 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Neven
Top achievements
Rank 2
Neven asked on 12 Feb 2016, 10:55 PM

I have to put 2 tif images on same page, one is regular "check" size,  other is "T4" doc size ...
Page orientation is landscape I did calculation , resizes images, everything works great ... BUT
"check image" position is on the bottom of page ?

I need it on top of page. Tried all alignments etc but did not figure out .

 I attached PDF page [as JPG] .

This is simple class that produced PDF.

 

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Windows.Documents.Flow.FormatProviders.Pdf;
using Telerik.Windows.Documents.Flow.Model;
using Telerik.Windows.Documents.Flow.Model.Editing;
using Telerik.Windows.Documents.Flow.Model.Styles;
using Telerik.Windows.Documents.Spreadsheet.Model;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using System.IO;

 namespace TEST{

class TELERIK_WordProcessing_PDFTest
{

public void ExportToPdf(string PdfURL)
{
Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider provider =
new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
using (Stream output = File.OpenWrite(PdfURL))
{
Telerik.Windows.Documents.Flow.Model.RadFlowDocument document = CreateRadFlowDocument();
provider.Export(document, output);
}
}
private Telerik.Windows.Documents.Flow.Model.RadFlowDocument CreateRadFlowDocument()
{
RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
editor.ParagraphFormatting.TextAlignment.LocalValue = Alignment.Justified;
Section section = editor.InsertSection();
section.PageMargins = new Telerik.Windows.Documents.Primitives.Padding(4);
//LANDSCAPE format 11 x 8.5 inches
double landscapeWidth = 11 * 96; //1056.00 pixels
double landscapeHeith = 8.5 * 96; //816.00 pixels
section.PageSize = new System.Windows.Size(landscapeWidth, landscapeHeith);
section.PageOrientation = Telerik.Windows.Documents.Model.PageOrientation.Landscape;
section.VerticalAlignment = Telerik.Windows.Documents.Flow.Model.Styles.VerticalAlignment.Top;
//insert 2 images , regular check and T4 in row, one one page
string imageBackCheck = @"C:\temp\test\BACK.tif";
string imageT4 = @"C:\temp\test\FrontA4Page.tif";
//open image to find size
int width = 0;
int height = 0;
SingleImageSize(imageT4, out width, out height);
using (FileStream fileStream = File.OpenRead(imageT4))
{
editor.InsertImageInline(fileStream, "tif", new System.Windows.Size(width, height));
}
SingleImageSize(imageBackCheck, out width, out height);
using (FileStream fileStream = File.OpenRead(imageBackCheck))
{
editor.InsertImageInline(fileStream, "tif", new System.Windows.Size(width, height));
}
return document;
}
private void SingleImageSize(string URL, out int destWidthFirst, out int destHeightFirst)
{
//need to shrink tif image to fit page [ 2 images in row ]
destWidthFirst = 0;
destHeightFirst = 0;
System.Drawing.Image FrontImage = System.Drawing.Image.FromFile(URL);
int sourceWidthFirst = FrontImage.Width;
int sourceHeightFirst = FrontImage.Height;
float nPercent = GetPercentShrink(sourceWidthFirst);
destWidthFirst = (int)(sourceWidthFirst * nPercent);
destHeightFirst = (int)(sourceHeightFirst * nPercent);
}
private float GetPercentShrink(int SourceWidth)
{
//some custom size calculation for lanscape mode [note margin is 4], 523 is half of page
int nConstWidth = 523;// 527;
return (float)nConstWidth / (float)SourceWidth;
}
}

 

}

3 Answers, 1 is accepted

Sort by
0
Accepted
Deyan
Telerik team
answered on 17 Feb 2016, 12:44 PM
Hello Neven,

Thank you for contacting us.

You may use a table in order to layout both images on top of the page. The following code snippet shows how this can be achieved:
RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
 
Table table = editor.InsertTable(1, 2);
 
editor.MoveToParagraphStart(table.Rows[0].Cells[0].Blocks.AddParagraph());
 
using (Stream firstImage = File.OpenRead("First.jpg"))
{
    editor.InsertImageInline(firstImage, "jpg");
}
 
editor.MoveToParagraphStart(table.Rows[0].Cells[1].Blocks.AddParagraph());
 
using (Stream secondImage = File.OpenRead("Second.jpg"))
{
    editor.InsertImageInline(secondImage, "jpg");
}


I hope this is helpful. If you have any other questions or concerns please do not hesitate to contact us again.

Regards,
Deyan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Neven
Top achievements
Rank 2
answered on 17 Feb 2016, 03:16 PM

OK ... Thanks  a lot .. It helped me .. Also helped me to understand concept of paragraph [that I did not pickup until now]

Small changes that I made to make code to work :

I must calculate size of image and call InsertImageInline with preferred size.

Also I must set size of cell [as I did bellow]

And must important, spent a time unitil figure out, I must move to end of table with  [editor.MoveToTableEnd(table);] before add next component

 

 


table = null;
cellX = null;
table = editor.InsertTable(1, 2);
table.Borders = new TableBorders(new Border(BorderStyle.None));
table.TableCellPadding = new Telerik.Windows.Documents.Primitives.Padding(0);
//calculate image size , landscape, half page
SingleImageSize(imageSMALLCheck, out width, out height);
cellX = table.Rows[0].Cells[0];
cellX.PreferredWidth = new TableWidthUnit(width);
cellX.Borders = new TableCellBorders(new Border(BorderStyle.None));
cellX.Padding = new Telerik.Windows.Documents.Primitives.Padding(0);
editor.MoveToParagraphStart(cellX.Blocks.AddParagraph());
using (Stream fileStream = File.OpenRead(imageSMALLCheck))
{
editor.InsertImageInline(fileStream, "tif", new System.Windows.Size(width, height));
}
//--------------------------------------------------
SingleImageSize(imageBIGT4, out width, out height);
cellX = table.Rows[0].Cells[1];
cellX.PreferredWidth = new TableWidthUnit(width);
cellX.Borders = new TableCellBorders(new Border(BorderStyle.None));
cellX.Padding = new Telerik.Windows.Documents.Primitives.Padding(0);
editor.MoveToParagraphStart(cellX.Blocks.AddParagraph());
using (Stream fileStream = File.OpenRead(imageBIGT4))
{
editor.InsertImageInline(fileStream, "tif", new System.Windows.Size(width, height));
}

editor.MoveToTableEnd(table);

0
Neven
Top achievements
Rank 2
answered on 17 Feb 2016, 03:20 PM
By the way.
I had other solution [your solution is better and I will use it  but this one also works].
I did draw 2 images side by side using .net System.Drawing.Bitmap/System.Drawing.Image namespace.
Got System.Drawing.Image, and add to editor flow as stream...


                //draw 2 images, side by side and add to SECTION
                //System.Drawing.Image FrontImage = LoadImage(URLFirst);
                int sourceWidthFirst = FrontImage.Width;//go from HorizontalResolution to find size//1277//2480
                int sourceHeightFirst = FrontImage.Height;
                float nPercent = GetPercentShrink(sourceWidthFirst);//only for check[CheckImagePercentShrink(int SourceWidth)]
                int destWidthFirst = (int)(sourceWidthFirst * nPercent);
                int destHeightFirst = (int)(sourceHeightFirst * nPercent);
                //System.Drawing.Image BackImage = LoadImage(URLSecond);
                int sourceWidthSecond = BackImage.Width;//go from HorizontalResolution to find size//1277//2480
                int sourceHeightSecond = BackImage.Height;
                nPercent = GetPercentShrink(sourceWidthSecond);
                int destWidthSecond = (int)(sourceWidthSecond * nPercent);
                int destHeightSecond = (int)(sourceHeightSecond * nPercent);
                //full size
                int newImageWidth = destWidthFirst + destWidthSecond;
                int newImageHeight = destHeightFirst > destHeightSecond ? destHeightFirst : destHeightSecond;
                System.Drawing.Bitmap newImageBmp =
                    new System.Drawing.Bitmap(newImageWidth, newImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Graphics newImageGrx = System.Drawing.Graphics.FromImage(newImageBmp);
                newImageGrx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                newImageGrx.DrawImage(FrontImage, 0, 0, destWidthFirst, destHeightFirst);
                newImageGrx.DrawImage(BackImage, destWidthFirst, 0, destWidthFirst, destHeightSecond);
return (System.Drawing.Image)newImageBmp;
Tags
WordsProcessing
Asked by
Neven
Top achievements
Rank 2
Answers by
Deyan
Telerik team
Neven
Top achievements
Rank 2
Share this question
or