워드 파일을 프로그래밍 방식으로 PDF로 변환하려면 어떻게 해야 합니까?
.doc 파일을 .pdf 파일로 변환할 수 있는 여러 오픈 소스/프리웨어 프로그램을 찾았지만 모두 응용 프로그램/프린터 드라이버 버전이며 SDK는 첨부되지 않았습니다.
.doc 파일을 .pdf 파일로 변환할 수 있는 SDK가 있는 여러 프로그램을 찾았지만, 모두 독점 유형이며 라이센스당 2,000달러 또는 그와 유사합니다.
C# 또는 VB.NET을 사용하여 제 문제에 대한 깨끗하고 저렴한(가능하면 무료인) 프로그램 솔루션을 아는 사람이 있습니까?
감사합니다!
for 루프 대신 for 루프를 사용하면 문제가 해결됩니다.
int j = 0;
foreach (Microsoft.Office.Interop.Word.Page p in pane.Pages)
{
var bits = p.EnhMetaFileBits;
var target = path1 +j.ToString()+ "_image.doc";
try
{
using (var ms = new MemoryStream((byte[])(bits)))
{
var image = System.Drawing.Image.FromStream(ms);
var pngTarget = Path.ChangeExtension(target, "png");
image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
j++;
}
여기 저에게 효과가 있었던 프로그램을 수정한 것이 있습니다.다른 이름으로 저장 PDF 추가 기능이 설치된 Word 2007을 사용합니다.디렉토리에서 .doc 파일을 검색하여 Word로 연 다음 PDF로 저장합니다.Microsoft에 대한 참조를 추가해야 합니다.사무실. 인터럽트.해결책에 대한 말입니다.
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(@"\\server\folder");
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;
vb.net 사용자를 위해 요약하자면, 무료 옵션(사무실이 설치되어 있어야 함):
Microsoft Office 어셈블리 다운로드:
- 2010년 사무실용 pia
Microsoft에 대한 참조를 추가합니다.사무실. 인터럽트.Word.어플리케이션
Microsoft에 vb.net 문을 추가하거나 가져옵니다.사무실. 인터럽트.Word.어플리케이션
VB.NET 예:
Dim word As Application = New Application()
Dim doc As Document = word.Documents.Open("c:\document.docx")
doc.Activate()
doc.SaveAs2("c:\document.pdf", WdSaveFormat.wdFormatPDF)
doc.Close()
제가 마이크로소프트를 사용했다는 것을 덧붙이고 싶습니다.Interop 라이브러리, 특히 이 스레드에서 사용되지 않은 ExportAsFixedFormat 함수.
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Office.Core;
Application app;
public string CreatePDF(string path, string exportDir)
{
Application app = new Application();
app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
app.Visible = true;
var objPresSet = app.Documents;
var objPres = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
var pdfFileName = Path.ChangeExtension(path, ".pdf");
var pdfPath = Path.Combine(exportDir, pdfFileName);
try
{
objPres.ExportAsFixedFormat(
pdfPath,
WdExportFormat.wdExportFormatPDF,
false,
WdExportOptimizeFor.wdExportOptimizeForPrint,
WdExportRange.wdExportAllDocument
);
}
catch
{
pdfPath = null;
}
finally
{
objPres.Close();
}
return pdfPath;
}
PDFCreator에는 .NET 또는 VBScript(다운로드에 포함된 샘플)에서 호출할 수 있는 COM 구성 요소가 있습니다.
하지만 제가 보기에는 프린터가 바로 필요한 것 같습니다. Word의 자동화 기능과 함께 사용하면 됩니다.
저는 PDF로 변환하기 위해 10000개의 단어 파일을 누군가가 저에게 버렸을 때 워드에서 PDF로 고통을 겪었습니다.지금은 C#에서 하고 워드 인터럽트를 사용했지만 PC를 사용하려고 하면 속도가 느리고 충돌했습니다.매우 답답합니다.
이것은 제가 사용하는 Excel(EPLUS)을 위해 인터럽트와 그 속도를 덤프할 수 있다는 것을 발견하게 했고, 그리고 나서 제한 없이 PDF로 변환할 수 있는 Spire라는 무료 도구를 얻을 수 있다는 것을 알게 되었습니다!
http://www.e-iceblue.com/Introduce/free-doc-component.html #.VtAg4PmLRheE
사용이 간편한 코드 및 솔루션Microsoft.Office.Interop.WordPDF로 워드를 변환하다
using Word = Microsoft.Office.Interop.Word;
private void convertDOCtoPDF()
{
object misValue = System.Reflection.Missing.Value;
String PATH_APP_PDF = @"c:\..\MY_WORD_DOCUMENT.pdf"
var WORD = new Word.Application();
Word.Document doc = WORD.Documents.Open(@"c:\..\MY_WORD_DOCUMENT.docx");
doc.Activate();
doc.SaveAs2(@PATH_APP_PDF, Word.WdSaveFormat.wdFormatPDF, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue);
doc.Close();
WORD.Quit();
releaseObject(doc);
releaseObject(WORD);
}
메모리를 해제하려면 다음 절차를 추가합니다.
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
//TODO
}
finally
{
GC.Collect();
}
}
여기 관련 정보가 있는 것 같습니다.
또한 Office 2007에서 PDF로 게시 기능을 사용하면 사무 자동화를 사용하여 Word 2007에서 *.DOC 파일을 열고 PDF로 저장할 수 있을 것입니다.사무실 자동화는 느리고 걸리기 쉽기 때문에 별로 좋아하지 않지만, 그냥 그것을 밖에 버리는 것은...
Microsoft PDF 워드용 추가 기능이 현재로서는 가장 좋은 해결책인 것 같지만 모든 워드 문서를 PDF로 올바르게 변환하지는 않으며 경우에 따라 워드와 출력 PDF 간에 큰 차이가 나타날 수 있다는 점을 고려해야 합니다.안타깝게도 모든 워드 문서를 올바르게 변환할 수 있는 api를 찾을 수 없었습니다.변환이 100% 정확하다는 것을 확인할 수 있는 유일한 해결책은 프린터 드라이버를 통해 문서를 변환하는 것이었습니다.단점은 문서가 대기열에 저장되고 하나씩 변환되지만 결과 PDF가 워드 문서 레이아웃과 정확히 같다는 것입니다.저는 개인적으로 UDC(Universal Document Converter)를 사용하는 것을 선호했고 Foxit Reader(무료 버전)를 서버에 설치한 후 "Process"를 시작하고 동사 속성을 "print"로 설정하여 문서를 인쇄했습니다.또한 FileSystemWatcher를 사용하여 변환이 완료되면 신호를 설정할 수 있습니다.
언급URL : https://stackoverflow.com/questions/607669/how-do-i-convert-word-files-to-pdf-programmatically
'programing' 카테고리의 다른 글
| Azure 함수 내에서 다른 함수를 호출하는 방법 (0) | 2023.05.29 |
|---|---|
| 리소스가 템플릿에 정의되지 않았습니다. (0) | 2023.05.29 |
| XML에서 C# 클래스 생성 (0) | 2023.05.29 |
| 를 누를 때 UILongPressGestureRecognizer가 두 번 호출됩니다. (0) | 2023.05.29 |
| NLog로 기록된 오류를 이메일로 보내려면 어떻게 해야 합니까? (0) | 2023.05.29 |