Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Including custom text in pdf (Read 5988 times)
M Isabel Cabeza
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Mar 24th, 2014
Including custom text in pdf
Mar 24th, 2014 at 10:06am
Print Post  
Hello

I have used this component to represent diagrams in my windows application. Where diagram can be exported to pdf, but as I use the mindfusion export method, I get a pdf file with the diagram image at the end. I would like to add some custom text as header and footer or at least at the begining of the pdf document. Is this posible with the ofered api? is There any way of doing this??

Thanks a lot in advance

Kind regards
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Including custom text in pdf
Reply #1 - Mar 24th, 2014 at 12:41pm
Print Post  
Hi,

Try custom-drawing it from DrawBackground or DrawForeground event handler:

Code
Select All
private void diagram_DrawForeground(object sender, DiagramEventArgs e)
{
	var graphics = e.Graphics as PdfGraphics;
	if (graphics != null)
	{
		var pageContentRect = e.ClipRect;
		var font = new Font("Arial", 10);

		graphics.DrawString(
			"header", font, Brushes.Black, pageContentRect,
			new StringFormat
			{
				Alignment = StringAlignment.Center,
				LineAlignment = StringAlignment.Near
			});

		graphics.DrawString(
			"footer", font, Brushes.Black, pageContentRect,
			new StringFormat
			{
				Alignment = StringAlignment.Center,
				LineAlignment = StringAlignment.Far
			});

		font.Dispose();
	}
} 



You could insert a title page by exporting a DiagramDocument, whose first page displays the text you want, and the second page is set to the actual diagram content. You could either assign the title text to a single transparent ShapeNode on the first page, or custom-draw it as above.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
M Isabel Cabeza
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Mar 24th, 2014
Re: Including custom text in pdf
Reply #2 - Mar 24th, 2014 at 2:26pm
Print Post  
I have test this code, but it does not seems to work. I am using a  PdfExporter and calling  "pdfExporter.Export(FlowChart, fileName);" and this is not redrawing neither foreground or background, although I amd registering the event.

any idea???

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Including custom text in pdf
Reply #3 - Mar 24th, 2014 at 4:04pm
Print Post  
What version of the control are you using?
  
Back to top
 
IP Logged
 
M Isabel Cabeza
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Mar 24th, 2014
Re: Including custom text in pdf
Reply #4 - Mar 24th, 2014 at 4:16pm
Print Post  
the MindFusion.Diagramming.Export.Pdf.dll is the version 5.8.0.23716. the control is 5.8.0.23715
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Including custom text in pdf
Reply #5 - Mar 25th, 2014 at 8:01am
Print Post  
That also worked with version 5.8 for me, using default PdfExporter settings. What does your export code look like?
  
Back to top
 
IP Logged
 
M Isabel Cabeza
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Mar 24th, 2014
Re: Including custom text in pdf
Reply #6 - Mar 25th, 2014 at 9:05am
Print Post  
Something like this:


