Insert an image into a .docx file - c#

I am developing a program that has the feature to dynamically create DocX files. As the title suggests, I am having a problem inserting images into a DocX file. One of the issues, as I see it, is that I am using C# 2.0. (When answering this question, I would like to stress that I do not wish to switch to C# 3.0, so please do not try to persuade me.)
I have taken a look at the MSDN article at http://msdn.microsoft.com/en-us/library/office/bb497430.aspx, but when I converted the C# 3.0 code that MSDN uses to C# 2.0, I get a document that does not open, and it gives me the error: "The file Testing.docx cannot be opened because there are problems with the contents," and "No error detail available."
Here is my code:
ImagePart ip_Part;
WordprocessingDocument wpd_Doc = WordprocessingDocument.Create("C:\\Convert\\Testing.docx", WordprocessingDocumentType.Document);
public Testing()
{
wpd_Doc.AddMainDocumentPart();
wpd_Doc.MainDocumentPart.Document = new Document();
wpd_Doc.MainDocumentPart.Document.Body = new Body();
ip_Part = wpd_Doc.MainDocumentPart.AddImagePart(ImagePartType.Png);
System.IO.FileStream fs_Stream = new System.IO.FileStream("image.png", System.IO.FileMode.Open);
ip_Part.FeedData(fs_Stream);
AddImageToBody("image.png", wpd_Doc.MainDocumentPart.GetIdOfPart(ip_Part));
AppendText("Here is a test bulleted list:");
wpd_Doc.MainDocumentPart.Document.Save();
//Close the document.
wpd_Doc.Close();
}
private void AddImageToBody(string s_ImagePath, string s_RelationshipId)
{
//OpenXmlElement oxe_Element = new Numbering();
Drawing d_Drawing = new Drawing();
DrawWord.Inline i_Inline = new DrawWord.Inline();
DrawWord.Extent e_Extent = new DrawWord.Extent();
e_Extent.Cx = 600075; //63px / 96dpi * 914400
e_Extent.Cy = 600075; //63px / 96dpi * 914400
i_Inline.Extent = e_Extent;
DrawWord.DocProperties dp_Prop = new DrawWord.DocProperties();
//dp_Prop.Id = uint.Parse(System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString());
dp_Prop.Id = 1;
dp_Prop.Name = "Picture 1";
dp_Prop.Description = "An automated image.";
i_Inline.DocProperties = dp_Prop;
DrawWord.NonVisualGraphicFrameDrawingProperties nvgfdp_Prop = new DrawWord.NonVisualGraphicFrameDrawingProperties();
Draw.GraphicFrameLocks gfl_Locks = new Draw.GraphicFrameLocks();
gfl_Locks.NoChangeAspect = true;
nvgfdp_Prop.GraphicFrameLocks = gfl_Locks;
i_Inline.NonVisualGraphicFrameDrawingProperties = nvgfdp_Prop;
Draw.Graphic g_Graphic = new Draw.Graphic();
Draw.GraphicData gd_Data = new DocumentFormat.OpenXml.Drawing.GraphicData();
gd_Data.Uri = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
DrawPic.Picture p_Pic = new DrawPic.Picture();
DrawPic.NonVisualPictureProperties nvpp_Prop = new DrawPic.NonVisualPictureProperties();
DrawPic.NonVisualDrawingProperties nvdp_Prop = new DrawPic.NonVisualDrawingProperties();
nvdp_Prop.Id = uint.Parse(System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString());
//nvdp_Prop.Name = s_ImagePath;
nvdp_Prop.Name = "New_Image.png";
nvpp_Prop.NonVisualDrawingProperties = nvdp_Prop;
DrawPic.NonVisualPictureDrawingProperties nvpdp_Prop = new DrawPic.NonVisualPictureDrawingProperties();
nvpp_Prop.NonVisualPictureDrawingProperties = nvpdp_Prop;
p_Pic.NonVisualPictureProperties = nvpp_Prop;
DrawPic.BlipFill bf_Fill = new DrawPic.BlipFill();
Draw.Blip b_Blip = new Draw.Blip();
Draw.ExtensionList el_List = new Draw.ExtensionList();
Draw.BlipExtension be_Extension = new Draw.BlipExtension();
be_Extension.Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}";
el_List.Append(be_Extension);
b_Blip.Append(el_List);
b_Blip.Embed = s_RelationshipId;
b_Blip.CompressionState = Draw.BlipCompressionValues.Print;
bf_Fill.Blip = b_Blip;
Draw.Stretch s_Stretch = new Draw.Stretch();
Draw.FillRectangle fr_Rect = new Draw.FillRectangle();
s_Stretch.FillRectangle = fr_Rect;
bf_Fill.Append(s_Stretch);
p_Pic.BlipFill = bf_Fill;
DrawPic.ShapeProperties sp_Prop = new DrawPic.ShapeProperties();
Draw.Transform2D t2d_Transform = new Draw.Transform2D();
Draw.Offset o_Offset = new Draw.Offset();
o_Offset.X = 0;
o_Offset.Y = 0;
t2d_Transform.Offset = o_Offset;
Draw.Extents e_Extents = new Draw.Extents();
e_Extents.Cx = 600075; //63px / 96dpi * 914400
e_Extents.Cy = 600075; //63px / 96dpi * 914400
t2d_Transform.Extents = e_Extents;
sp_Prop.Transform2D = t2d_Transform;
Draw.PresetGeometry pg_Geom = new Draw.PresetGeometry();
Draw.AdjustValueList avl_List = new Draw.AdjustValueList();
pg_Geom.AdjustValueList = avl_List;
pg_Geom.Preset = Draw.ShapeTypeValues.Rectangle;
sp_Prop.Append(pg_Geom);
p_Pic.ShapeProperties = sp_Prop;
gd_Data.Append(p_Pic);
g_Graphic.GraphicData = gd_Data;
i_Inline.Graphic = g_Graphic;
d_Drawing.Inline = i_Inline;
//oxe_Element.Append(d_Drawing);
//Run r_Run = new Run(d_Drawing);
wpd_Doc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(d_Drawing)));
}
(The variable names are bad, but this was just a test program, so I did not spend too much time. Also, I have the lines spaced to mimic the Xml format that MSDN had in its example. If I could use the C# 3.0 syntax, I would, but I am using Visual Studio 2005.)

I have found the answer. Surprisingly, I did not find this webpage--because it was some Google pages down with some odd keyword searching--that completely worked: http://blog.stuartwhiteford.com/?p=33. Though it is written with Collection Initializations, I was able to change them to fit C# 2.0 standards.

Related

How to add a first page footer different from default one in NPOI - Docx?

I'm generating a docx file using NPOI 2.5.2 and I stuck with headers/footers for first page.
I'd like to have a first page custom footer and start numbering pages from the second one.
Here is my code for the first page footer:
// First page
doc.Document.body.sectPr = new CT_SectPr();
var footer = new CT_Ftr();
var footerParagraph = footer.AddNewP();
footerParagraph.AddNewR().AddNewT().Value = $"FIRST PAGE CUSTOM FOOTER";
var footerPar = new XWPFParagraph(footerParagraph, doc);
var parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerPar;
var headerFooterPolicy = doc.GetHeaderFooterPolicy();
if (headerFooterPolicy == null)
headerFooterPolicy = doc.CreateHeaderFooterPolicy();
headerFooterPolicy.CreateFooter(XWPFHeaderFooterPolicy.FIRST, parsFooter);
Here is my code for the default footer with page numbering:
// Other pages
footerParagraph = footer.AddNewP();
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
footerParagraph.AddNewR().AddNewInstrText().Value = " PAGE ";
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;
footerPar = new XWPFParagraph(footerParagraph, doc);
parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerPar;
headerFooterPolicy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
With the code above I could not see the first page custom footer but the page numbering in every page. What am I doing wrong?
I found this similar question but I could not find addNewTitlePg method in NPOI.
And is there any appropriate documentation with examples about NPOI?
Here is my code with the first footer working. (using NPOI 2.5.3)
var doc = new XWPFDocument();
using (var sw = File.Create("fileformat.docx"))
{
XWPFParagraph p1 = doc.CreateParagraph();
XWPFParagraph p2 = doc.CreateParagraph();
XWPFRun r1 = p1.CreateRun();
XWPFRun r2 = p2.CreateRun();
r1.SetText("The quick brown fox");
r1.AddBreak(BreakType.PAGE);
r2.SetText("Next page: The quick brown fox");
doc.Document.body.sectPr = new CT_SectPr();
var policy = doc.CreateHeaderFooterPolicy();
var ctSectPr = doc.Document.body.sectPr;
if (ctSectPr.titlePg == null)
{
ctSectPr.titlePg = new CT_OnOff() { val = true };
}
var firstFooter = policy.CreateFooter(ST_HdrFtr.first);
var paragraph = firstFooter.CreateParagraph();
var run = paragraph.CreateRun();
run.SetText("First page footer...");
var defaultFooter = policy.CreateFooter(ST_HdrFtr.#default);
paragraph = defaultFooter.CreateParagraph();
run = paragraph.CreateRun();
paragraph.Alignment = ParagraphAlignment.RIGHT;
paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
paragraph.GetCTP().AddNewR().AddNewInstrText().Value = " PAGE ";
paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;
run = paragraph.CreateRun();
doc.Write(sw);
}

XML reading - Deserialize vs XElement.Load

my question is fairly simple, which method should I use and why for parsing XML files?
Right now I have function for that:
return new EdiFile
{
SPPLR_MAILBOX = xmlDoc.Element("SPPLR_MAILBOX").Value,
MESSAGE_ID = xmlDoc.Element("MESSAGE_ID").Value,
ATTRIBUTE05 = xmlDoc.Element("ATTRIBUTE05").Value,
Levels0 = (from a in xmlDoc.Element("LEVELS0").Elements("LEVEL0")
select new Level0
{
PLT_NUM = a.Element("PLT_NUM").Value,
PLT_LABEL_ID = a.Element("PLT_LABEL_ID").Value,
BOX_QTY = a.Element("BOX_QTY").Value,
PLT_WEIGHT = a.Element("PLT_WEIGTH").Value,
PLT_DIMENSION = a.Element("PLT_DIMENSION").Value,
PLT_CHEM = a.Element("PLT_CHEM").Value,
PLT_NOT_STACK = a.Element("PLT_NOT_STACK").Value,
PLT_NOTE = a.Element("PLT_NOTE").Value,
ATTRIBUTE01 = a.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = a.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = a.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = a.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = a.Element("ATTRIBUTE05").Value,
Levels1 = (from b in a.Element("LEVELS1").Elements("LEVEL1")
select new Level1
{
BOX_NUM = b.Element("BOX_NUM").Value,
BOX_LABEL_ID = b.Element("BOX_LABEL_ID").Value,
Items = (from c in b.Element("ITEMS").Elements("ITEM")
select new Item
{
SPPLR_ITEM = c.Element("SPPLR_ITEM").Value,
CUST_ITEM = c.Element("CUST_ITEM").Value,
ATTRIBUTE01 = c.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = c.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = c.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = c.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = c.Element("ATTRIBUTE05").Value,
Lots = (from d in c.Element("LOTS").Elements("LOT")
select new Lot
{
LOT_NUM = d.Element("LOT_NUM").Value,
LOT_LABEL_ID = d.Element("LOT_LABEL_ID").Value,
LOT_NOTE = d.Element("LOT_NOTE").Value,
LOT_EXP_DATE = d.Element("LOT_EXP_DATE").Value,
QTY = d.Element("QTY").Value,
UOM = d.Element("UOM").Value,
ATTRIBUTE01 = d.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = d.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = d.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = d.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = d.Element("ATTRIBUTE05").Value
}).ToList()
}).ToList()
}).ToList()
}).ToList()
};
But should I use deserialize? I learnt about serialization yesterday.
I cant really find any source explaining my method, vs deserialization.
Can someone get me any insight please?
Your method is more performance and flexie, because you have control on processing xml file. Serialization is using reflection and it is less performance.

programmatically create smartform ektron

I have created the smartform and generated the relevant class using a bat file (using xsd to generate c# class). Then I assigned that created smartform to a particular folder and I created the sample smartforms using the CMS work area.
Is there a way to create a smartform from code behind? I have tried as follows, but it didn't work as expected:
ContentType<root> cData = new ContentType<root>();
cData.SmartForm.EventName = "Conference Event1";
cData.SmartForm.EventDescription = "Test Description";
cData.SmartForm.EventDate = DateTime.Now.AddMonths(2).ToString("yyyy-MM-dd");
ContentTypeManager<root> contentTypeManager = new ContentTypeManager<root>();
contentTypeManager.Add(cData);
I have found the solution. You can achieve it using ContentManager.
ContentManager contentManager = new ContentManager(ApiAccessMode.Admin);
Ektron.Cms.ContentData contentData = new Ektron.Cms.ContentData();
contentData.Title = "title 011";
contentData.Html = "<root><EventName>Change1...</EventName>" +
"<EventDescription>Description Test</EventDescription>" +
"<EventDate>2014-10-30</EventDate>" +
"</root>";
contentData.ContType = 1;
contentData.Comment = "Automatically generated from a script.";
contentData.FolderId = 86; //folder id to save you smart data
contentData.IsPublished = true;
contentData.IsSearchable = true;
contentData.LanguageId = 1033;
contentData.XmlInheritedFrom = 86; //folder id to save you smart data
Ektron.Cms.XmlConfigData xcd = new Ektron.Cms.XmlConfigData();
xcd.Id = 7; //SmartForm ID
contentData.XmlConfiguration = xcd;
contentManager.Add(contentData);

Class generated from complex xsd, trying to serialize gives nullreferenceexception

Been scratching my head for some hours. Googled as well. But can't figure out how to generate the xml properly. Would highly appreciate input that could help me figure this out. I've used xsd.exe earlier together with less complex schemes without any problems.
So I get the error: Object reference not set to an instance of an object.
I have created C# Classes from this xsd-file: http://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd
I created the classes with the Microsoft xsd.exe tool like this: xsd.exe xsd-file /classes
Then I removed double brackets like [][] and replaced with single [], otherwise I cant serialize/deserialize at all.
I actually don't know the correct way to create a xml-file with the class generated from the xsd-document. Here is an example of such a xml-file: https://github.com/mlt/schwinn810/wiki/Sample-.TCX-Files
This is my object that I'm trying to serialize (just an example):
XmlObjects.Tcx20.TrainingCenterDatabase_t tcx = new XmlObjects.Tcx20.TrainingCenterDatabase_t();
XmlObjects.Tcx20.AbstractSource_t abstractSource = new XmlObjects.Tcx20.Application_t();
abstractSource.Name = "TcxCreator";
tcx.Author = abstractSource;
abstractSource = new XmlObjects.Tcx20.Application_t();
XmlObjects.Tcx20.ActivityList_t activityList = new XmlObjects.Tcx20.ActivityList_t();
XmlObjects.Tcx20.Activity_t[] activity = new XmlObjects.Tcx20.Activity_t[1];
XmlObjects.Tcx20.ActivityLap_t[] lap = new ActivityLap_t[1];
XmlObjects.Tcx20.Course_t[] course = new Course_t[1];
XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1];
XmlObjects.Tcx20.Position_t position = new Position_t();
double lat = 10;
double lon = 11;
position.LatitudeDegrees = lat;
position.LongitudeDegrees = lon;
trackPoint[0].Time = DateTime.Now;
trackPoint[0].Position = position;
lap[0].Track = trackPoint;
activity[0].Lap = lap;
activityList.Activity = activity;
tcx.Activities = activityList;
Line trackPoint[0].Time = DateTime.Now; gives the mentioned error. But i think its more related to that im creating the classes/xml wrong compared to how the xsd/xml looks like.
Could someone just point me in the right direction concerning how to build up the xml from the class generated by xsd.exe?
Edit: Thanks YavgenyP! That was it, this code is working:
XmlObjects.Tcx20.TrainingCenterDatabase_t tcx = new XmlObjects.Tcx20.TrainingCenterDatabase_t();
XmlObjects.Tcx20.AbstractSource_t abstractSource = new XmlObjects.Tcx20.Application_t();
abstractSource.Name = "TcxCreator";
tcx.Author = abstractSource;
abstractSource = new XmlObjects.Tcx20.Application_t();
XmlObjects.Tcx20.ActivityList_t activityList = new XmlObjects.Tcx20.ActivityList_t();
XmlObjects.Tcx20.Activity_t[] activity = new XmlObjects.Tcx20.Activity_t[1];
XmlObjects.Tcx20.ActivityLap_t[] lap = new ActivityLap_t[1];
XmlObjects.Tcx20.Course_t[] course = new Course_t[1];
XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1];
XmlObjects.Tcx20.Position_t position = new Position_t();
double lat = 10;
double lon = 11;
position.LatitudeDegrees = lat;
position.LongitudeDegrees = lon;
trackPoint[0] = new Trackpoint_t {Time = DateTime.Now, Position = position};
lap[0] = new ActivityLap_t {Track = trackPoint};
activity[0] = new Activity_t {Lap = lap};
activityList.Activity = activity;
tcx.Activities = activityList;
Line trackPoint[0].Time = DateTime.Now; gives the mentioned error
Look at your code, you initialize the aforementioned array here:
XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1];
but you never initialize the Trackpoint_t ITSELF in the array, which causes this
trackPoint[0].Time = DateTime.Now;
to fail (trackPoint[0] is still a null)

