pdf generation - Konverter Doc-fil til PDF i VB.Net

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg har en situation, hvor jeg skal konvertere Doc-fil til PDF-fil. Jeg er devepoing windows ansøgning i vb.net. og jeg vil heller ikke bruge tredjeparts dll, hvis det er muligt.
så kan nogen give mig en mere ide?

Bedste reference


Du kan bruge Office Interop til dette. Men det er bedre at bruge nogle administrerede bibliotek som Aspose


using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

...

// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;

// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\serverfolder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

word.Visible = false;
word.ScreenUpdating = false;

foreach (FileInfo wordFile in wordFiles)
{
    // Cast as Object for word Open method
    Object filename = (Object)wordFile.FullName;

    // Use the dummy value as a placeholder for optional arguments
    Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    doc.Activate();

    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;

    // Save document into PDF Format
    doc.SaveAs(ref outputFileName,
        ref fileFormat, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type \_Document so that it will find the
    // correct Close method.                
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((\_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
    doc = null;
}

// word has to be cast to type \_Application so that it will find
// the correct Quit method.
((\_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;

Andre referencer 1


Imports Microsoft.Office.Interop

'This code happens to be loading a template, but it isn't necessary...

'Opens Word Application

Dim MyApp As New Word.Application

'Opens new WordDoc

Dim MyWordDoc As Word.Document = MyApp.Documents.Add(template)

MyApp.Visible = True

MyWordDoc = MyApp.ActiveDocument

'code to fill doc

'code to fill doc

'code to fill doc

MyWordDoc.SaveAs(FileLocation, Word.WdSaveFormat.wdFormatPDF)

Andre referencer 2


Microsoft Office Add-in 2007: Microsoft Gem som PDF og 2007 Microsoft Office-tilføjelse: Microsoft Save as XPS tillader et Microsoft Office Word 2007 at eksportere og gemme dokumenter i PDF- og XPS-formaterne. [3] [4]


Tjek disse:

Gemmer Word 2007-dokumenter til PDF- og XPS-formater

Sådan konverteres Word til PDF i asp.net [5] [6]


Hvis du vil bruge Thirt party dll, så tjek denne SO tråd: Konvertering MS Word Documents til PDF i ASP.NET