Code
Select All
    var pdfExporter = new PdfExporter
                {
                    AutoScale = PdfExportOptions.Instance.PdfAutoScale,
                    InvisibleItems = PdfExportOptions.Instance.InvisibleItems,
                    Margins = PdfExportOptions.Instance.Margins,
                    PageOrientation = PdfExportOptions.Instance.PageOrientation,
                    PageSize = PdfExportOptions.Instance.PageSize,
                    Scale = PdfExportOptions.Instance.PdfScale,
                };

            // Remove the expandable sign from nodes
            var expandableNodes = new List<MindFusion.Diagramming.DiagramNode>();
            foreach (var node in FlowChart.Nodes.Cast<MindFusion.Diagramming.DiagramNode>().Where(node => node.Expandable))
            {
                expandableNodes.Add(node);
                node.Expandable = false;
            }

            DiagramItemCollection oldFlowChartSelection;
            SelectionBlock oldSketchMatrixSelection;
            bool oldShowSketchMatrixGridLines;

            // Store selection
            DeselectFlowChartItemsBeforeExport(out oldFlowChartSelection, out oldSketchMatrixSelection, out oldShowSketchMatrixGridLines);

            // Special case, when generating the WMF image the RTF nodes has to have
            // the IsPrinting property to true in order to right calculate the size for the
            // custom draw for that nodes
            foreach (DiagramNode node in _diagramNodes.Values)
            {
                if (node is RTFNode)
                    (node as RTFNode).IsPrinting = true;
            }

            try
            {
                // Get this old zoom and scroll values for restore after printing
                oldZoomFactorForPrint = diagramView.ZoomFactor;
                oldScrollX = diagramView.ScrollX;
                oldScrollY = diagramView.ScrollY;

                diagramView.ZoomFactor = 100;
                pdfExporter.RenderOptions.Scale = 100;

                pdfExporter.Export(FlowChart, fileName);
                if (!bUnattended)
                    Process.Start(fileName);
            }
            catch (Exception exc)
            {
                MessageBox.Show("An error occured during the export to PDF. The diagram may contain elements that are not supported by the PDF exporter.\n" + exc.Message, "Error in export to PDF", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Handler.Show(exc);
            } 

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Including custom text in pdf
Reply #7 - Mar 26th, 2014 at 8:38am
Print Post  
What settings are there in PdfExportOptions when it doesn't work? I got the attached PDF file with following code:

Code
Select All
private void button1_Click(object sender, EventArgs e)
{
	saveFileDialog.DefaultExt = "pdf";
	saveFileDialog.Filter = "PDF files|*.pdf";
	if (saveFileDialog.ShowDialog() == DialogResult.OK)
	{
		var pdfExporter = new MindFusion.Diagramming.Export.PdfExporter
		{
			AutoScale = MindFusion.Diagramming.Export.AutoScale.FitToWidth,
			InvisibleItems = true,
			Margins = new Margins(10, 10, 10, 10),
			PageOrientation = PageOrientation.Landscape,
			PageSize = PageSize.A4,
			Scale = 100,
		};

		pdfExporter.Export(diagramView1.Diagram, saveFileDialog.FileName);
	}
}

private void diagram_DrawForeground(object sender, DiagramEventArgs e)
{
	var graphics = e.Graphics as PdfGraphics;
	if (graphics != null)
	{
		var pageContentRect = e.ClipRect;
		var font = new Font("Arial", 10);

		graphics.DrawString(
			"header", font, Brushes.Black, pageContentRect,
			new StringFormat
			{
				Alignment = StringAlignment.Center,
				LineAlignment = StringAlignment.Near
			});

		graphics.DrawString(
			"footer", font, Brushes.Black, pageContentRect,
			new StringFormat
			{
				Alignment = StringAlignment.Center,
				LineAlignment = StringAlignment.Far
			});

		font.Dispose();
	}
}  



If you set a breakpoint inside DrawForeground handler, does the debugger ever stop there when you export?
  

test_header.pdf (Attachment deleted)
Back to top
 
IP Logged
 
M Isabel Cabeza
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Mar 24th, 2014
Re: Including custom text in pdf
Reply #8 - Mar 26th, 2014 at 10:19am
Print Post  
I should have introduced an error because now it is working!!! thanks a lot!!!  Wink Wink Wink

Kind regards !!!
  
Back to top
 
IP Logged
 
M Isabel Cabeza
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Mar 24th, 2014
Re: Including custom text in pdf
Reply #9 - Apr 1st, 2014 at 1:46pm
Print Post  
Now another problem comes up. The diagram is exported using a PdfExporter, and the image of the diagram overlaps with the header.

Is there anyway to tell the exporter where to start writting? I have put the header and the footer in the flowChart_DrawBackground event.

Thansk a lot!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Including custom text in pdf
Reply #10 - Apr 1st, 2014 at 6:39pm
Print Post  
If you mean drawing text in the margin area, you could in theory achieve that by inflating the target rectangle a bit:

Code
Select All
var pageContentRect = e.ClipRect;
pageContentRect.Inflate(5, 5); 



However the control always clips content at the page margins at this time, so you won't see the headers then. We'll try to add an option to disable that clipping and implement built-in headers support for next release.

Meanwhile you could use some third-party PDF processing library to remove the clip command before header text, or even modify the PDF file directly by loading it into a string and modifying the clip parts. The PDF mark-up for the header above will look like this:

Code
Select All
36 36 522.9999 769.9999 re W n
...
(header)  Tj 



You can remove the clip region by replacing the "re W n" commands with "re n n".

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
M Isabel Cabeza
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Mar 24th, 2014
Re: Including custom text in pdf
Reply #11 - Apr 3rd, 2014 at 7:39am
Print Post  
Thanks, we will keep on looking for solutions.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Including custom text in pdf
Reply #12 - Apr 14th, 2014 at 1:12pm
Print Post  
This version adds HeaderFormat, HeaderFont, FooterFormat, FooterFont properties to PdfExporter:
https://mindfusion.eu/_beta/fcnet613.zip

Similarly to PrintOptions.HeaderFormat, %P is placeholder for current page number:

Code
Select All
pdfex.HeaderFormat = "page %P";
pdfex.FooterFormat = "copyright 2014";
pdfex.Export(diagram, sfd.FileName);
 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint