Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Nov 04, 2021 2:38 am

I'm converting the a .rtf file to svg then loading as image like in below code but I'd like to set the dimensions (height and width) of the converted image. How do I do that?

Code: Select all
string svgFilename = @"C:\path\to\file.svg";
var doc = new Document();
doc.LoadFromFile(@"C:path\to\file.rtf", FileFormat.Rtf);
doc.SaveToSVG(svgFilename);
var svgDoc = Svg.SvgDocument.Open(svgFilename);
var bitmap = svgDoc.Draw();

netorib94
 
Posts: 6
Joined: Thu Nov 04, 2021 2:32 am

Thu Nov 04, 2021 3:24 am

Hello,

Thanks for your message.
Please try to change the page size to modify the svg size. If there is still any questions, please share us with your rtf file as well as your desired svg effect for further testing. You could attach them here or send them to us via email(support@e-iceblue.com).
Code: Select all
 var doc = new Document();
 doc.LoadFromFile(@"C:path\to\file.rtf", Spire.Doc.FileFormat.Rtf);
 foreach (Section section in doc.Sections)
 {
     //change page size
     section.PageSetup.PageSize = new SizeF(400, 400);
 }
 doc.SaveToSVG(svgFilename);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Thu Nov 04, 2021 7:19 pm

Lisa.Li wrote:Hello,

Thanks for your message.
Please try to change the page size to modify the svg size. If there is still any questions, please share us with your rtf file as well as your desired svg effect for further testing. You could attach them here or send them to us via email(support@e-iceblue.com).
Code: Select all
 var doc = new Document();
 doc.LoadFromFile(@"C:path\to\file.rtf", Spire.Doc.FileFormat.Rtf);
 foreach (Section section in doc.Sections)
 {
     //change page size
     section.PageSetup.PageSize = new SizeF(400, 400);
 }
 doc.SaveToSVG(svgFilename);

Sincerely,
Lisa
E-iceblue support team


Hi Lisa, thanks for such quick answer! I still trying to adjust it. So let's say I want to set the size to a specific width and height, how would I do that? something like below?

Code: Select all
                float p1 = w * 72 / 96;
                float p2 = h * 72 / 96;
                section.PageSetup.PageSize = new SizeF(p1, p2);


For some reason, I'm getting a black image

netorib94
 
Posts: 6
Joined: Thu Nov 04, 2021 2:32 am

Fri Nov 05, 2021 2:33 am

Hello,

Thanks for your feedback.
Yes, the internal default unit of our Spire.Doc is “point”. You can use the code as you know, or you can refer to the following code to specify.
Code: Select all
.....
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
float width = unitCvtr.ConvertUnits(600, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
float height = unitCvtr.ConvertUnits(600, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
foreach (Spire.Doc.Section section in doc.Sections)
{
    //change page size
    section.PageSetup.PageSize = new SizeF(width, height);
}
......

Meanwhile, you can share us with your rtf file for testing on our side. Look forward to your further reply.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Mon Nov 08, 2021 7:38 pm

Lisa.Li wrote:Hello,

Thanks for your feedback.
Yes, the internal default unit of our Spire.Doc is “point”. You can use the code as you know, or you can refer to the following code to specify.
Code: Select all
.....
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
float width = unitCvtr.ConvertUnits(600, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
float height = unitCvtr.ConvertUnits(600, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
foreach (Spire.Doc.Section section in doc.Sections)
{
    //change page size
    section.PageSetup.PageSize = new SizeF(width, height);
}
......

Meanwhile, you can share us with your rtf file for testing on our side. Look forward to your further reply.

Sincerely,
Lisa
E-iceblue support team


Hello Lisa, it works like a charm! thanks! I have one more question: How do I set the dpi of the generated image by below code?

Code: Select all
           
 var doc = new Document();
doc.LoadFromFile(..., FileFormat.Rtf);
var bitmap = (Bitmap) doc.SaveToImages(Spire.Doc.Documents.ImageType.Bitmap)[0];

netorib94
 
Posts: 6
Joined: Thu Nov 04, 2021 2:32 am

Tue Nov 09, 2021 1:37 am

Hello,

Thanks for your feedback.
Please refer to this tutorial (How to convert RTF to Image and reset image resolution) to the dpi. If there is any other questions, just feel free to contact us.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Tue Nov 09, 2021 2:13 am

Lisa.Li wrote:Hello,

Thanks for your feedback.
Please refer to this tutorial (How to convert RTF to Image and reset image resolution) to the dpi. If there is any other questions, just feel free to contact us.

Sincerely,
Lisa
E-iceblue support team


Thanks but where is Metafile declared?

netorib94
 
Posts: 6
Joined: Thu Nov 04, 2021 2:32 am

Tue Nov 09, 2021 2:56 am

Hello,

Please refer to the below screenshot to specify the image type as Metafile when you save your rtf. Did you encounter any issues? If so, you can share us with your rtf file as well as the error details for our reference.
saveToImage.png

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Tue Nov 09, 2021 3:01 am

Lisa.Li wrote:Hello,

Please refer to the below screenshot to specify the image type as Metafile when you save your rtf. Did you encounter any issues? If so, you can share us with your rtf file as well as the error details for our reference.
saveToImage.png

Sincerely,
Lisa
E-iceblue support team


the compiler give an error about MetaFile type missing even albeit SPire.Doc is included but I no longer need it, I used this:

Code: Select all
            Bitmap bitmap = null;
            using (var img = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile)[0])
            {
                bitmap = new Bitmap(AppSettings.Instance.SeloSize.Width,
                                    AppSettings.Instance.SeloSize.Height);
                bitmap.SetResolution(dpi, dpi);
                using (var g = Graphics.FromImage(bitmap))
                {
                    g.DrawImage(img, Point.Empty);
                }
            }


Now how can I make the background image generated by doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile)[0] transparent?

netorib94
 
Posts: 6
Joined: Thu Nov 04, 2021 2:32 am

Tue Nov 09, 2021 3:33 am

Hello,

Sorry our product does not have a direct way to make images transparent. However, you can use the online methods to achieve it, such as the following sample code.
Code: Select all
......
 private static Image SetImageOpacity(Image srcImage, int opacity)
 {
     Bitmap img = new Bitmap(srcImage);
     using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
     {
         using (Graphics g = Graphics.FromImage(bmp))
         {
             g.DrawImage(img, 0, 0);
             for (int h = 0; h <= img.Height - 1; h++)
             {
                 for (int w = 0; w <= img.Width - 1; w++)
                 {
                     Color c = img.GetPixel(w, h);
                     if (!c.Equals(Color.FromArgb(0, 0, 0, 0)))
                     {
                         bmp.SetPixel(w, h, Color.FromArgb(opacity, c.R, c.G, c.B));
                     }
                     else
                     {
                         bmp.SetPixel(w, h, Color.FromArgb(c.A, c.R, c.G, c.B));
                     }
                 }
             }
         }
         Image ninaimg = (Image)bmp.Clone();
         ninaimg.Save("result.png");
         return (Image)bmp.Clone();
     }
 }
......

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Wed Nov 24, 2021 6:17 am

Lisa.Li wrote:Hello,

Sorry our product does not have a direct way to make images transparent. However, you can use the online methods to achieve it, such as the following sample code.
Code: Select all
......
 private static Image SetImageOpacity(Image srcImage, int opacity)
 {
     Bitmap img = new Bitmap(srcImage);
     using (Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
     {
         using (Graphics g = Graphics.FromImage(bmp))
         {
             g.DrawImage(img, 0, 0);
             for (int h = 0; h <= img.Height - 1; h++)
             {
                 for (int w = 0; w <= img.Width - 1; w++)
                 {
                     Color c = img.GetPixel(w, h);
                     if (!c.Equals(Color.FromArgb(0, 0, 0, 0)))
                     {
                         bmp.SetPixel(w, h, Color.FromArgb(opacity, c.R, c.G, c.B));
                     }
                     else
                     {
                         bmp.SetPixel(w, h, Color.FromArgb(c.A, c.R, c.G, c.B));
                     }
                 }
             }
         }
         Image ninaimg = (Image)bmp.Clone();
         ninaimg.Save("result.png");
         return (Image)bmp.Clone();
     }
 }
......

Sincerely,
Lisa
E-iceblue support team


Thanks for your support Lisa, you can close this question as solved.

netorib94
 
Posts: 6
Joined: Thu Nov 04, 2021 2:32 am

Wed Nov 24, 2021 6:57 am

Hello,

Glad to hear that, if you encounter any issues related to our products in the future, just feel free to contact us.
Wish you all the best!

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc