Кодеки: Как преобразовать DOCX в PDF?
В этом разделе
VintaSoft Imaging .NET SDK может конвертировать документ DOCX в векторный PDF или PDF/A документ с текстом, ссылками и навигацией.
Для выполнения приведенного ниже кода необходимо:
- VintaSoft Imaging .NET SDK (Vintasoft.Shared.dll; Vintasoft.Imaging.dll; Vintasoft.Imaging.Gdi.dll или Vintasoft.Imaging.Drawing.SkiaSharp.dll)
- VintaSoft Office .NET Plug-in (Vintasoft.Imaging.Office.OpenXml.dll)
- VintaSoft PDF .NET Plug-in (Vintasoft.Imaging.Pdf.dll)
- System.IO.Packaging nuget-пакет (nuget-пакет используется Vintasoft.Imaging.Office.OpenXml.dll)
-
System.Drawing.Common nuget-пакет (nuget-пакет необходим, если GDI+ следует использовать в качестве движка для 2D-рисования в VintaSoft Imaging .NET SDK) или
SkiaSharp nuget-пакет (nuget-пакет необходим, если SkiaSharp следует использовать в качестве движка для 2D-рисования в VintaSoft Imaging .NET SDK)
Вот код C#/VB.NET, который показывает, как преобразовать DOCX-документ в PDF-документ:
/// <summary>
/// Converts DOCX document to a PDF document using ImageCollection and PdfEncoder classes.
/// </summary>
public static void ConvertDocxToPdf(string docxFileName, string pdfFileName)
{
// specify that VintaSoft Imaging .NET SDK should use GDI+ for drawing of 2D graphics
Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault();
// specify that VintaSoft Imaging .NET SDK should use SkiaSharp for drawing of 2D graphics
//Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault();
// create image collection
using (Vintasoft.Imaging.ImageCollection imageCollection = new Vintasoft.Imaging.ImageCollection())
{
// add DOCX document to collection
imageCollection.Add(docxFileName);
// create pdfEncoder
using (Vintasoft.Imaging.Codecs.Encoders.PdfEncoder pdfEncoder =
new Vintasoft.Imaging.Codecs.Encoders.PdfEncoder(true))
{
// set comression for image resources
pdfEncoder.Settings.Compression = Vintasoft.Imaging.Codecs.Encoders.PdfImageCompression.Jpeg;
// save images of image collection to PDF document using PdfEncoder
imageCollection.SaveSync(pdfFileName, pdfEncoder);
}
// dispose images
imageCollection.ClearAndDisposeItems();
}
}
''' <summary>
''' Converts DOCX document to a PDF document using ImageCollection and PdfEncoder classes.
''' </summary>
Public Shared Sub ConvertDocxToPdf(docxFileName As String, pdfFileName As String)
' specify that VintaSoft Imaging .NET SDK should use GDI+ for drawing of 2D graphics
Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault()
' specify that VintaSoft Imaging .NET SDK should use SkiaSharp for drawing of 2D graphics
'Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault()
' create image collection
Using imageCollection As New Vintasoft.Imaging.ImageCollection()
' add DOCX document to collection
imageCollection.Add(docxFileName)
' create pdfEncoder
Using pdfEncoder As New Vintasoft.Imaging.Codecs.Encoders.PdfEncoder(True)
' set comression for image resources
pdfEncoder.Settings.Compression = Vintasoft.Imaging.Codecs.Encoders.PdfImageCompression.Jpeg
' save images of image collection to PDF document using PdfEncoder
imageCollection.SaveSync(pdfFileName, pdfEncoder)
End Using
' dispose images
imageCollection.ClearAndDisposeItems()
End Using
End Sub
Вот код C#/VB.NET, который показывает, как преобразовать DOCX-документ в документ PDF/A-1b:
/// <summary>
/// Converts DOCX document to a PDF/A-1b document using ImageCollection and PdfEncoder classes.
/// </summary>
public static void ConvertDocxToPdfA1b(string docxFileName, string pdfFileName)
{
// specify that VintaSoft Imaging .NET SDK should use GDI+ for drawing of 2D graphics
Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault();
// specify that VintaSoft Imaging .NET SDK should use SkiaSharp for drawing of 2D graphics
//Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault();
// create image collection
using (Vintasoft.Imaging.ImageCollection imageCollection = new Vintasoft.Imaging.ImageCollection())
{
// add DOCX document to collection
imageCollection.Add(docxFileName);
// create pdfEncoder
using (Vintasoft.Imaging.Codecs.Encoders.PdfEncoder pdfEncoder =
new Vintasoft.Imaging.Codecs.Encoders.PdfEncoder(true))
{
// set PDF/A-1b (ISO 19005-1, Level B conformance)
pdfEncoder.Settings.Conformance = Vintasoft.Imaging.Pdf.PdfDocumentConformance.PdfA_1b;
// set CMYK ICC profile, which is used by PDF/A converter as a profile in DefaulRGB color space
pdfEncoder.Settings.PdfADefaultRgbIccProfileFilename = "DefaultRGB.icc";
// set comression for image resources
pdfEncoder.Settings.Compression = Vintasoft.Imaging.Codecs.Encoders.PdfImageCompression.Jpeg;
// save images of image collection to PDF/A-1b document using PdfEncoder
imageCollection.SaveSync(pdfFileName, pdfEncoder);
}
// dispose images
imageCollection.ClearAndDisposeItems();
}
}
''' <summary>
''' Converts DOCX document to a PDF/A-1b document using ImageCollection and PdfEncoder classes.
''' </summary>
Public Shared Sub ConvertDocxToPdfA1b(docxFileName As String, pdfFileName As String)
' specify that VintaSoft Imaging .NET SDK should use GDI+ for drawing of 2D graphics
Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault()
' specify that VintaSoft Imaging .NET SDK should use SkiaSharp for drawing of 2D graphics
'Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault()
' create image collection
Using imageCollection As New Vintasoft.Imaging.ImageCollection()
' add DOCX document to collection
imageCollection.Add(docxFileName)
' create pdfEncoder
Using pdfEncoder As New Vintasoft.Imaging.Codecs.Encoders.PdfEncoder(True)
' set PDF/A-1b (ISO 19005-1, Level B conformance)
pdfEncoder.Settings.Conformance = Vintasoft.Imaging.Pdf.PdfDocumentConformance.PdfA_1b
' set CMYK ICC profile, which is used by PDF/A converter as a profile in DefaulRGB color space
pdfEncoder.Settings.PdfADefaultRgbIccProfileFilename = "DefaultRGB.icc"
' set comression for image resources
pdfEncoder.Settings.Compression = Vintasoft.Imaging.Codecs.Encoders.PdfImageCompression.Jpeg
' save images of image collection to PDF/A-1b document using PdfEncoder
imageCollection.SaveSync(pdfFileName, pdfEncoder)
End Using
' dispose images
imageCollection.ClearAndDisposeItems()
End Using
End Sub