UPS API - LabelLinksIndicator - Object Reference not set to an instance of an object

Below is some code for generating a UPS shipping label. It is based on the .net sample provided in the UPS developer kit. My problem is with the line highlighted in bold below. It throws and error as follows "System.NullReferenceException: Object reference not set to an instance of an object.".
What I know is this LabelLinksIndicator is required if you want the UPS API to return a link to a label. Otherwise it returns details on the shipment but no label. The relevant section of the UPS documentation is at the bottom of my question.
What do I need to do to overcome this error? It is not clear to me from the documentation what value I should be passing for LabelLinksIndicator. I've tried passing a 1, true and and an empty string. Same error in every case. The error message seems to indicate that there is an object I need to instantiate that I am not currently. However, I can't figure out what to instantiate. The available information on the web and on UPS.com is unfortunately sparse.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Foo.Web.UPSWebReference;
namespace Foo.Web.Auth
{
public partial class UPS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
try
{
ShipService shpSvc = new ShipService();
ShipmentRequest shipmentRequest = new ShipmentRequest();
UPSSecurity upss = new UPSSecurity();
UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
upssSvcAccessToken.AccessLicenseNumber = "foo";
upss.ServiceAccessToken = upssSvcAccessToken;
UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
upssUsrNameToken.Username = "foo";
upssUsrNameToken.Password = "foo";
upss.UsernameToken = upssUsrNameToken;
shpSvc.UPSSecurityValue = upss;
RequestType request = new RequestType();
String[] requestOption = { "nonvalidate" };
request.RequestOption = requestOption;
shipmentRequest.Request = request;
ShipmentType shipment = new ShipmentType();
shipment.Description = "Ship webservice example";
ReturnServiceType rtn = new ReturnServiceType();
rtn.Code = "8";
rtn.Description = "Description";
ShipperType shipper = new ShipperType();
shipper.ShipperNumber = "foo";
PaymentInfoType paymentInfo = new PaymentInfoType();
ShipmentChargeType shpmentCharge = new ShipmentChargeType();
BillShipperType billShipper = new BillShipperType();
billShipper.AccountNumber = "foo";
shpmentCharge.BillShipper = billShipper;
shpmentCharge.Type = "01";
ShipmentChargeType[] shpmentChargeArray = { shpmentCharge };
paymentInfo.ShipmentCharge = shpmentChargeArray;
shipment.PaymentInformation = paymentInfo;
foo.Web.UPSWebReference.ShipAddressType shipperAddress = new foo.Web.UPSWebReference.ShipAddressType();
String[] addressLine = { "301 166th Street" };
shipperAddress.AddressLine = addressLine;
shipperAddress.City = "Jersey City";
shipperAddress.PostalCode = "07310";
shipperAddress.StateProvinceCode = "NJ";
shipperAddress.CountryCode = "US";
shipperAddress.AddressLine = addressLine;
shipper.Address = shipperAddress;
shipper.Name = "ABC Associates";
shipper.AttentionName = "ABC Associates";
ShipPhoneType shipperPhone = new ShipPhoneType();
shipperPhone.Number = "1234567890";
shipper.Phone = shipperPhone;
shipment.Shipper = shipper;
ShipFromType shipFrom = new ShipFromType();
foo.Web.UPSWebReference.ShipAddressType shipFromAddress = new foo.Web.UPSWebReference.ShipAddressType();
String[] shipFromAddressLine = { "100 82nd Street" };
shipFromAddress.AddressLine = addressLine;
shipFromAddress.City = "New York";
shipFromAddress.PostalCode = "10024";
shipFromAddress.StateProvinceCode = "NY";
shipFromAddress.CountryCode = "US";
shipFrom.Address = shipFromAddress;
shipFrom.AttentionName = "Mr.ABC";
shipFrom.Name = "ABC Associates";
shipment.ShipFrom = shipFrom;
ShipToType shipTo = new ShipToType();
ShipToAddressType shipToAddress = new ShipToAddressType();
String[] addressLine1 = { "Some Street" };
shipToAddress.AddressLine = addressLine1;
shipToAddress.City = "Roswell";
shipToAddress.PostalCode = "30076";
shipToAddress.StateProvinceCode = "GA";
shipToAddress.CountryCode = "US";
shipTo.Address = shipToAddress;
shipTo.Address.ResidentialAddressIndicator = "1"; //dan
shipTo.AttentionName = "DEF";
shipTo.Name = "DEF Associates";
ShipPhoneType shipToPhone = new ShipPhoneType();
shipToPhone.Number = "1234567890";
shipTo.Phone = shipToPhone;
shipment.ShipTo = shipTo;
ServiceType service = new ServiceType();
service.Code = "03";
service.Description = "Ground";
shipment.Service = service;
PackageType package = new PackageType();
package.Description = "Deliver to Warehouse";
PackageWeightType packageWeight = new PackageWeightType();
packageWeight.Weight = "10";
ShipUnitOfMeasurementType uom = new ShipUnitOfMeasurementType();
uom.Code = "LBS";
packageWeight.UnitOfMeasurement = uom;
package.PackageWeight = packageWeight;
PackagingType packType = new PackagingType();
packType.Code = "02";
package.Packaging = packType;
PackageType[] pkgArray = { package };
shipment.Package = pkgArray;
LabelSpecificationType labelSpec = new LabelSpecificationType();
LabelImageFormatType labelImageFormat = new LabelImageFormatType();
labelImageFormat.Code = "GIF";
labelSpec.LabelImageFormat = labelImageFormat;
string userAgent = Request.UserAgent;
labelSpec.HTTPUserAgent = userAgent;
shipmentRequest.LabelSpecification = labelSpec;
**shipmentRequest.Shipment.ShipmentServiceOptions.LabelDelivery.LabelLinksIndicator = "1";**
shipmentRequest.Shipment = shipment;
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
ShipmentResponse shipmentResponse = shpSvc.ProcessShipment(shipmentRequest);
Response.Redirect(shipmentResponse.ShipmentResults.LabelURL.ToString());
I figured out what the problem was. I needed first to new-up an instance of
ShipmentTypeShipmentServiceOptions shipServOpt = new ShipmentTypeServiceOptions();
and
LabelDeliveryType labelDel = new LabelDeliveryType();
Then set the LabelLinksIndicator element
labeldel.LabelLinksIndicator = "";
Then assign the options to my shipment instance
shipment.ShipmentServiceOptions = shipServOpt;
Note, ShipmentServiceOptionsType is NOT the same as ShipmentTypeServiceOptions. This tripped me up for a while.
Dear UPS, if you are reading this please consider improving upon your documentation and providing a more complete set of code examples.
shipmentRequest.Shipment appears to be null, since you aren't assigning anything to it until the next line. So, you can't do shipmentRequest.Shipment.ANYTHING. Switch them around, so that you have
shipmentRequest.Shipment = shipment;
shipmentRequest.Shipment.ShipmentServiceOptions.LabelDelivery.LabelLinksIndicator = "1";
The element needs to get passed as <LabelLinksIndicator/> in the XML. In your sample you'd be sending it as <LabelLinksIndicator>1</LabelLinksIndicator> which is invalid.

Categories

Resources