PDFExportForm.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. using FastReport.Controls;
  2. using FastReport.Export;
  3. using FastReport.Export.Pdf;
  4. using FastReport.Utils;
  5. using System;
  6. using System.Windows.Forms;
  7. namespace FastReport.Forms
  8. {
  9. /// <summary>
  10. /// Form for <see cref="PDFExport"/>.
  11. /// For internal use only.
  12. /// </summary>
  13. public partial class PDFExportForm : BaseExportForm
  14. {
  15. #region Methods
  16. /// <inheritdoc/>
  17. public override void Init(ExportBase export)
  18. {
  19. base.Init(export);
  20. PDFExport pdfExport = Export as PDFExport;
  21. // Options
  22. cbPdfStandard.SelectedIndex = (int)pdfExport.PdfCompliance;
  23. cbEmbeddedFonts.Checked = pdfExport.EmbeddingFonts;
  24. cbBackground.Checked = pdfExport.Background;
  25. cbTextInCurves.Checked = pdfExport.TextInCurves;
  26. cbColorSpace.SelectedIndex = (int)pdfExport.ColorSpace;
  27. cbOriginalResolution.Checked = pdfExport.ImagesOriginalResolution;
  28. cbPrintOptimized.Checked = pdfExport.PrintOptimized;
  29. cbJpegCompression.Checked = pdfExport.JpegCompression;
  30. nudJpegQuality.Value = pdfExport.JpegQuality;
  31. cbAcroForm.Checked = pdfExport.InteractiveForms;
  32. // Document Information
  33. tbTitle.Text = pdfExport.Title;
  34. tbAuthor.Text = pdfExport.Author;
  35. tbSubject.Text = pdfExport.Subject;
  36. tbKeywords.Text = pdfExport.Keywords;
  37. tbCreator.Text = pdfExport.Creator;
  38. tbProducer.Text = pdfExport.Producer;
  39. // Security
  40. tbOwnerPassword.Text = pdfExport.OwnerPassword;
  41. tbUserPassword.Text = pdfExport.UserPassword;
  42. cbPrintTheDocument.Checked = pdfExport.AllowPrint;
  43. cbModifyTheDocument.Checked = pdfExport.AllowModify;
  44. cbCopyOfTextAndGraphics.Checked = pdfExport.AllowCopy;
  45. cbAnnotations.Checked = pdfExport.AllowAnnotate;
  46. // Viewer
  47. cbPrintDialog.Checked = pdfExport.ShowPrintDialog;
  48. cbHideToolbar.Checked = pdfExport.HideToolbar;
  49. cbHideMenubar.Checked = pdfExport.HideMenubar;
  50. cbHideUI.Checked = pdfExport.HideWindowUI;
  51. cbFitWindow.Checked = pdfExport.FitWindow;
  52. cbCenterWindow.Checked = pdfExport.CenterWindow;
  53. cbPrintScaling.Checked = pdfExport.PrintScaling;
  54. cbOutline.Checked = pdfExport.Outline;
  55. cbbZoom.SelectedIndex = (int)pdfExport.DefaultZoom;
  56. //curves
  57. switch (pdfExport.GradientQuality)
  58. {
  59. case PDFExport.GradientQualityEnum.Image:
  60. rbGradientImage.Select();
  61. break;
  62. case PDFExport.GradientQualityEnum.Low:
  63. rbGradientLow.Select();
  64. break;
  65. case PDFExport.GradientQualityEnum.Medium:
  66. rbGradientMedium.Select();
  67. break;
  68. case PDFExport.GradientQualityEnum.High:
  69. rbGradientHigh.Select();
  70. break;
  71. }
  72. SelectIndexByValue(cbGradientInterpolationPoints, ((int)pdfExport.GradientInterpolationPoints).ToString());
  73. switch (pdfExport.CurvesInterpolation)
  74. {
  75. case PDFExport.CurvesInterpolationEnum.Curves:
  76. cbCurvesInterpolation.SelectedIndex = 0;
  77. break;
  78. default:
  79. SelectIndexByValue(cbCurvesInterpolation, ((int)pdfExport.CurvesInterpolation).ToString());
  80. break;
  81. }
  82. cbSvgAsPicture.Checked = pdfExport.SvgAsPicture;
  83. switch (pdfExport.CurvesInterpolationText)
  84. {
  85. case PDFExport.CurvesInterpolationEnum.Curves:
  86. cbCurvesInterpolationText.SelectedIndex = 0;
  87. break;
  88. default:
  89. SelectIndexByValue(cbCurvesInterpolationText, ((int)pdfExport.CurvesInterpolationText).ToString());
  90. break;
  91. }
  92. cbSignPdf.Checked = pdfExport.IsDigitalSignEnable;
  93. UpdateTextBox(tbSignLocation, pdfExport.DigitalSignLocation);
  94. UpdateTextBox(tbSignReason, pdfExport.DigitalSignReason);
  95. UpdateTextBox(tbSignContactInfo, pdfExport.DigitalSignContactInfo);
  96. //UpdateCertificate(pdfExport.DigitalSignCertificate);
  97. UpdateTextBox(tbCertificatePath, pdfExport.DigitalSignCertificatePath);
  98. cbSaveCertificatePassword.Checked = pdfExport.SaveDigitalSignCertificatePassword;
  99. }
  100. private void UpdateTextBox(TextBox tb, string text)
  101. {
  102. if (!String.IsNullOrEmpty(text))
  103. tb.Text = text;
  104. else tb.Text = "";
  105. }
  106. private void UpdateTextBox(TextBoxButton tb, string text)
  107. {
  108. if (!String.IsNullOrEmpty(text))
  109. tb.Text = text;
  110. else tb.Text = "";
  111. }
  112. private void SelectIndexByValue(ComboBox comboBox, string value)
  113. {
  114. for (int i = 0; i < comboBox.Items.Count; i++)
  115. if (comboBox.Items[i].ToString() == value)
  116. {
  117. comboBox.SelectedIndex = i;
  118. return;
  119. }
  120. comboBox.SelectedIndex = 0;
  121. }
  122. /// <inheritdoc/>
  123. protected override void Done()
  124. {
  125. base.Done();
  126. PDFExport pdfExport = Export as PDFExport;
  127. // Options
  128. pdfExport.PdfCompliance = (PDFExport.PdfStandard)cbPdfStandard.SelectedIndex;
  129. pdfExport.EmbeddingFonts = cbEmbeddedFonts.Checked;
  130. pdfExport.Background = cbBackground.Checked;
  131. pdfExport.TextInCurves = cbTextInCurves.Checked;
  132. pdfExport.ColorSpace = (PDFExport.PdfColorSpace)cbColorSpace.SelectedIndex;
  133. pdfExport.ImagesOriginalResolution = cbOriginalResolution.Checked;
  134. pdfExport.PrintOptimized = cbPrintOptimized.Checked;
  135. pdfExport.JpegCompression = cbJpegCompression.Checked;
  136. pdfExport.JpegQuality = (int)nudJpegQuality.Value;
  137. pdfExport.InteractiveForms = cbAcroForm.Checked;
  138. // Document Information
  139. pdfExport.Title = tbTitle.Text;
  140. pdfExport.Author = tbAuthor.Text;
  141. pdfExport.Subject = tbSubject.Text;
  142. pdfExport.Keywords = tbKeywords.Text;
  143. pdfExport.Creator = tbCreator.Text;
  144. pdfExport.Producer = tbProducer.Text;
  145. // Security
  146. pdfExport.OwnerPassword = tbOwnerPassword.Text;
  147. pdfExport.UserPassword = tbUserPassword.Text;
  148. pdfExport.AllowPrint = cbPrintTheDocument.Checked;
  149. pdfExport.AllowModify = cbModifyTheDocument.Checked;
  150. pdfExport.AllowCopy = cbCopyOfTextAndGraphics.Checked;
  151. pdfExport.AllowAnnotate = cbAnnotations.Checked;
  152. // Viewer
  153. pdfExport.ShowPrintDialog = cbPrintDialog.Checked;
  154. pdfExport.HideToolbar = cbHideToolbar.Checked;
  155. pdfExport.HideMenubar = cbHideMenubar.Checked;
  156. pdfExport.HideWindowUI = cbHideUI.Checked;
  157. pdfExport.FitWindow = cbFitWindow.Checked;
  158. pdfExport.CenterWindow = cbCenterWindow.Checked;
  159. pdfExport.PrintScaling = cbPrintScaling.Checked;
  160. pdfExport.Outline = cbOutline.Checked;
  161. pdfExport.DefaultZoom = (PDFExport.MagnificationFactor)cbbZoom.SelectedIndex;
  162. //curves
  163. if (rbGradientImage.Checked)
  164. pdfExport.GradientQuality = PDFExport.GradientQualityEnum.Image;
  165. else if (rbGradientHigh.Checked)
  166. pdfExport.GradientQuality = PDFExport.GradientQualityEnum.High;
  167. else if (rbGradientMedium.Checked)
  168. pdfExport.GradientQuality = PDFExport.GradientQualityEnum.Medium;
  169. else pdfExport.GradientQuality = PDFExport.GradientQualityEnum.Low;
  170. try { pdfExport.GradientInterpolationPoints = (PDFExport.GradientInterpolationPointsEnum)Int32.Parse(cbGradientInterpolationPoints.Items[cbGradientInterpolationPoints.SelectedIndex].ToString()); }
  171. catch { pdfExport.GradientInterpolationPoints = PDFExport.GradientInterpolationPointsEnum.P128; }
  172. if (cbCurvesInterpolation.SelectedIndex == 0)
  173. pdfExport.CurvesInterpolation = PDFExport.CurvesInterpolationEnum.Curves;
  174. else
  175. try { pdfExport.CurvesInterpolation = (PDFExport.CurvesInterpolationEnum)Int32.Parse(cbCurvesInterpolation.Items[cbCurvesInterpolation.SelectedIndex].ToString()); }
  176. catch { pdfExport.CurvesInterpolation = PDFExport.CurvesInterpolationEnum.Curves; }
  177. pdfExport.SvgAsPicture = cbSvgAsPicture.Checked;
  178. if (cbCurvesInterpolationText.SelectedIndex == 0)
  179. pdfExport.CurvesInterpolationText = PDFExport.CurvesInterpolationEnum.Curves;
  180. else
  181. try { pdfExport.CurvesInterpolationText = (PDFExport.CurvesInterpolationEnum)Int32.Parse(cbCurvesInterpolationText.Items[cbCurvesInterpolationText.SelectedIndex].ToString()); }
  182. catch { pdfExport.CurvesInterpolationText = PDFExport.CurvesInterpolationEnum.Curves; }
  183. pdfExport.IsDigitalSignEnable = cbSignPdf.Checked;
  184. pdfExport.SaveDigitalSignCertificatePassword = cbSaveCertificatePassword.Checked;
  185. pdfExport.DigitalSignLocation = tbSignLocation.Text;
  186. pdfExport.DigitalSignReason = tbSignReason.Text;
  187. pdfExport.DigitalSignContactInfo = tbSignContactInfo.Text;
  188. if (tbCertificatePassword.PasswordChar != '\0')
  189. {
  190. pdfExport.DigitalSignCertificatePassword = tbCertificatePassword.Text;
  191. }
  192. pdfExport.DigitalSignCertificatePath = tbCertificatePath.Text;
  193. }
  194. /// <inheritdoc/>
  195. public override void Localize()
  196. {
  197. base.Localize();
  198. MyRes res = new MyRes("Export,Pdf");
  199. // Main
  200. Text = res.Get("");
  201. panPages.Text = res.Get("Export");
  202. // Options
  203. pageControlOptions.Text = Res.Get("Export,Misc,Options");
  204. gbOptions.Text = Res.Get("Export,Misc,Options");
  205. lblCompliance.Text = res.Get("PDFCompliance");
  206. cbEmbeddedFonts.Text = res.Get("EmbeddedFonts");
  207. cbBackground.Text = res.Get("Background");
  208. cbTextInCurves.Text = res.Get("TextInCurves");
  209. gbImages.Text = res.Get("Images");
  210. lblColorSpace.Text = res.Get("ColorSpace");
  211. cbOriginalResolution.Text = res.Get("OriginalResolution");
  212. cbPrintOptimized.Text = res.Get("PrintOptimized");
  213. cbJpegCompression.Text = res.Get("JpegCompression");
  214. lblJpegQuality.Text = res.Get("JpegQuality");
  215. cbAcroForm.Text = res.Get("InteractiveForms");
  216. // Document Information
  217. pageControlInformation.Text = res.Get("Information");
  218. gbDocumentInfo.Text = res.Get("DocumentInformation");
  219. lbTitle.Text = res.Get("Title");
  220. lbAuthor.Text = res.Get("Author");
  221. lbSubject.Text = res.Get("Subject");
  222. lbKeywords.Text = res.Get("Keywords");
  223. lbCreator.Text = res.Get("Creator");
  224. lbProducer.Text = res.Get("Producer");
  225. // Security
  226. pageControlSecurity.Text = res.Get("Security");
  227. gbAuth.Text = res.Get("Authentification");
  228. lbOwnerPassword.Text = res.Get("OwnerPassword");
  229. lbUserPassword.Text = res.Get("UserPassword");
  230. gbPermissions.Text = res.Get("Permissions");
  231. cbPrintTheDocument.Text = res.Get("PrintTheDocument");
  232. cbModifyTheDocument.Text = res.Get("ModifyTheDocument");
  233. cbCopyOfTextAndGraphics.Text = res.Get("CopyOfTextAndGraphics");
  234. cbAnnotations.Text = res.Get("AddOrModifyTextAnnotations");
  235. // Viewer
  236. pageControlViewer.Text = res.Get("Viewer");
  237. gbViewerPrfs.Text = res.Get("ViewerPreferences");
  238. cbPrintDialog.Text = res.Get("ShowPrintDialog");
  239. cbHideToolbar.Text = res.Get("HideToolbar");
  240. cbHideMenubar.Text = res.Get("HideMenubar");
  241. cbHideUI.Text = res.Get("HideWindowUserInterface");
  242. cbFitWindow.Text = res.Get("FitWindow");
  243. cbCenterWindow.Text = res.Get("CenterWindow");
  244. cbPrintScaling.Text = res.Get("PrintScaling");
  245. cbOutline.Text = res.Get("Outline");
  246. lblInitialZoom.Text = res.Get("InitialZoom");
  247. cbbZoom.Items[0] = res.Get("ActualSize");
  248. cbbZoom.Items[1] = res.Get("FitPage");
  249. cbbZoom.Items[2] = res.Get("FitWidth");
  250. cbbZoom.Items[3] = res.Get("Default");
  251. // Vector graphics
  252. res = new MyRes("Export,Pdf,VectorGraphics");
  253. pageControlVector.Text = res.Get("");
  254. gbGradientQuality.Text = res.Get("GradientQuality");
  255. rbGradientImage.Text = res.Get("ExportAsImage");
  256. rbGradientLow.Text = res.Get("LowQuality");
  257. rbGradientMedium.Text = res.Get("MediumQuality");
  258. rbGradientHigh.Text = res.Get("HighQuality");
  259. lbGradientInterpolationPoints.Text = res.Get("GradientInterpolation");
  260. gbCurves.Text = res.Get("Curves");
  261. lbCurvesInterpolation.Text = res.Get("CurvesInterpolation");
  262. lbCurvesInterpolationText.Text = res.Get("TextInterpolation");
  263. cbCurvesInterpolation.Items[0] = res.Get("Disabled");
  264. cbCurvesInterpolationText.Items[0] = res.Get("Disabled");
  265. cbSvgAsPicture.Text = res.Get("SvgAsPicture");
  266. // Digital signature
  267. res = new MyRes("Export,Pdf,DigitalSignature");
  268. pageDigitalSignature.Text = res.Get("");
  269. gbSignInformation.Text = res.Get("SignInformation");
  270. cbSignPdf.Text = res.Get("SignDocument");
  271. lbSignLocation.Text = res.Get("Location");
  272. lbSignReason.Text = res.Get("Reason");
  273. lbSignContactInfo.Text = res.Get("ContactInfo");
  274. gbCertificate.Text = res.Get("CertificateInformation");
  275. lbCertificatePath.Text = res.Get("CertificatePath");
  276. lbCertificatePassword.Text = res.Get("CertificatePassword");
  277. cbSaveCertificatePassword.Text = res.Get("CertificateSavePassword");
  278. lbSaveCertificatePasswordWarning.Text = res.Get("CertificateSavePasswordWarning");
  279. tbCertificatePassword.PasswordChar = '\0';
  280. tbCertificatePassword.Text = res.Get("ClickToChangeThePassword");
  281. }
  282. /// <inheritdoc/>
  283. public override void UpdateDpiDependencies()
  284. {
  285. base.UpdateDpiDependencies();
  286. tbCertificatePath.Image = GetImage(1);
  287. }
  288. #endregion
  289. #region Constructors
  290. /// <summary>
  291. /// Initializes a new instance of the <see cref="PDFExportForm"/> class.
  292. /// </summary>
  293. public PDFExportForm()
  294. {
  295. InitializeComponent();
  296. cbPdfStandard.SelectedIndexChanged += optionChanged;
  297. cbEmbeddedFonts.CheckedChanged += optionChanged;
  298. cbTextInCurves.CheckedChanged += optionChanged;
  299. cbColorSpace.SelectedIndexChanged += optionChanged;
  300. cbOriginalResolution.CheckedChanged += optionChanged;
  301. cbJpegCompression.CheckedChanged += optionChanged;
  302. cbAcroForm.CheckedChanged += optionChanged;
  303. #if DOTNET_4
  304. cbSvgAsPicture.Enabled = true;
  305. #else
  306. cbSvgAsPicture.Enabled = false;
  307. #endif
  308. tbCertificatePassword.GotFocus += TbPassword_GotFocus;
  309. // init
  310. optionChanged(null, null);
  311. }
  312. private void TbPassword_GotFocus(object sender, EventArgs e)
  313. {
  314. if (tbCertificatePassword.PasswordChar == '\0')
  315. {
  316. tbCertificatePassword.Text = "";
  317. tbCertificatePassword.PasswordChar = '*';
  318. }
  319. }
  320. #endregion
  321. #region Events
  322. private void optionChanged(object sender, EventArgs e)
  323. {
  324. bool noPdfStandard = false;
  325. if (cbAcroForm.Checked)
  326. {
  327. if (cbPdfStandard.SelectedIndex != 0)
  328. cbPdfStandard.SelectedIndex = 0;
  329. cbPdfStandard.Enabled = false;
  330. }
  331. else cbPdfStandard.Enabled = true;
  332. switch (cbPdfStandard.SelectedIndex)
  333. {
  334. case 1: // PDF/A-1a
  335. case 2: // PDF/A-2a
  336. case 3: // PDF/A-2b
  337. case 4: // PDF/A-2u
  338. case 5: // PDF/A-3a
  339. case 6: // PDF/A-3b
  340. pageControlSecurity.Enabled = false;
  341. tbUserPassword.Text = "";
  342. tbOwnerPassword.Text = "";
  343. cbEmbeddedFonts.Checked = true;
  344. cbEmbeddedFonts.Enabled = false;
  345. cbTextInCurves.Checked = false;
  346. cbTextInCurves.Enabled = false;
  347. cbAcroForm.Enabled = false;
  348. break;
  349. case 7: // PDF/X-3
  350. case 8: // PDF/X-4
  351. pageControlSecurity.Enabled = false;
  352. tbUserPassword.Text = "";
  353. tbOwnerPassword.Text = "";
  354. cbEmbeddedFonts.Checked = true;
  355. cbEmbeddedFonts.Enabled = false;
  356. cbTextInCurves.Enabled = true;
  357. cbAcroForm.Enabled = false;
  358. break;
  359. case 0: // PDF 1.5
  360. default:
  361. pageControlSecurity.Enabled = true;
  362. cbEmbeddedFonts.Enabled = true;
  363. cbTextInCurves.Enabled = true;
  364. noPdfStandard = true;
  365. cbAcroForm.Enabled = true;
  366. break;
  367. }
  368. if (noPdfStandard)
  369. {
  370. // cbEmbeddedFonts
  371. cbEmbeddedFonts.Enabled = !cbTextInCurves.Checked;// && !cbAcroForm.Checked;
  372. //if (!cbEmbeddedFonts.Enabled)
  373. // cbEmbeddedFonts.Checked = false;
  374. // end
  375. // cbTextInCurves
  376. cbTextInCurves.Enabled = !cbEmbeddedFonts.Checked;
  377. if (!cbTextInCurves.Enabled)
  378. cbTextInCurves.Checked = false;
  379. // end
  380. }
  381. // cbPrintOptimized
  382. cbPrintOptimized.Enabled = !cbOriginalResolution.Checked;
  383. if (!cbPrintOptimized.Enabled)
  384. cbPrintOptimized.Checked = false;
  385. // end
  386. // cbJpegCompression
  387. if (cbColorSpace.SelectedIndex == (int)PDFExport.PdfColorSpace.CMYK || cbOriginalResolution.Checked)
  388. cbJpegCompression.Enabled = false;
  389. else
  390. cbJpegCompression.Enabled = true;
  391. if (!cbJpegCompression.Enabled)
  392. cbJpegCompression.Checked = false;
  393. // end
  394. // lblJpegQuality
  395. lblJpegQuality.Enabled = cbJpegCompression.Checked;
  396. // end
  397. // nudJpegQuality
  398. nudJpegQuality.Enabled = cbJpegCompression.Checked;
  399. // end
  400. }
  401. #endregion
  402. private void cbSignPdf_CheckedChanged(object sender, EventArgs e)
  403. {
  404. tbSignLocation.Enabled = cbSignPdf.Checked;
  405. tbSignReason.Enabled = cbSignPdf.Checked;
  406. tbSignContactInfo.Enabled = cbSignPdf.Checked;
  407. gbCertificate.Enabled = cbSignPdf.Checked;
  408. }
  409. private void btnSelectCertificatePath_Click(object sender, EventArgs e)
  410. {
  411. OpenFileDialog odf = new OpenFileDialog();
  412. if (odf.ShowDialog() == DialogResult.OK)
  413. {
  414. tbCertificatePath.Text = odf.FileName;
  415. }
  416. }
  417. private void cbSaveCertificatePassword_CheckedChanged(object sender, EventArgs e)
  418. {
  419. lbSaveCertificatePasswordWarning.Visible = cbSaveCertificatePassword.Checked;
  420. }
  421. }
  422. }