PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jul 14, 2024 10:21 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: open pdf in webform help
PostPosted: Mon Aug 25, 2014 5:24 pm 
Offline

Joined: Mon Aug 25, 2014 4:44 pm
Posts: 8
Hello everyone,

new to the forum and new to pdfsharp. I'm trying to create a C# winform program that will open a PDF from a file location, then redact(*) a portion of the PDF, and finally save the PDF in the same location with a random password encryption.
I have found many bits and pieces of code here on the forum and by using google, but the one thing that I still can't figure is how to actually open a PDF using winforms and C#. The issue seems to be that I'm not referencing the correct dll's. I have, for example, this simple code snippet that is supposed to open a file dialog and let me open a file:
Code:
  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PdfSharp;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
                dlg.ShowDialog();
                this.acroViewer.LoadFile(dlg.FileName);
            }
            //this.acroViewer.ShowToolbar = false;
        }
    }
}

But I get an error from VS2010 saying that there is no definition for acroviewer. I loaded the nuget package for PdfSharp and it shows in the references. So what else am I missing?

Thanks for the help!

Chris

(*) I know I'm not really redacting, merely covering things up, but the password protection is sort of a workaround to make it look like it's redacted. :wink:


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 26, 2014 8:00 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
You have to add something like "private PdfSharp.Viewing.PdfAcroViewer acroViewer;" to your form.

You should be able to add the PdfAcroViewer using the Winforms editor.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 26, 2014 1:52 pm 
Offline

Joined: Mon Aug 25, 2014 4:44 pm
Posts: 8
Thomas,

thank you for your reply. I have tried adding
Code:
using PdfSharp.Viewing;
and further on this
Code:
private PdfSharp.Viewing.PdfAcroViewer acroViewer;
. But I get an error saying that the "Type or Namespace Name "Viewing" does not exist in the Namespace'PdfSharp'.
So I'm not sure if I have the wrong package or if something has changed since the code I found was written and the code needs to be changed.


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 26, 2014 2:58 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
It seems that version 1.32 does not include the viewer.
Go to http://pdfsharp.codeplex.com/releases/view/37054 and download PDFSharp-MigraDocFoundation-1_31.zip

After unzipping, you'll find the project in PDFsharp\dev\PdfViewer.Demo
The solution PDFsharp-VS2008.sln is in the parent directory. Newer VS versions should be able to convert it.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 27, 2014 2:12 pm 
Offline

Joined: Mon Aug 25, 2014 4:44 pm
Posts: 8
Thomas,

thanks for the link. I downloaded 1.31 but when I try to open and build the PdfSharp-VS2008 i get multiple errors. I'm not sure if I'm doing everything right, so let me just tell you the steps:
1. Open the PdfSharp-VS2008 and converting it (that works fine with only a handful of warnings, no errors)
2. Click on the Solution and then click "Build"
3. I get multiple errors and warnings:
    Error 1 Assembly signing failed; output may not be signed -- Error signing assembly -- The system cannot find the file specified. PdfSharp
    Error 2 Warning as Error: 'System.Drawing.FontFamily.GetFamilies(System.Drawing.Graphics)' is obsolete: 'Do not use method GetFamilies, use property Families instead' C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\PdfSharp.Drawing\XFontFamily.cs 267 18 PdfSharp
    Error 3 Metadata file 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll' could not be found PdfSharp.Charting
    Error 4 Metadata file 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll' could not be found PdfSharp.Ghostscript
    Error 5 Metadata file 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll' could not be found PrivateFonts
    Error 6 Could not load referenced assembly "C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll". Caught a FileNotFoundException saying "Could not load file or assembly 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll' or one of its dependencies. The system cannot find the file specified.". C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\dev\PDFsharp.Explorer\ResGen PdfSharp.Explorer
    Error 7 Could not load referenced assembly "C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll". Caught a FileNotFoundException saying "Could not load file or assembly 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll' or one of its dependencies. The system cannot find the file specified.". C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\dev\XGraphicsLab\ResGen XGraphicsLab
    Error 8 Could not load referenced assembly "C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp.Charting\bin\Debug\PdfSharp.Charting.dll". Caught a FileNotFoundException saying "Could not load file or assembly 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp.Charting\bin\Debug\PdfSharp.Charting.dll' or one of its dependencies. The system cannot find the file specified.". C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\dev\PdfSharp.Charting.Demo\ResGen PdfSharp.Charting.Demo
    Error 9 Could not load referenced assembly "C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll". Caught a FileNotFoundException saying "Could not load file or assembly 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll' or one of its dependencies. The system cannot find the file specified.". C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\dev\PdfSharp.Ghostscript.Demo\ResGen UseGhostscript
    Error 10 Could not load referenced assembly "C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll". Caught a FileNotFoundException saying "Could not load file or assembly 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll' or one of its dependencies. The system cannot find the file specified.". C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp.AcroPdfLib\ResGen PdfSharp.AcroPdfLib
    Error 11 System.ApplicationException - Unable to located specified reference: 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\code\PdfSharp\bin\Debug\PdfSharp.dll' C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\dev\PdfViewer.Demo\TlbImp PdfViewer.Demo
    Warning 12 Cannot find wrapper assembly for type library "AcroPDFLib". Verify that (1) the COM component is registered correctly and (2) your target platform is the same as the bitness of the COM component. For example, if the COM component is 32-bit, your target platform must not be 64-bit. PdfViewer.Demo
    Error 13 Unable to locate referenced assembly: 'C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\dev\PdfViewer.Demo\obj\Debug\Interop.AcroPDFLib.dll' C:\Users\KokatatTest\Documents\Visual Studio 2010\Projects\PdfSharp\PDFsharp\dev\PdfViewer.Demo\AXIMP PdfViewer.Demo
    Warning 14 Cannot find wrapper assembly for type library "AxAcroPDFLib". Verify that (1) the COM component is registered correctly and (2) your target platform is the same as the bitness of the COM component. For example, if the COM component is 32-bit, your target platform must not be 64-bit. PdfViewer.Demo

When I open the PdfViewer.Demo I get the following errors:

    Warning 1 The referenced component 'PdfSharp' could not be found.
    Warning 2 The referenced component 'PdfSharp.AcroPdfLib' could not be found.

Somehow I feel like I'm doing something fundamentally wrong, i.e. I'm not installing/using the package correctly.

Any ideas?

Sorry for being a pain,

Chris


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 27, 2014 2:29 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Compile the project, open the Output window and search for ": error". Address this error and compile again.

Probably you just have to disable the "Treat warnings as errors" option in the project settings.
"Error 2 Warning as Error" suggests something like that.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 28, 2014 1:56 pm 
Offline

Joined: Mon Aug 25, 2014 4:44 pm
Posts: 8
Thomas,

I'm not sure what I did differently today than I did yesterday, but I'm happy to report that it works now!
Thank you for helping me out and I'm sure as my project goes along I will have many more questions.

Cheers,

Chris


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 28, 2014 2:03 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
campagnolo_1 wrote:
I'm not sure what I did differently today than I did yesterday, but I'm happy to report that it works now!
Maybe you switched from Release to Debug.
Remember the tip from my last post when you switch to Release again ...

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 26 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group