PDFsharp – 如何将一个CDATA XML节点转换为PDF文档?

huangapple go评论70阅读模式
英文:

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&#39;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(&quot;Read input file.&quot;);

String xmlFile = File.ReadAllText(&quot;input.xml&quot;);
XDocument xmlDoc = XDocument.Parse(xmlFile);
XElement node1Element = xmlDoc.Descendants(&quot;tekening&quot;).FirstOrDefault();
string cdataValue = node1Element.Element(&quot;tekening_data&quot;).Value;

Console.Write(cdataValue);
Console.WriteLine();

byte[] bytes = Encoding.UTF8.GetBytes(cdataValue);

Console.WriteLine(&quot;Create PDF from input file.&quot;);
using (PdfDocument InputDocument = PdfReader.Open(new MemoryStream(bytes), PdfDocumentOpenMode.Import))
{
	Console.WriteLine(&quot;Save as PDF file.&quot;);
	InputDocument.Save(&quot;output.pdf&quot;);
}

Console.WriteLine(&quot;Ready!&quot;);
Console.ReadLine();

The XML input:

                            &lt;tekening&gt;
				&lt;tekening_data&gt;&lt;![CDATA[%%Title: Platten-Skizze von AB: 20133/061123 Pos.: &lt;0048|00&gt; 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 &amp; 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
% &lt;------------- END APL-Core
/DS {1.0000 mul} BIND GR
% Ende Platten-Skizze von AB: 20133/061123 Pos.: &lt;0048|00&gt; WB65
SaveMark_Embed restore
% *** End embedded object ***]]&gt;&lt;/tekening_data&gt;
			&lt;/tekening&gt;

I have tried to trim line ends, and removed a header that comes in with cdata XML node.

650 800 translate 180 rotate
&lt;&lt;/Orientation 2&gt;&gt; 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.

huangapple
  • 本文由 发表于 2023年6月8日 19:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定