Link to home
Start Free TrialLog in
Avatar of pclarke7
pclarke7

asked on

Convert XML to PDF in C#

Hello,
I have a Dymo .label file which is XML code and contains the code required to produce a label on a Dymo Labelwriter 450 printer. I need to convert this XML file to PDF so that I can send it to www.PrintNode.com via an API so that I can print it Clientside. PrintNode will only except a PDF file. In my Server application I have copied the XML code out of the .label file and inserted into into a PDF file using iTextSharp as shown below.
The outcome is that I get a valid PDF file but when I open it , it contains the XML code from the .label rather than the formatted Label which I was expecting to see.  I am obviously missing some part of the conversion process but don't know what it is. Needless to can that if I send this file to printNode it will just print the XML text rather than the label. Any suggestions or pointers or examples would be welcome.

regards
Pat

C# Code where I insert XML into PDF document                          
                           // Create new pdf file
                            try
                            {
                                fs = new FileStream(filePath1, FileMode.Create);
                            }
                            catch (Exception ex)
                            {
                               
                            }

                            //To create a PDF document, create an instance of the class Document and pass the page size and the page margins to the constructor.Then use that object and the file stream to create the PdfWriter instance enabling us to output text and other elements to the PDF file.

[b]C# Code to insert XML into PDF document[/b]
                            // Create an instance of the document class which represents the PDF document itself.
                            Document document = new Document(PageSize.A4, 25, 25, 30, 30);
                            // Create an instance to the PDF file by creating an instance of the PDF 
                            // Writer class using the document and the filestrem in the constructor.
                            PdfWriter writer = PdfWriter.GetInstance(document, fs);

                            // Add meta information to the document
                            document.AddAuthor("Tios");
                            document.AddCreator("XML to PDF using iTextSharp");
                            document.AddKeywords("Tios");
                            document.AddSubject("Document subject - Describing the steps creating a PDF document");
                            document.AddTitle("The document title - PDF creation using iTextSharp");

                            //Before we can write to the document, we need to open it.
                            document.Open();
                            // Add a simple and wellknown phrase to the document in a flow layout manner
                            document.Add(new Paragraph(xmlString);
                            // Close the document & writer and open File Handle
                            document.Close();
                            writer.Close();
                            fs.Close();

Open in new window



XML Code in .label file
<?xml version="1.0" encoding="utf-8"?>
<DieCutLabel Version="8.0" Units="twips">
	<PaperOrientation>Landscape</PaperOrientation>
	<Id>Shipping</Id>
	<IsOutlined>false</IsOutlined>
	<PaperName>30323 Shipping</PaperName>
	<DrawCommands>
		<RoundRectangle X="0" Y="0" Width="3060" Height="5715" Rx="270" Ry="270" />
	</DrawCommands>
	<ObjectInfo>
		<ShapeObject Stroke="SolidLine">
			<Name>SHAPE</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<ShapeType>Rectangle</ShapeType>
			<LineWidth>15</LineWidth>
			<LineAlignment>Center</LineAlignment>
			<FillColor Alpha="0" Red="255" Green="255" Blue="255" />
		</ShapeObject>
		<Bounds X="319" Y="150" Width="660.000000000001" Height="660" />
	</ObjectInfo>
	<ObjectInfo>
		<TextObject>
			<Name>TEXT</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<HorizontalAlignment>Left</HorizontalAlignment>
			<VerticalAlignment>Middle</VerticalAlignment>
			<TextFitMode>ShrinkToFit</TextFitMode>
			<UseFullFontHeight>True</UseFullFontHeight>
			<Verticalized>False</Verticalized>
			<StyledText>
				<Element>
					<String xml:space="preserve">Item:</String>
					<Attributes>
						<Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" />
						<ForeColor Alpha="255" Red="0" Green="0" Blue="0" HueScale="100" />
					</Attributes>
				</Element>
			</StyledText>
		</TextObject>
		<Bounds X="367" Y="225" Width="588" Height="468" />
	</ObjectInfo>
	<ObjectInfo>
		<ShapeObject Stroke="SolidLine">
			<Name>SHAPE_1</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<ShapeType>Rectangle</ShapeType>
			<LineWidth>15</LineWidth>
			<LineAlignment>Center</LineAlignment>
			<FillColor Alpha="0" Red="255" Green="255" Blue="255" />
		</ShapeObject>
		<Bounds X="998.000000000003" Y="154.000000000001" Width="4428" Height="648" />
	</ObjectInfo>
	<ObjectInfo>
		<TextObject>
			<Name>TEXT_1</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<HorizontalAlignment>Left</HorizontalAlignment>
			<VerticalAlignment>Middle</VerticalAlignment>
			<TextFitMode>ShrinkToFit</TextFitMode>
			<UseFullFontHeight>True</UseFullFontHeight>
			<Verticalized>False</Verticalized>
			<StyledText>
				<Element>
					<String xml:space="preserve">07009039</String>
					<Attributes>
						<Font Family="Arial" Size="22" Bold="True" Italic="False" Underline="False" Strikeout="False" />
						<ForeColor Alpha="255" Red="0" Green="0" Blue="0" HueScale="100" />
					</Attributes>
				</Element>
			</StyledText>
		</TextObject>
		<Bounds X="1082" Y="218.999999999999" Width="3756" Height="528" />
	</ObjectInfo>
	<ObjectInfo>
		<ShapeObject Stroke="SolidLine">
			<Name>SHAPE_2</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<ShapeType>Rectangle</ShapeType>
			<LineWidth>15</LineWidth>
			<LineAlignment>Center</LineAlignment>
			<FillColor Alpha="0" Red="255" Green="255" Blue="255" />
		</ShapeObject>
		<Bounds X="307" Y="1626" Width="5160" Height="515.999999999999" />
	</ObjectInfo>
	<ObjectInfo>
		<TextObject>
			<Name>TEXT_2</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<HorizontalAlignment>Left</HorizontalAlignment>
			<VerticalAlignment>Middle</VerticalAlignment>
			<TextFitMode>ShrinkToFit</TextFitMode>
			<UseFullFontHeight>True</UseFullFontHeight>
			<Verticalized>False</Verticalized>
			<StyledText>
				<Element>
					<String xml:space="preserve">TEFLON TUBING OD12MM-ID 10MM </String>
					<Attributes>
						<Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" />
						<ForeColor Alpha="255" Red="0" Green="0" Blue="0" HueScale="100" />
					</Attributes>
				</Element>
			</StyledText>
		</TextObject>
		<Bounds X="369" Y="1665" Width="4836" Height="432" />
	</ObjectInfo>
	<ObjectInfo>
		<ShapeObject Stroke="SolidLine">
			<Name>SHAPE_3</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<ShapeType>Rectangle</ShapeType>
			<LineWidth>15</LineWidth>
			<LineAlignment>Center</LineAlignment>
			<FillColor Alpha="0" Red="255" Green="255" Blue="255" />
		</ShapeObject>
		<Bounds X="307" Y="2172" Width="696" Height="720" />
	</ObjectInfo>
	<ObjectInfo>
		<ShapeObject Stroke="SolidLine">
			<Name>SHAPE_4</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<ShapeType>Rectangle</ShapeType>
			<LineWidth>15</LineWidth>
			<LineAlignment>Center</LineAlignment>
			<FillColor Alpha="0" Red="255" Green="255" Blue="255" />
		</ShapeObject>
		<Bounds X="1010" Y="2166" Width="4452" Height="720" />
	</ObjectInfo>
	<ObjectInfo>
		<BarcodeObject>
			<Name>BARCODE</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>True</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<Text>PRODUCTBC</Text>
			<Type>Code39</Type>
			<Size>Medium</Size>
			<TextPosition>None</TextPosition>
			<TextFont Family="Arial" Size="8" Bold="False" Italic="False" Underline="False" Strikeout="False" />
			<CheckSumFont Family="Arial" Size="8" Bold="False" Italic="False" Underline="False" Strikeout="False" />
			<TextEmbedding>None</TextEmbedding>
			<ECLevel>0</ECLevel>
			<HorizontalAlignment>Left</HorizontalAlignment>
			<QuietZonesPadding Left="0" Top="0" Right="0" Bottom="0" />
		</BarcodeObject>
		<Bounds X="1032" Y="864" Width="4320" Height="696" />
	</ObjectInfo>
	<ObjectInfo>
		<TextObject>
			<Name>TEXT_3</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<HorizontalAlignment>Left</HorizontalAlignment>
			<VerticalAlignment>Middle</VerticalAlignment>
			<TextFitMode>ShrinkToFit</TextFitMode>
			<UseFullFontHeight>True</UseFullFontHeight>
			<Verticalized>False</Verticalized>
			<StyledText>
				<Element>
					<String xml:space="preserve">Lot:</String>
					<Attributes>
						<Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" />
						<ForeColor Alpha="255" Red="0" Green="0" Blue="0" HueScale="100" />
					</Attributes>
				</Element>
			</StyledText>
		</TextObject>
		<Bounds X="367" Y="2226" Width="588" Height="588" />
	</ObjectInfo>
	<ObjectInfo>
		<TextObject>
			<Name>TEXT_4</Name>
			<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />
			<BackColor Alpha="0" Red="255" Green="255" Blue="255" />
			<LinkedObjectName />
			<Rotation>Rotation0</Rotation>
			<IsMirrored>False</IsMirrored>
			<IsVariable>False</IsVariable>
			<GroupID>-1</GroupID>
			<IsOutlined>False</IsOutlined>
			<HorizontalAlignment>Left</HorizontalAlignment>
			<VerticalAlignment>Middle</VerticalAlignment>
			<TextFitMode>ShrinkToFit</TextFitMode>
			<UseFullFontHeight>True</UseFullFontHeight>
			<Verticalized>False</Verticalized>
			<StyledText>
				<Element>
					<String xml:space="preserve">* This product is not Lot Controlled</String>
					<Attributes>
						<Font Family="Arial" Size="8" Bold="False" Italic="True" Underline="False" Strikeout="False" />
						<ForeColor Alpha="255" Red="0" Green="0" Blue="0" HueScale="100" />
					</Attributes>
				</Element>
			</StyledText>
		</TextObject>
		<Bounds X="1075" Y="2229" Width="4308" Height="600" />
	</ObjectInfo>
</DieCutLabel>

Open in new window


Label I expected to see in PDF document
User generated image
Avatar of ste5an
ste5an
Flag of Germany image

Ask PrintNode. The explicitly list Dymo. As Dymos are not regular printers, there must be a catch.
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As well as the .xml file (which just contains structured data), you need something to describe how to interpret this information.

So you need something like a DTD (a document type definition), or schema, to accompany the xml.

The DTD provides instructions about the structure of the associated XML document; you probably need to invoke something using XSLT to marry the two together.
I have a vague recollection of using something like this a decade or so ago, but my memory fails me, sorry.

Look at https://www.sitepoint.com/really-good-introduction-xml/ to give you some background.
Hi Pat,
The Code39 bar code in your XML code in the .label file is hard-coded as PRODUCTBC (<Text>PRODUCTBC</Text>). Is it always going to be that or could the bar code contain other values? I know nothing about DYMO label makers, but I was expecting the bar code to contain either the item number (such as 07009039) or the description (such as TEFLON TUBING OD12MM-ID 10MM) and was surprised to see PRODUCTBC as its value. Thanks, Joe
Avatar of pclarke7
pclarke7

ASKER

Thanks Joe,
I just haven't implemented the replacement process yet. PRODUCTBC will be replaced with the value of the product. Need to try get the labels printing first then I'll look at the content

regards
Pat
> the value of the product

But is that the item number (such as 07009039) or the item description (such as TEFLON TUBING OD12MM-ID 10MM) or some other "value"?
Hi Joe,
I just put an example of a label up to show the format of the XML. It is not a live label. It is not the content of the table that this question is concerned about. What I am trying to find out is whether this label can be copied to PDF format. I need it in PDF format to use printNode to print it out Clientside.
regards
Pat
Hi Pat,
Yes, I get all that. I was trying to avoid a long explanation of why I'm asking (until later), but here you go. The thing is, pcelba and DansDadUK are both spot-on — you need to understand XML and marry it to a PDF form. To do that in a "proper" fashion, you have to understand and use XSLT. I have numerous clients who don't have a clue how XML/XSLT works, but they know how to build a PDF form with Acrobat and know how to use Notepad to look at a text file. So I wrote a program, called CreatePDFfromXML, that allows you to assign an XML element, such as <ItemNumber>, to a PDF form field, such as ItemNumber. The user doesn't have to know a lot about XML — just enough to open the XML file in Notepad (or Word or WordPad or any text editor) and look for the element names that are used in the PDF form. If the XML is somewhat complex, such as having the same element names in multiple branches, this is not the program to use. But lots of folks are receiving relatively simple XML files with a single occurrence of elements, such as FirstName, LastName, Phone, Email, etc. CreatePDFfromXML will find those elements, extract the values (such as John from <FirstName>John</FirstName>), and create a PDF file with those values in it by matching up the values to the PDF form that the user created with Acrobat (or Nuance's Power PDF or Tracker's PDF-XChange Editor or any software that can create PDF forms). Under the covers, CreatePDFfromXML uses name-value pairs in FDF files and then does a form-fill of the PDF form with the contents of the FDF file.

When I first read your question, it seemed to me that your DYMO label is a good candidate for CreatePDFfromXML, as the XML has just four elements: ItemNumber, Barcode, Description, and Lot. However, the barcode is tricky, because the values in the name-value pairs of the FDF file must be text. So I was thinking that if the value of the barcode is contained in another field, such as ItemNumber, then I could use a Code39 bar code generator (which I already have — also have Code128 and QR Code generators, if needed) to create the bar code image and then merge it with the rest of the PDF form, including, of course, the name-value pairs from the FDF file.

Just doing some brainstorming on it. :)  Regards, Joe
I didn't managed to find a way to convert my label to PDF so that I could pass it server side to "PrintNode" for printing to a local clientside printer. However Pcelba's comments did put me on the track of looking at Dymo SDK and particularly JavaScript support. I discovered that Dymo have a service running on device where their software in installed and an API which caters for listing and writing to a printer. By running this API in javascript on the browser (which runs clientside) I was able to read the xml contents of the label and pass this to the javascript function which printed silently to the locally attached printer. Not a generic solution for any printer but works well for Dymo. See java script function below.

function printDymoLabel2(xmlString) {
    dymo.label.framework.getPrintersAsync().then(function(xmlstring){
        dymo.label.framework.trace = 1; //true
        dymo.label.framework.init; 
        var printers = dymo.label.framework.getPrinters();
        if (printers.length == 0)
            throw "No DYMO printers are installed. Install DYMO printers.";

        // Successful result, printers variable has a list of all supported by the DYMO Label Framework
      
        // retrieve first printer
        var printerName = "";
        for (var i = 0; i < printers.length; ++i) {
            var printer = printers[i];
            if (printer.printerType == "LabelWriterPrinter") {
                printerName = printer.name;
                break;
            }
        }
        

        //label.setObjectText("BARCODE", '000220200');
        var label = dymo.label.framework.openLabelXml(xmlString);
        label.print(printerName); // This is the NAME of the printer which i found 
   
    }
  )}

Open in new window



regards
Pat
Thanks to all for help and comments
Good to hear you were successful!
You're welcome, Pat. Happy to help. Regards, Joe
Try using ZedPDF.com
It helped me a lot!

Best regards

Addison Ervis