Aurigma Forums
»
Graphics Mill
»
Discussions – Graphics Mill
»
PdfWriter page size matching figure's bounding box
Rank: Member
Groups: Guest
Joined: 11/19/2014(UTC) Posts: 10
Thanks: 4 times
|
Using PdfWriter to produce some text with effects, like BridgeText. My problem is Pdf's page size is defined before the text being produced so I don't know at that time of how large text is: Code: Text lText;
using (var lPdfWriter = new PdfWriter(Location.Temp() + aFileName + Extensions.PDF))
using (var lGraphics = lPdfWriter.GetGraphics())
{
lPdfWriter.AddPage(500, 500);
{
var lFont = lGraphics.CreateFont(aFont, aFontSize);
var lBrush = new SolidBrush(aColor);
lText = new PlainText(aText, lFont, lBrush)
{
Position = new System.Drawing.PointF(150f, 50f)
};
lGraphics.DrawText(lText);
}
}
So I was wondering if I could create the text before, adding the page to PdfWriter, CreateFont() depends on PdfWriter's GetGraphics so not sure how to do this. Also, is GetBlackBox() the right interface for getting the font's dimensions in order to pass them to AddPage() method?
|
|
|
|
Rank: Advanced Member
Groups: Guest
Joined: 7/28/2003(UTC) Posts: 1,660
Thanks: 5 times Was thanked: 76 time(s) in 74 post(s)
|
You can create a bitmap of the size 1x1 pixel with the same DPI as the PDF document and obtain the instance of AdvancedGraphics from the bitmap. Then you need to use the method GetBlackBox to measure the text size and pass it to the PdfWriter. Here is a full code: Code:var aFont = "Arial";
var aFontSize = 24f;
var aText = "Graphics Mill by Aurigma";
var aColor = System.Drawing.Color.Blue;
System.Drawing.RectangleF blackBox;
var dpi = 150f;
using (var bitmap = new Bitmap(1, 1, PixelFormat.Format24bppRgb))
{
bitmap.DpiX = dpi;
bitmap.DpiY = dpi;
using (var lGraphics = bitmap.GetAdvancedGraphics())
{
var lFont = lGraphics.CreateFont(aFont, aFontSize);
var lBrush = new SolidBrush(aColor);
var lText = new PlainText(aText, lFont, lBrush);
lText.Position = new System.Drawing.PointF(0f, 0f);
blackBox = lText.GetBlackBox();
}
}
using (var lPdfWriter = new PdfWriter(@"c:\eps\out.pdf"))
using (var lGraphics = lPdfWriter.GetGraphics())
{
lPdfWriter.AddPage((int)Math.Ceiling(blackBox.Width), (int)Math.Ceiling(blackBox.Height), dpi, dpi);
{
var lFont = lGraphics.CreateFont(aFont, aFontSize);
var lBrush = new SolidBrush(aColor);
var lText = new PlainText(aText, lFont, lBrush)
{
Position = new System.Drawing.PointF(0, - blackBox.Y)
};
lGraphics.DrawText(lText);
}
}
|
Best regards, Fedor Skvortsov
|
1 user thanked Fedor for this useful post.
|
|
|
Rank: Member
Groups: Guest
Joined: 11/19/2014(UTC) Posts: 10
Thanks: 4 times
|
Thank you, that helped a lot! One more thing with regard to this issue. After generating the PDF it's being converted to SVG and then loaded by FabricJS into Canvas; looks like there's an offset between the real object and the bounding box: Any ideas on this issue? Does it have to do with setting Position to 0f, 0f?
|
|
|
|
Rank: Member
Groups: Guest
Joined: 11/19/2014(UTC) Posts: 10
Thanks: 4 times
|
So I fixed the bounding box alignment issue by setting Text object position programatically after it was created, in the following way: Code:
BridgeText lBridgeText = new BridgeText(aText, lFont, lBrush)
{
Center = new System.Drawing.PointF(0f, 0f),
Bend = 0.2f
};
lBridgeText.Center = new System.Drawing.PointF(lBridgeText.GetBlackBox().Width / 2, lBridgeText.GetBlackBox().Height / 2);
However, PlainText() class does not contain a Center property so I was not able to fix the issue for this type of Text. Any ideas?
|
|
|
|
Rank: Member
Groups: Guest
Joined: 11/19/2014(UTC) Posts: 10
Thanks: 4 times
|
Fixed for PlainText in the following way, not sure why Position differs from Center but this is how it works (Bruteforced it): Code:PlainText lPlainText = new PlainText(aText, lFont, lBrush)
{
Position = new System.Drawing.PointF(0f, 0f)
};
lPlainText.Position = new System.Drawing.PointF(0f, lPlainText.GetBlackBox().Height);
|
|
|
|
Aurigma Forums
»
Graphics Mill
»
Discussions – Graphics Mill
»
PdfWriter page size matching figure's bounding box
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.