PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue May 14, 2024 3:06 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue Apr 01, 2014 7:36 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
Hi,
i am pretty new to coding and was wondering if anyone can offer me some help adding pdfsharp into my already existing mvc project. i am just looking for the basic steps needed to generate a pdf and what files i need to add references too,(controller, views, models., ect. ) i looked at a few samples and web searches but there isnt enough info for me to understand what and where i need to add the code....any help or support would be greatly appreciated. thank you.


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 02, 2014 7:55 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
Hi!

The Clock sample doesn't use MVC, but maybe it helps to get started:
http://www.pdfsharp.net/wiki/Clock-sample.ashx

The PDFsharp source package includes the complete sample code with solution, all references etc.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 08, 2014 9:53 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
thanks so much for your reply, i was able to get the project started but now i am having some issues adding an image to the pdf. i reviewed many samples but keep getting errors that the section is not part of the current context or that i am missing using or references. here is the code to generate my pdf, can you help me figure out why i can get an image to dispaly or how to remove the errors.

Code:
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Fonts;
using PdfSharp;
using System;
using System.Collections.Generic;
using ArdentMC.DHS.COP.Mobile.MVC.Models;
using System.Linq;
using System.Text;
using System.IO;
using MigraDoc.DocumentObjectModel.Shapes;
using System.Drawing;
using PdfSharp.Drawing.Layout;
using System.Web.Mvc;
using System.Web;
using System.Text.RegularExpressions;
using MigraDoc.DocumentObjectModel;
using System.Drawing.Imaging;
using HtmlRenderer;




namespace ArdentMC.DHS.COP.Mobile.MVC.Controllers
{
   
    class ReportPdfManager
    {
        protected int lineHeight = 10;
        protected int currentTop = 10;
        public static int margin = 20;
        protected int currentLeft = margin;
        public static int indentedMargin = margin + 20;
        protected PdfPage currentPage;


        public PdfDocument GenerateReport(Reports report)
        {
            var document = new PdfDocument();
            document.Info.Title = "";
            var page = document.AddPage();
            var graphics = XGraphics.FromPdfPage(page);
            var font = new XFont("Verdana", 10, XFontStyle.BoldItalic);
            //HtmlRenderer.HtmlContainer c = new HtmlRenderer.HtmlContainer();
            //c.SetHtml(report.Description);

            string ImageArea = @C:\TFS\DHS GMO\ArdentMC.DHS.COP.MapShell-PDFSharpDemoAlana\ArdentMC.DHS.COP.Mobile.MVC\Images\dhslogo.png;
            Image image = section.AddImage(ImageArea + "dhslogo.png");
            image.Width = "2.5cm";
            image.LockAspectRatio = true;

            image.RelativeHorizontal = RelativeHorizontal.Margin;

            image.WrapFormat.Style = WrapStyle.Through;

            graphics.DrawString("UNCLASSIFIED/FOR OFFICAL USE ONLY", font, XBrushes.Red, new XRect(margin, currentTop, page.Width, currentTop), XStringFormats.TopCenter);
            currentTop += lineHeight;
            graphics.DrawString(" " + report.Phase + "-" + report.NocNumber + " " + report.Title + "-" + report.Location, font, XBrushes.Black, new XRect(margin, currentTop, page.Width, page.Height), XStringFormats.TopCenter);
            currentTop += lineHeight;
            graphics.DrawString("US Department of Homeland Security              Report as of:" + report.ReportDateText, font, XBrushes.Black, new XRect(margin, currentTop, page.Width, page.Height), XStringFormats.TopCenter);
            currentTop += lineHeight;
            graphics.DrawString("" +  report.Description, font, XBrushes.Black, new XRect(margin, page.Height - (lineHeight * 35), page.Width, page.Height), XStringFormats.TopCenter);
            currentTop += lineHeight;
            graphics.DrawString("Prepared by the DHS National Operations Center", font, XBrushes.Black, new XRect(margin, page.Height - (lineHeight * 4), page.Width, page.Height), XStringFormats.TopCenter);
            currentTop += lineHeight;
            graphics.DrawString("UNCLASSIFIED/FOR OFFICAL USE ONLY", font, XBrushes.Red, new XRect(margin, page.Height - (lineHeight * 3), page.Width, page.Height), XStringFormats.TopCenter);
            return document;
        }
       
    }
}

thank you so much


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 09, 2014 7:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
adandrea wrote:
i reviewed many samples but keep getting errors that the section is not part of the current context or that i am missing using or references.
I don't see where your "section" object comes from (that's the problem with incomplete code snippets - the real problem could be somewhere else).
I presume you mix PDFsharp classes and MigraDoc classes.

Do not mix lines from MigraDoc samples with lines from PDFsharp samples.
Either use MigraDoc (with classes Document, Section, Image) or use PDFsharp (with PdfDocument, PdfPage, XImage).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 09, 2014 5:52 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
with the code I have now, what do think is the best option for me to use? also can you please give me a sample of how i can add it into my project? thank you so much. i can move forward until i figure this out so i really appreciate the help.


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 09, 2014 5:58 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
do you have a pdfsharp sample I can add into this project ? all i want to do is add in a deafult image that is saved in my project. ill like to add one to the top left corner and one in the center of the page. thank you in advance.


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 10, 2014 7:44 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
There is a web sample that shows how to send a PDF to the browser:
http://www.pdfsharp.net/wiki/Clock-sample.ashx
With MVC, this will be done in a slightly different way - but still you'll save the PDF to a stream and return the byte[].

There's another sample that shows what can be done with images:
http://www.pdfsharp.net/wiki/Graphics-sample.ashx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 10, 2014 10:10 pm 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
HI Thomas, thank you so much for your reply, however i have looked at both of these samples and cant seem to figure out how to add them to my project. is there anyway you can give me a more descriptive example. for example, when you say save pdf to a stream and return a byte. i am farely new to coding and could really use some more details. thank you so much. also if there are any specific using references i need that would help me a lot as well.

thank you so much.


Top
 Profile  
Reply with quote  
PostPosted: Sat Apr 12, 2014 12:22 am 
Offline

Joined: Tue Apr 01, 2014 7:33 pm
Posts: 19
Hi Thomas, I was able to figure it out using this code:

currentTop += lineHeight; // down a line
var assembly = GetType().Assembly;
var streamBitmap = assembly.GetManifestResourceStream("ArdentMC.DHS.COP.Mobile.MVC.Images.dhslogo20x20.png");
var dhsLogo = new Bitmap(streamBitmap);
var xImage = XImage.FromGdiPlusImage(dhsLogo);

var imageWidth = 40;
var imageHeight = 40;

var p1 = new XPoint(margin + 10, currentTop);
var p2 = new XPoint(margin + 10 + imageWidth, currentTop + imageHeight);
var imageDhsRect = new XRect(p1, p2);

graphics.DrawImage(xImage, imageDhsRect);

thanks for the help


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 76 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group