#c# #wpf #printing #system.drawing #system.printing
#c# #wpf #печать #system.drawing #система.печать
Вопрос:
Я вручную преобразовал PaperKind в PageMediaSizeName следующим образом:
static PageMediaSizeName ConvertPaperKindToPageMediaSize(PaperKind paperKind)
{
switch (paperKind)
{
case PaperKind.Custom:
return PageMediaSizeName.Unknown;
case PaperKind.Letter:
return PageMediaSizeName.NorthAmericaLetter;
case PaperKind.Legal:
return PageMediaSizeName.NorthAmericaLegal;
case PaperKind.A4:
return PageMediaSizeName.ISOA4;
case PaperKind.CSheet:
return PageMediaSizeName.NorthAmericaCSheet;
case PaperKind.DSheet:
return PageMediaSizeName.NorthAmericaDSheet;
case PaperKind.ESheet:
return PageMediaSizeName.NorthAmericaESheet;
case PaperKind.LetterSmall:
throw new NotImplementedException();
case PaperKind.Tabloid:
return PageMediaSizeName.NorthAmericaTabloid;
case PaperKind.Ledger:
throw new NotImplementedException();
case PaperKind.Statement:
return PageMediaSizeName.NorthAmericaStatement;
case PaperKind.Executive:
return PageMediaSizeName.NorthAmericaExecutive;
case PaperKind.A3:
return PageMediaSizeName.ISOA3;
case PaperKind.A4Small:
throw new NotImplementedException();
case PaperKind.A5:
return PageMediaSizeName.ISOA5;
case PaperKind.B4:
return PageMediaSizeName.ISOB4;
case PaperKind.B5:
throw new NotImplementedException();
case PaperKind.Folio:
return PageMediaSizeName.OtherMetricFolio;
case PaperKind.Quarto:
return PageMediaSizeName.NorthAmericaQuarto;
case PaperKind.Standard10x14:
throw new NotImplementedException();
case PaperKind.Standard11x17:
throw new NotImplementedException();
case PaperKind.Note:
return PageMediaSizeName.NorthAmericaNote;
case PaperKind.Number9Envelope:
return PageMediaSizeName.NorthAmericaNumber9Envelope;
case PaperKind.Number10Envelope:
return PageMediaSizeName.NorthAmericaNumber10Envelope;
case PaperKind.Number11Envelope:
return PageMediaSizeName.NorthAmericaNumber11Envelope;
case PaperKind.Number12Envelope:
return PageMediaSizeName.NorthAmericaNumber12Envelope;
case PaperKind.Number14Envelope:
return PageMediaSizeName.NorthAmericaNumber14Envelope;
case PaperKind.DLEnvelope:
return PageMediaSizeName.ISODLEnvelope;
case PaperKind.C5Envelope:
return PageMediaSizeName.ISOC5Envelope;
case PaperKind.C3Envelope:
return PageMediaSizeName.ISOC3Envelope;
case PaperKind.C4Envelope:
return PageMediaSizeName.ISOC4Envelope;
case PaperKind.C6Envelope:
return PageMediaSizeName.ISOC6Envelope;
case PaperKind.C65Envelope:
return PageMediaSizeName.ISOC6C5Envelope;
case PaperKind.B4Envelope:
return PageMediaSizeName.ISOB4Envelope;
case PaperKind.B5Envelope:
return PageMediaSizeName.ISOB5Envelope;
case PaperKind.B6Envelope:
throw new NotImplementedException();
case PaperKind.ItalyEnvelope:
return PageMediaSizeName.OtherMetricItalianEnvelope;
case PaperKind.MonarchEnvelope:
return PageMediaSizeName.NorthAmericaMonarchEnvelope;
case PaperKind.PersonalEnvelope:
return PageMediaSizeName.NorthAmericaPersonalEnvelope;
case PaperKind.USStandardFanfold:
throw new NotImplementedException();
case PaperKind.GermanStandardFanfold:
return PageMediaSizeName.NorthAmericaGermanStandardFanfold;
case PaperKind.GermanLegalFanfold:
return PageMediaSizeName.NorthAmericaGermanLegalFanfold;
case PaperKind.IsoB4:
return PageMediaSizeName.ISOB4;
case PaperKind.JapanesePostcard:
return PageMediaSizeName.JapanHagakiPostcard;
case PaperKind.Standard9x11:
throw new NotImplementedException();
case PaperKind.Standard10x11:
throw new NotImplementedException();
case PaperKind.Standard15x11:
throw new NotImplementedException();
case PaperKind.InviteEnvelope:
return PageMediaSizeName.OtherMetricInviteEnvelope;
case PaperKind.LetterExtra:
return PageMediaSizeName.NorthAmericaLetterExtra;
case PaperKind.LegalExtra:
return PageMediaSizeName.NorthAmericaLegalExtra;
case PaperKind.TabloidExtra:
return PageMediaSizeName.NorthAmericaTabloidExtra;
case PaperKind.A4Extra:
return PageMediaSizeName.ISOA4Extra;
case PaperKind.LetterTransverse:
throw new NotImplementedException();
case PaperKind.A4Transverse:
throw new NotImplementedException();
case PaperKind.LetterExtraTransverse:
throw new NotImplementedException();
case PaperKind.APlus:
throw new NotImplementedException();
case PaperKind.BPlus:
throw new NotImplementedException();
case PaperKind.LetterPlus:
return PageMediaSizeName.NorthAmericaLetterPlus;
case PaperKind.A4Plus:
return PageMediaSizeName.OtherMetricA4Plus;
case PaperKind.A5Transverse:
throw new NotImplementedException();
case PaperKind.B5Transverse:
throw new NotImplementedException();
case PaperKind.A3Extra:
return PageMediaSizeName.ISOA3Extra;
case PaperKind.A5Extra:
return PageMediaSizeName.ISOA5Extra;
case PaperKind.B5Extra:
return PageMediaSizeName.ISOB5Extra;
case PaperKind.A2:
return PageMediaSizeName.ISOA2;
case PaperKind.A3Transverse:
throw new NotImplementedException();
case PaperKind.A3ExtraTransverse:
throw new NotImplementedException();
case PaperKind.JapaneseDoublePostcard:
return PageMediaSizeName.JapanDoubleHagakiPostcard;
case PaperKind.A6:
return PageMediaSizeName.ISOA6;
case PaperKind.JapaneseEnvelopeKakuNumber2:
return PageMediaSizeName.JapanKaku2Envelope;
case PaperKind.JapaneseEnvelopeKakuNumber3:
return PageMediaSizeName.JapanKaku3Envelope;
case PaperKind.JapaneseEnvelopeChouNumber3:
return PageMediaSizeName.JapanChou3Envelope;
case PaperKind.JapaneseEnvelopeChouNumber4:
return PageMediaSizeName.JapanChou4Envelope;
case PaperKind.LetterRotated:
return PageMediaSizeName.NorthAmericaLetterRotated;
case PaperKind.A3Rotated:
return PageMediaSizeName.ISOA3Rotated;
case PaperKind.A4Rotated:
return PageMediaSizeName.ISOA4Rotated;
case PaperKind.A5Rotated:
return PageMediaSizeName.ISOA5Rotated;
case PaperKind.B4JisRotated:
return PageMediaSizeName.JISB4Rotated;
case PaperKind.B5JisRotated:
return PageMediaSizeName.JISB5Rotated;
case PaperKind.JapanesePostcardRotated:
return PageMediaSizeName.JapanHagakiPostcardRotated;
case PaperKind.JapaneseDoublePostcardRotated:
return PageMediaSizeName.JapanHagakiPostcardRotated;
case PaperKind.A6Rotated:
return PageMediaSizeName.ISOA6Rotated;
case PaperKind.JapaneseEnvelopeKakuNumber2Rotated:
return PageMediaSizeName.JapanKaku2EnvelopeRotated;
case PaperKind.JapaneseEnvelopeKakuNumber3Rotated:
return PageMediaSizeName.JapanKaku3EnvelopeRotated;
case PaperKind.JapaneseEnvelopeChouNumber3Rotated:
return PageMediaSizeName.JapanChou3EnvelopeRotated;
case PaperKind.JapaneseEnvelopeChouNumber4Rotated:
return PageMediaSizeName.JapanChou4EnvelopeRotated;
case PaperKind.B6Jis:
return PageMediaSizeName.JISB6;
case PaperKind.B6JisRotated:
return PageMediaSizeName.JISB6Rotated;
case PaperKind.Standard12x11:
throw new NotImplementedException();
case PaperKind.JapaneseEnvelopeYouNumber4:
return PageMediaSizeName.JapanYou4Envelope;
case PaperKind.JapaneseEnvelopeYouNumber4Rotated:
return PageMediaSizeName.JapanYou4EnvelopeRotated;
case PaperKind.Prc16K:
return PageMediaSizeName.PRC16K;
case PaperKind.Prc32K:
return PageMediaSizeName.PRC32K;
case PaperKind.Prc32KBig:
return PageMediaSizeName.PRC32KBig;
case PaperKind.PrcEnvelopeNumber1:
return PageMediaSizeName.PRC1Envelope;
case PaperKind.PrcEnvelopeNumber2:
return PageMediaSizeName.PRC2Envelope;
case PaperKind.PrcEnvelopeNumber3:
return PageMediaSizeName.PRC3Envelope;
case PaperKind.PrcEnvelopeNumber4:
return PageMediaSizeName.PRC4Envelope;
case PaperKind.PrcEnvelopeNumber5:
return PageMediaSizeName.PRC5Envelope;
case PaperKind.PrcEnvelopeNumber6:
return PageMediaSizeName.PRC6Envelope;
case PaperKind.PrcEnvelopeNumber7:
return PageMediaSizeName.PRC7Envelope;
case PaperKind.PrcEnvelopeNumber8:
return PageMediaSizeName.PRC8Envelope;
case PaperKind.PrcEnvelopeNumber9:
return PageMediaSizeName.PRC9Envelope;
case PaperKind.PrcEnvelopeNumber10:
return PageMediaSizeName.PRC10Envelope;
case PaperKind.Prc16KRotated:
return PageMediaSizeName.PRC16KRotated;
case PaperKind.Prc32KRotated:
return PageMediaSizeName.PRC32KRotated;
case PaperKind.Prc32KBigRotated:
throw new NotImplementedException();
case PaperKind.PrcEnvelopeNumber1Rotated:
return PageMediaSizeName.PRC1EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber2Rotated:
return PageMediaSizeName.PRC2EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber3Rotated:
return PageMediaSizeName.PRC3EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber4Rotated:
return PageMediaSizeName.PRC4EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber5Rotated:
return PageMediaSizeName.PRC5EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber6Rotated:
return PageMediaSizeName.PRC6EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber7Rotated:
return PageMediaSizeName.PRC7EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber8Rotated:
return PageMediaSizeName.PRC8EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber9Rotated:
return PageMediaSizeName.PRC9EnvelopeRotated;
case PaperKind.PrcEnvelopeNumber10Rotated:
return PageMediaSizeName.PRC10EnvelopeRotated;
default:
throw new ArgumentOutOfRangeException("paperKind");
}
}
Но есть ли у кого-нибудь идея получше, как это сделать? Я использую старые диалоговые окна печати Forms для получения / установки настроек принтера, но хочу распечатать документ с фиксированным кодом WPF и должен знать носитель для PrintTicket.
Ответ №1:
Спасибо за сопоставление!
Для удобства других, вот двунаправленное отображение в виде пары словарей, обернутых в методы расширения:
public static class UiExtensions
{
private static readonly Dictionary<PaperKind, PageMediaSizeName> _kindToSizeName;
private static readonly Dictionary<PageMediaSizeName, PaperKind> _sizeNameToKind =
new Dictionary<PageMediaSizeName, PaperKind>();
static UiExtensions()
{
_kindToSizeName = new Dictionary<PaperKind, PageMediaSizeName>
{
{PaperKind.Custom, PageMediaSizeName.Unknown},
{PaperKind.Letter, PageMediaSizeName.NorthAmericaLetter},
{PaperKind.LetterSmall, PageMediaSizeName.NorthAmericaLetter},
{PaperKind.Tabloid, PageMediaSizeName.NorthAmericaTabloid},
{PaperKind.Ledger, PageMediaSizeName.NorthAmericaTabloid},
{PaperKind.Legal, PageMediaSizeName.NorthAmericaLegal},
{PaperKind.Statement, PageMediaSizeName.NorthAmericaStatement},
{PaperKind.Executive, PageMediaSizeName.NorthAmericaExecutive},
{PaperKind.A3, PageMediaSizeName.ISOA3},
{PaperKind.A4, PageMediaSizeName.ISOA4},
{PaperKind.A4Small, PageMediaSizeName.ISOA4},
{PaperKind.A5, PageMediaSizeName.ISOA5},
{PaperKind.B4, PageMediaSizeName.ISOB4},
{PaperKind.B5, PageMediaSizeName.ISOB5Extra},
{PaperKind.Folio, PageMediaSizeName.OtherMetricFolio},
{PaperKind.Quarto, PageMediaSizeName.NorthAmericaQuarto},
{PaperKind.Standard10x14, PageMediaSizeName.NorthAmerica10x14},
{PaperKind.Standard11x17, PageMediaSizeName.NorthAmericaTabloid},
{PaperKind.Note, PageMediaSizeName.NorthAmericaNote},
{PaperKind.Number9Envelope, PageMediaSizeName.NorthAmericaNumber9Envelope},
{PaperKind.Number10Envelope, PageMediaSizeName.NorthAmericaNumber10Envelope},
{PaperKind.Number11Envelope, PageMediaSizeName.NorthAmericaNumber11Envelope},
{PaperKind.Number12Envelope, PageMediaSizeName.NorthAmericaNumber12Envelope},
{PaperKind.Number14Envelope, PageMediaSizeName.NorthAmericaNumber14Envelope},
{PaperKind.CSheet, PageMediaSizeName.NorthAmericaCSheet},
{PaperKind.DSheet, PageMediaSizeName.NorthAmericaDSheet},
{PaperKind.ESheet, PageMediaSizeName.NorthAmericaESheet},
{PaperKind.DLEnvelope, PageMediaSizeName.ISODLEnvelope},
{PaperKind.C5Envelope, PageMediaSizeName.ISOC5Envelope},
{PaperKind.C3Envelope, PageMediaSizeName.ISOC3Envelope},
{PaperKind.C4Envelope, PageMediaSizeName.ISOC4Envelope},
{PaperKind.C6Envelope, PageMediaSizeName.ISOC6Envelope},
{PaperKind.C65Envelope, PageMediaSizeName.ISOC6C5Envelope},
{PaperKind.B4Envelope, PageMediaSizeName.ISOB4Envelope},
{PaperKind.B5Envelope, PageMediaSizeName.ISOB5Envelope},
{PaperKind.B6Envelope, PageMediaSizeName.Unknown},
{PaperKind.ItalyEnvelope, PageMediaSizeName.OtherMetricItalianEnvelope},
{PaperKind.MonarchEnvelope, PageMediaSizeName.NorthAmericaMonarchEnvelope},
{PaperKind.PersonalEnvelope, PageMediaSizeName.NorthAmericaPersonalEnvelope},
{PaperKind.USStandardFanfold, PageMediaSizeName.Unknown},
{PaperKind.GermanStandardFanfold, PageMediaSizeName.NorthAmericaGermanStandardFanfold},
{PaperKind.GermanLegalFanfold, PageMediaSizeName.NorthAmericaGermanLegalFanfold},
{PaperKind.IsoB4, PageMediaSizeName.ISOB4},
{PaperKind.JapanesePostcard, PageMediaSizeName.JapanHagakiPostcard},
{PaperKind.Standard9x11, PageMediaSizeName.Unknown},
{PaperKind.Standard10x11, PageMediaSizeName.Unknown},
{PaperKind.Standard15x11, PageMediaSizeName.Unknown},
{PaperKind.InviteEnvelope, PageMediaSizeName.OtherMetricInviteEnvelope},
{PaperKind.LetterExtra, PageMediaSizeName.NorthAmericaLetterExtra},
{PaperKind.LegalExtra, PageMediaSizeName.NorthAmericaLegalExtra},
{PaperKind.TabloidExtra, PageMediaSizeName.NorthAmericaTabloidExtra},
{PaperKind.A4Extra, PageMediaSizeName.ISOA4Extra},
{PaperKind.LetterTransverse, PageMediaSizeName.Unknown},
{PaperKind.A4Transverse, PageMediaSizeName.Unknown},
{PaperKind.LetterExtraTransverse, PageMediaSizeName.Unknown},
{PaperKind.APlus, PageMediaSizeName.Unknown},
{PaperKind.BPlus, PageMediaSizeName.Unknown},
{PaperKind.LetterPlus, PageMediaSizeName.NorthAmericaLetterPlus},
{PaperKind.A4Plus, PageMediaSizeName.OtherMetricA4Plus},
{PaperKind.A5Transverse, PageMediaSizeName.Unknown},
{PaperKind.B5Transverse, PageMediaSizeName.Unknown},
{PaperKind.A3Extra, PageMediaSizeName.ISOA3Extra},
{PaperKind.A5Extra, PageMediaSizeName.ISOA5Extra},
{PaperKind.B5Extra, PageMediaSizeName.ISOB5Extra},
{PaperKind.A2, PageMediaSizeName.ISOA2},
{PaperKind.A3Transverse, PageMediaSizeName.Unknown},
{PaperKind.A3ExtraTransverse, PageMediaSizeName.Unknown},
{PaperKind.JapaneseDoublePostcard, PageMediaSizeName.JapanDoubleHagakiPostcard},
{PaperKind.A6, PageMediaSizeName.ISOA6},
{PaperKind.JapaneseEnvelopeKakuNumber2, PageMediaSizeName.JapanKaku2Envelope},
{PaperKind.JapaneseEnvelopeKakuNumber3, PageMediaSizeName.JapanKaku3Envelope},
{PaperKind.JapaneseEnvelopeChouNumber3, PageMediaSizeName.JapanChou3Envelope},
{PaperKind.JapaneseEnvelopeChouNumber4, PageMediaSizeName.JapanChou4Envelope},
{PaperKind.LetterRotated, PageMediaSizeName.NorthAmericaLetterRotated},
{PaperKind.A3Rotated, PageMediaSizeName.ISOA3Rotated},
{PaperKind.A4Rotated, PageMediaSizeName.ISOA4Rotated},
{PaperKind.A5Rotated, PageMediaSizeName.ISOA5Rotated},
{PaperKind.B4JisRotated, PageMediaSizeName.JISB4Rotated},
{PaperKind.B5JisRotated, PageMediaSizeName.JISB5Rotated},
{PaperKind.JapanesePostcardRotated, PageMediaSizeName.JapanHagakiPostcardRotated},
{PaperKind.JapaneseDoublePostcardRotated, PageMediaSizeName.JapanHagakiPostcardRotated},
{PaperKind.A6Rotated, PageMediaSizeName.ISOA6Rotated},
{PaperKind.JapaneseEnvelopeKakuNumber2Rotated, PageMediaSizeName.JapanKaku2EnvelopeRotated},
{PaperKind.JapaneseEnvelopeKakuNumber3Rotated, PageMediaSizeName.JapanKaku3EnvelopeRotated},
{PaperKind.JapaneseEnvelopeChouNumber3Rotated, PageMediaSizeName.JapanChou3EnvelopeRotated},
{PaperKind.JapaneseEnvelopeChouNumber4Rotated, PageMediaSizeName.JapanChou4EnvelopeRotated},
{PaperKind.B6Jis, PageMediaSizeName.JISB6},
{PaperKind.B6JisRotated, PageMediaSizeName.JISB6Rotated},
{PaperKind.Standard12x11, PageMediaSizeName.Unknown},
{PaperKind.JapaneseEnvelopeYouNumber4, PageMediaSizeName.JapanYou4Envelope},
{PaperKind.JapaneseEnvelopeYouNumber4Rotated, PageMediaSizeName.JapanYou4EnvelopeRotated},
{PaperKind.Prc16K, PageMediaSizeName.PRC16K},
{PaperKind.Prc32K, PageMediaSizeName.PRC32K},
{PaperKind.Prc32KBig, PageMediaSizeName.PRC32KBig},
{PaperKind.PrcEnvelopeNumber1, PageMediaSizeName.PRC1Envelope},
{PaperKind.PrcEnvelopeNumber2, PageMediaSizeName.PRC2Envelope},
{PaperKind.PrcEnvelopeNumber3, PageMediaSizeName.PRC3Envelope},
{PaperKind.PrcEnvelopeNumber4, PageMediaSizeName.PRC4Envelope},
{PaperKind.PrcEnvelopeNumber5, PageMediaSizeName.PRC5Envelope},
{PaperKind.PrcEnvelopeNumber6, PageMediaSizeName.PRC6Envelope},
{PaperKind.PrcEnvelopeNumber7, PageMediaSizeName.PRC7Envelope},
{PaperKind.PrcEnvelopeNumber8, PageMediaSizeName.PRC8Envelope},
{PaperKind.PrcEnvelopeNumber9, PageMediaSizeName.PRC9Envelope},
{PaperKind.PrcEnvelopeNumber10, PageMediaSizeName.PRC10Envelope},
{PaperKind.Prc16KRotated, PageMediaSizeName.PRC16KRotated},
{PaperKind.Prc32KRotated, PageMediaSizeName.PRC32KRotated},
{PaperKind.Prc32KBigRotated, PageMediaSizeName.Unknown},
{PaperKind.PrcEnvelopeNumber1Rotated, PageMediaSizeName.PRC1EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber2Rotated, PageMediaSizeName.PRC2EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber3Rotated, PageMediaSizeName.PRC3EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber4Rotated, PageMediaSizeName.PRC4EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber5Rotated, PageMediaSizeName.PRC5EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber6Rotated, PageMediaSizeName.PRC6EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber7Rotated, PageMediaSizeName.PRC7EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber8Rotated, PageMediaSizeName.PRC8EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber9Rotated, PageMediaSizeName.PRC9EnvelopeRotated},
{PaperKind.PrcEnvelopeNumber10Rotated, PageMediaSizeName.PRC10EnvelopeRotated},
};
foreach (var pair in _kindToSizeName)
{
_sizeNameToKind[pair.Value] = pair.Key;
}
_sizeNameToKind[PageMediaSizeName.Unknown] = PaperKind.Custom;
}
public static PageMediaSizeName ToPageMediaSizeName(this PaperKind kind)
{
PageMediaSizeName sizeName;
return _kindToSizeName.TryGetValue(kind, out sizeName) ? sizeName : PageMediaSizeName.Unknown;
}
public static PaperKind ToPaperKind(this PageMediaSizeName sizeName)
{
PaperKind kind;
return _sizeNameToKind.TryGetValue(sizeName, out kind) ? kind : PaperKind.Custom;
}
}
Использование:
var sizeName = PaperKind.A4.ToPageMediaSizeName();
Console.WriteLine(sizeName); // ISOA4
Ответ №2:
Насколько я знаю, нет существующего convert. Я бы реализовал преобразование в более читаемой версии:
static Dictionary<PaperKind, PageMediaSizeName> PaperKindMap = new Dictionary<PaperKind, PageMediaSizeName>()
{
{PaperKind.Custom, PageMediaSizeName.Unknown},
{PaperKind.Letter, PageMediaSizeName.NorthAmericaLetter},
//...
};
static PageMediaSizeName ConvertPaperKindToPageMediaSize(PaperKind paperKind)
{
if (PaperKindMap.ContainsKey(paperKind)) {
return PaperKindMap[paperKind];
} else {
throw new NotImplementedException();
}
}