Software Requirements:
- .Net Framework 3.5 or higher
- Visual studio 2010 or higher
- Pdf , Spire.Doc and Spire.License dll files
1. Open Visual Studio and create new Windows Form Application with c# code as shown below
2. Download and Add Spire.pdf and Spire.Doc dll references and other existing References as shown below
- Right click on References and GOTO -> Add Reference ->Assemblies->Framework and Add One reference from this: Microsoft.VisualBasic.dll
- Add downloaded Reference from Add Reference ->Browse-> Spire.Doc.dll, Spire.Pdf.dll and Spire.License.dll files ->Ok ( if you downloaded from NuGet packages from VS then it will be added automatically to your project so, no need to browse this reference )
-Figure is showing the example for adding references
3. Now the time for the design as show below
- Add two text boxes and buttons into form1 and design as follows
- Add one folderBrowserDialog into this form
- Make Two TextBoxes Enabled=false
4. Now we can start write the C# code
- Select Doc button click
private void bt_SelDoc_Click(object sender, EventArgs e) //Select Word File
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = “Document file (*.doc,*.docx) | *.doc; *.docx;”;
dlg.ShowDialog();
txt_DocPath.Text = dlg.FileName;
}
- Convert To Pdf button click
private void bt_ConvPdf_Click(object sender, EventArgs e) //Convert doc To pdf
{
if (txt_DocPath.Text != “”)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.FileName = “DocToPdf.pdf”;
savefile.Filter = “Pdf file (*.pdf) | *.pdf;”;
if (savefile.ShowDialog() ==
DialogResult.OK)
{
try
{
document.LoadFromFile(txt_DocPath.Text);
//Convert Word to PDF
document.SaveToFile(savefile.FileName, Spire.Doc.FileFormat.PDF);
//Launch Document
System.Diagnostics.Process.Start(savefile.FileName);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
else
{
MessageBox.Show(“Please select a file before convertion”, “File not found”, MessageBoxButtons.OK);
}
}
- Doc To Images button click
private void bt_Conv_DocToImg_Click(object sender, EventArgs e) //Doc to Images
{
if (txt_DocPath.Text != “”)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
try
{
if (!Directory.Exists(string.Format(@”{0}\DocToPng”, folderBrowserDialog1.SelectedPath)))
{
Directory.CreateDirectory(string.Format(@”{0}\DocToPng”, folderBrowserDialog1.SelectedPath));
}
document.LoadFromFile(txt_DocPath.Text);
for (int i = 0; i < document.PageCount; i++)
{
Image img = document.SaveToImages(i, Spire.Doc.Documents.ImageType.Metafile);
img.Save(String.Format(@”{0}\DocToPng\DocToPng_Page{1}.png”, folderBrowserDialog1.SelectedPath, i), ImageFormat.Png);
}
System.Diagnostics.Process.Start(string.Format(@”{0}\DocToPng\DocToPng_Page1.png”, folderBrowserDialog1.SelectedPath));
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
else
{
MessageBox.Show(“Please select a file before convertion”, “File not found”, MessageBoxButtons.OK);
}
}
- Doc To HTML button click
private void bt_Conv_DocToHtml_Click(object sender, EventArgs e) //Convert Doc to Html
{
if (txt_DocPath.Text != “”)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.FileName = “DocToHTML.html”;
savefile.Filter = “Web file (*.html) | *.html;”;
if (savefile.ShowDialog() ==
DialogResult.OK)
{
try
{
document.LoadFromFile(txt_DocPath.Text);
document.SaveToFile(savefile.FileName, Spire.Doc.FileFormat.Html);
System.Diagnostics.Process.Start(savefile.FileName);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
else
{
MessageBox.Show(“Please select a file before convertion”, “File not found”, MessageBoxButtons.OK);
}
}
- Lock Doc File button click
private void bt_LockDoc_Click(object sender, EventArgs e)
{
if (txt_DocPath.Text != “”)
{
string pwd = Microsoft.VisualBasic.Interaction.InputBox(“Enter password to lock the file”, “Lock”, “Password”, -1, -1);
if (pwd != “”)
{
document.LoadFromFile(txt_DocPath.Text);
document.Encrypt(pwd);
document.SaveToFile(“Encrypted_File.doc”, Spire.Doc.FileFormat.Doc);
}
else
{
MessageBox.Show(“Please provide password to lock the file”, “File not locked”, MessageBoxButtons.OK);
}
}
else
{
MessageBox.Show(“Please select a file to lock”, “File not found”, MessageBoxButtons.OK);
}
}
- Select Pdf button click
private void bt_SelPdf_Click(object sender, EventArgs e)//SElect Pdf File
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = “PDF file (*.pdf) | *.pdf;”;
dlg.ShowDialog();
txt_PdfPath.Text = dlg.FileName;
}
- Convert To Doc button click
private void bt_Convdoc_Click(object sender, EventArgs e) // Pdf To Doc
{
if (txt_PdfPath.Text != “”)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.FileName = “PDFtoDoc.doc”;
savefile.Filter = “Document file (*.doc,*.docx) | *.doc; *.docx;”;
if (savefile.ShowDialog() ==
DialogResult.OK)
{
try
{
pdfDoc.LoadFromFile(txt_PdfPath.Text);
pdfDoc.SaveToFile(savefile.FileName, Spire.Pdf.FileFormat.DOC);
System.Diagnostics.Process.Start(savefile.FileName);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
else
{
MessageBox.Show(“Please select a file before convertion”, “File not found”, MessageBoxButtons.OK);
}
}
- Pdf To Images button click
private void bt_Conv_PdftoImg_Click(object sender, EventArgs e) //Pdf To Images
{
if (txt_PdfPath.Text != “”)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
try
{
if (!Directory.Exists(string.Format(@”{0}\PdfToPng”, folderBrowserDialog1.SelectedPath)))
{
Directory.CreateDirectory(string.Format(@”{0}\PdfToPng”, folderBrowserDialog1.SelectedPath));
}
pdfDoc.LoadFromFile(txt_PdfPath.Text);
for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
Image img = pdfDoc.SaveAsImage(i, Spire.Pdf.Graphics.PdfImageType.Metafile);
img.Save(String.Format(@”{0}\PdfToPng\PdfToPng_Page{1}.png”, folderBrowserDialog1.SelectedPath, i), ImageFormat.Png);
}
System.Diagnostics.Process.Start(string.Format(@”{0}\PdfToPng\PdfToPng_Page1.png”, folderBrowserDialog1.SelectedPath));
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
else
{
MessageBox.Show(“Please select a file before convertion”, “File not found”, MessageBoxButtons.OK);
}
}
- Pdf To HTML button click
private void bt_Conv_PdfToHtml_Click(object sender, EventArgs e) //Pdf to Html
{
if (txt_PdfPath.Text != “”)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.FileName = “PdfToHTML.html”;
savefile.Filter = “Web file (*.html) | *.html;”;
if (savefile.ShowDialog() ==
DialogResult.OK)
{
try
{
pdfDoc.LoadFromFile(txt_PdfPath.Text);
pdfDoc.SaveToFile(savefile.FileName, Spire.Pdf.FileFormat.HTML);
System.Diagnostics.Process.Start(savefile.FileName.Replace(“.”, “_” + 1 + “-” + pdfDoc.Pages.Count + “.”));
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
else
{
MessageBox.Show(“Please select a file before convertion”, “File not found”, MessageBoxButtons.OK);
}
}
- Lock Pdf File button click
private void bt_LockPdf_Click(object sender, EventArgs e)
{
if (txt_PdfPath.Text != “”)
{
string pwd = Microsoft.VisualBasic.Interaction.InputBox(“Enter password to lock the file”, “Lock”, “Password”, -1, -1);
if (pwd != “”)
{
pdfDoc.LoadFromFile(txt_PdfPath.Text);
pdfDoc.Security.UserPassword = pwd;
pdfDoc.SaveToFile(“Encrypted_File.pdf”, Spire.Pdf.FileFormat.PDF);
}
else
{
MessageBox.Show(“Please provide password to lock the file”, “File not locked”, MessageBoxButtons.OK);
}
}
else
{
MessageBox.Show(“Please select a file to lock”, “File not found”, MessageBoxButtons.OK);
}
}
Note:
- For Pdf file conversion use Spire.pdf dll
- For Doc file conversion use Spire.doc dll
Read more articles:
HI..
Thanks RNTP
LikeLike
Amigo cuando abro el documento que se acaba de crear, en la parte de superior derecha aparece el siguiente mensaje: “Evaluation Warning : The document was created with Spire.PDF for .NET.”
Como hago para quitar ese mensaje??
LikeLike
para eliminar ese mensaje necesitas comprar ese producto, estás usando una versión de prueba, en la versión de prueba que mostrará el mensaje,
Para comprar puede ir a su sitio web
https://www.e-iceblue.com/
LikeLike