英文:
PDFsharp - How to convert a cdata XML node to a PDF document?
问题
问题在于我试图将XML文档中的cdata节点转换为PDF文档,使用PDFsharp。但我得到的只是一个损坏的PDF文档。
这是我的测试代码:
```c#
Console.WriteLine("读取输入文件。");
String xmlFile = File.ReadAllText("input.xml");
XDocument xmlDoc = XDocument.Parse(xmlFile);
XElement node1Element = xmlDoc.Descendants("tekening").FirstOrDefault();
string cdataValue = node1Element.Element("tekening_data").Value;
Console.Write(cdataValue);
Console.WriteLine();
byte[] bytes = Encoding.UTF8.GetBytes(cdataValue);
Console.WriteLine("从输入文件创建PDF。");
using (PdfDocument InputDocument = PdfReader.Open(new MemoryStream(bytes), PdfDocumentOpenMode.Import))
{
Console.WriteLine("保存为PDF文件。");
InputDocument.Save("output.pdf");
}
Console.WriteLine("准备完毕!");
Console.ReadLine();
XML输入:
<tekening>
<tekening_data><![CDATA[%%Title: Platten-Skizze von AB: 20133/061123 Pos.: <0048|00> WB65
%%From: CNCBD
%%Creator: GAV Vers 5.2.21552
%%CreationDate: 2023-04-20 11:14:56
%Driver: PcsPrint 5.0.0
% *** Begin embedded object, PcsPrint 5.0.0 ***
/SaveMark_Embed save def
% save status
%%BeginProlog
%%
%% Standard prolog
%% (c) Copyright PCS Strakeljahn GmbH & Co. KG 2011
%%
/BIND {bind def} bind def /_SNAP {transform round exch round exch itransform} BIND /_DSNAP {dtransform round exch round exch
idtransform} BIND /LINE {_SNAP lineto} BIND /MOVE {_SNAP moveto} BIND /RLINE {_DSNAP rlineto} BIND
.... 省略 ....
08WL6) CENTER GR
% <------------- END APL-Core
/DS {1.0000 mul} BIND GR
% Ende Platten-Skizze von AB: 20133/061123 Pos.: <0048|00> WB65
SaveMark_Embed restore
% *** End embedded object ***]]></tekening_data>
</tekening>
我尝试过修整行尾,并移除了与cdata XML节点一起出现的标题。
650 800 translate 180 rotate
<</Orientation 2>> setpagedevice
50 10 translate .95 .95 scale
<details>
<summary>英文:</summary>
The issue is that I'm trying to convert a cdata node from an XML document to a PDF document via PDFsharp. All I get is a damaged PDF document.
The is my test code:
```c#
Console.WriteLine("Read input file.");
String xmlFile = File.ReadAllText("input.xml");
XDocument xmlDoc = XDocument.Parse(xmlFile);
XElement node1Element = xmlDoc.Descendants("tekening").FirstOrDefault();
string cdataValue = node1Element.Element("tekening_data").Value;
Console.Write(cdataValue);
Console.WriteLine();
byte[] bytes = Encoding.UTF8.GetBytes(cdataValue);
Console.WriteLine("Create PDF from input file.");
using (PdfDocument InputDocument = PdfReader.Open(new MemoryStream(bytes), PdfDocumentOpenMode.Import))
{
Console.WriteLine("Save as PDF file.");
InputDocument.Save("output.pdf");
}
Console.WriteLine("Ready!");
Console.ReadLine();
The XML input:
<tekening>
<tekening_data><![CDATA[%%Title: Platten-Skizze von AB: 20133/061123 Pos.: <0048|00> WB65
%%From: CNCBD
%%Creator: GAV Vers 5.2.21552
%%CreationDate: 2023-04-20 11:14:56
%Driver: PcsPrint 5.0.0
% *** Begin embedded object, PcsPrint 5.0.0 ***
/SaveMark_Embed save def
% save status
%%BeginProlog
%%
%% Standard prolog
%% (c) Copyright PCS Strakeljahn GmbH & Co. KG 2011
%%
/BIND {bind def} bind def /_SNAP {transform round exch round exch itransform} BIND /_DSNAP {dtransform round exch round exch
idtransform} BIND /LINE {_SNAP lineto} BIND /MOVE {_SNAP moveto} BIND /RLINE {_DSNAP rlineto} BIND
.... snip ...
08WL6) CENTER GR
% <------------- END APL-Core
/DS {1.0000 mul} BIND GR
% Ende Platten-Skizze von AB: 20133/061123 Pos.: <0048|00> WB65
SaveMark_Embed restore
% *** End embedded object ***]]></tekening_data>
</tekening>
I have tried to trim line ends, and removed a header that comes in with cdata XML node.
650 800 translate 180 rotate
<</Orientation 2>> setpagedevice
50 10 translate .95 .95 scale
答案1
得分: 1
你需要使用XML阅读器打开你的XML文件。
然后,在内存中创建一个空的PdfDocument,根据需要向文档添加页面,根据需要使用DrawString写入XML中的数据,最后保存PDF文档。
你正在使用的PdfReader
只能读取PDF文件,它不是一个可以将任何文件类型转换为PDF的魔法转换器。
英文:
You have to open your XML with an XML reader.
Then you create an empty PdfDocument in memory, add pages to the document as needed, write the data from the XML using DrawString as needed, and finally save the PDF document.
The PdfReader
you are using can read PDF files only. It is not a magic "convert any file type to PDF" converter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论