C#将OFD文件转换成PDF文件
时间: 2023-10-24 11:07:26 浏览: 237
你可以使用第三方库iTextSharp来将OFD文件转换成PDF文件。首先需要将OFD文件解压缩,然后将解压缩后的XML文件转换成PDF文件。
以下是一个示例代码:
```csharp
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Xml;
public static void ConvertOFDToPDF(string ofdFilePath, string pdfFilePath)
{
// 解压缩OFD文件
string tempFolderPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempFolderPath);
ZipFile.ExtractToDirectory(ofdFilePath, tempFolderPath);
// 获取OFD文档的页面尺寸
string documentXmlPath = Path.Combine(tempFolderPath, "Documents", "Document.xml");
XmlDocument documentXml = new XmlDocument();
documentXml.Load(documentXmlPath);
XmlNamespaceManager nsManager = new XmlNamespaceManager(documentXml.NameTable);
nsManager.AddNamespace("ofd", "http://www.ofdspec.org");
XmlNode pageAreaNode = documentXml.SelectSingleNode("/ofd:Document/ofd:Pages/ofd:PageArea", nsManager);
float pageWidth = float.Parse(pageAreaNode.Attributes["PageWidth"].Value);
float pageHeight = float.Parse(pageAreaNode.Attributes["PageHeight"].Value);
// 创建PDF文档
using (FileStream pdfFileStream = new FileStream(pdfFilePath, FileMode.Create))
{
using (Document pdfDocument = new Document(new Rectangle(pageWidth, pageHeight)))
{
using (PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, pdfFileStream))
{
pdfDocument.Open();
// 遍历OFD文档的页面,将每个页面转换成PDF页面
XmlNodeList pageNodes = documentXml.SelectNodes("/ofd:Document/ofd:Pages/ofd:Page", nsManager);
foreach (XmlNode pageNode in pageNodes)
{
string pageFileNodeValue = pageNode.Attributes["BaseLoc"].Value;
string pageFilePath = Path.Combine(tempFolderPath, pageFileNodeValue);
using (FileStream pageFileStream = new FileStream(pageFilePath, FileMode.Open))
{
using (MemoryStream pageMemoryStream = new MemoryStream())
{
pageFileStream.CopyTo(pageMemoryStream);
byte[] pageBytes = pageMemoryStream.ToArray();
// 将OFD页面转换成PDF页面
using (MemoryStream pdfMemoryStream = new MemoryStream())
{
using (Document pageDocument = new Document())
{
using (PdfWriter pdfPageWriter = PdfWriter.GetInstance(pageDocument, pdfMemoryStream))
{
pageDocument.Open();
using (MemoryStream pageXmlStream = new MemoryStream(pageBytes))
{
XmlDocument pageXml = new XmlDocument();
pageXml.Load(pageXmlStream);
XmlNode pageContentNode = pageXml.SelectSingleNode("/ofd:Page/ofd:Content", nsManager);
string pageContent = pageContentNode.InnerXml;
using (MemoryStream pageContentStream = new MemoryStream())
{
using (StreamWriter pageContentWriter = new StreamWriter(pageContentStream))
{
pageContentWriter.Write(pageContent);
pageContentWriter.Flush();
pageContentStream.Position = 0;
XmlParserContext xmlParserContext = new XmlParserContext(null, nsManager, null, XmlSpace.None);
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlReader xmlReader = XmlReader.Create(pageContentStream, xmlReaderSettings, xmlParserContext))
{
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
string elementName = xmlReader.Name;
if (elementName == "ofd:TextObject")
{
string fontId = xmlReader.GetAttribute("Font");
string fontSizeString = xmlReader.GetAttribute("Size");
float fontSize = float.Parse(fontSizeString);
string fontFamily = null;
XmlNode fontNode = documentXml.SelectSingleNode($"/ofd:Document/ofd:Fonts/ofd:Font[@ID='{fontId}']", nsManager);
if (fontNode != null)
{
fontFamily = fontNode.Attributes["FontName"].Value;
}
if (string.IsNullOrEmpty(fontFamily))
{
fontFamily = "宋体";
}
BaseFont baseFont = BaseFont.CreateFont($"{fontFamily},Bold", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont, fontSize);
string text = xmlReader.ReadElementContentAsString();
pdfDocument.Add(new Paragraph(text, font));
}
else if (elementName == "ofd:PathObject")
{
// 处理OFD路径对象
}
else if (elementName == "ofd:ImageObject")
{
// 处理OFD图像对象
}
}
}
}
}
}
}
pageDocument.Close();
}
}
byte[] pdfBytes = pdfMemoryStream.ToArray();
PdfReader pdfReader = new PdfReader(pdfBytes);
pdfWriter.DirectContent.AddTemplate(pdfWriter.GetImportedPage(pdfReader, 1), 0, 0);
}
}
}
}
pdfDocument.Close();
}
}
}
Directory.Delete(tempFolderPath, true);
}
```
请注意,这段代码只是一个示例,可能需要根据你的具体情况进行修改和调整。
阅读全文